biGpython

生亦何欢,死亦何苦? 予我長袖,我必善舞!

导航

Axis2客户端生成的四种方式

使用AXIOM创建客户端
 
欲用AXIOM创建客户端,请执行以下步骤。
 
为了完整性,下面的目录结构将“用AXIOM创建服务”一节中的目录一并列出。 
 
- quickstartaxiom
   - README.txt
   - build.xml
   - resources
     - META-INF
       - services.xml
       - StockQuoteService.wsdl
   - src
     - samples
       - quickstart
         - service
           - axiom
             - StockQuoteService.java
         - clients
           - AXIOMClient.java
 
上述引用的AXIOMClient.java类定义如代码9所示。
 
Code Listing 9: The AXIOMClient class using AXIOM
 
package samples.quickstart.clients;
import org.apache.axiom.om.OMAbstractFactory;
import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.OMFactory;
import org.apache.axiom.om.OMNamespace;
import org.apache.axis2.Constants;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.client.ServiceClient;
public class AXIOMClient {
    private static EndpointReference targetEPR = 
        new EndpointReference("http://localhost:8080/axis2/services/StockQuoteService");
    public static OMElement getPricePayload(String symbol) {
        OMFactory fac = OMAbstractFactory.getOMFactory();
        OMNamespace omNs = fac.createOMNamespace("http://axiom.service.quickstart.samples/xsd", "tns");
        OMElement method = fac.createOMElement("getPrice", omNs);
        OMElement value = fac.createOMElement("symbol", omNs);
        value.addChild(fac.createOMText(value, symbol));
        method.addChild(value);
        return method;
    }
    public static OMElement updatePayload(String symbol, double price) {
        OMFactory fac = OMAbstractFactory.getOMFactory();
        OMNamespace omNs = fac.createOMNamespace("http://axiom.service.quickstart.samples/xsd", "tns");
        OMElement method = fac.createOMElement("update", omNs);
        OMElement value1 = fac.createOMElement("symbol", omNs);
        value1.addChild(fac.createOMText(value1, symbol));
        method.addChild(value1);
        OMElement value2 = fac.createOMElement("price", omNs);
        value2.addChild(fac.createOMText(value2,
                & nbsp;                 &nbs p;      Double.toString(price)));
        method.addChild(value2);
        return method;
    }
    public static void main(String[] args) {
        try {
            OMElement getPricePayload = getPricePayload("WSO");
            OMElement updatePayload = updatePayload("WSO", 123.42);
            Options options = new Options();
            options.setTo(targetEPR);
            options.setTransportInProtocol(Constants.TRANSPORT_HTTP);
            ServiceClient sender = new ServiceClient();
            sender.setOptions(options);
            sender.fireAndForget(updatePayload);
            System.err.println("price updated");
            OMElement result = sender.sendReceive(getPricePayload);
            String response = result.getFirstElement().getText();
            System.err.println("Current price of WSO: " + response);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }   
}
 

 

 
Axis2使用的AXIOM又叫AXIS对象模型,它是一个以StAX  API (XML的流式 API)为基础的类似DOM (文档对象模型)的结构。在这里,您 为调用服务提供的两个方法update和getPrice设置了有效载荷。有效载荷的建立过程类似于您为AXIOM服务的getPriceResponse 建立有效载荷的情形。然后,您设置可选类,并创建一个用于和服务端通讯的ServiceClient。接下来先调用Update方法,这是 一个一触即忘的方法,因为它没有任何返回值。最后调用getPrice方法从服务端获得当前股价并显示出来。
 
现在,您可以通过键入在Axis2_HOME/samples/quickstartaxiom的命令”ant run.client”构建并运行AXIOM客户端。
 
你应该看到以下输出: 
 
done
Current price of WSO: 123.42
 
 
 
使用ADB生成客户端
 
若要用ADB生成客户端,请执行以下步骤。 
 
输入下列在Axis2_HOME/samples/quickstartadb目录中的命令来创建ADB数据绑定的客户端: 
 
%AXIS2_HOME%\bin\WSDL2Java -uri resources\META-INF\StockQuoteService.wsdl -p samples.quickstart.clients -d adb -s -o build\client
 
或者,输入在Axis2_HOME/samples/quickstartadb目录中的命令”ant generate.client”。 
 
代码10是quickstartadb/src/samples/quickstart/clients/ADBClient.java的内容。
 
Code Listing 10: The ADBClient Class
 
package samples.quickstart.clients;
import samples.quickstart.service.adb.StockQuoteServiceStub;
public class ADBClient{
    public static void main(java.lang.String args[]){
        try{
            StockQuoteServiceStub stub =
                new StockQuoteServiceStub
                ("http://localhost:8080/axis2/services/StockQuoteService");
            getPrice(stub);
            update(stub);
            getPrice(stub);
        } catch(Exception e){
            e.printStackTrace();
            System.err.println("\n\n\n");
        }
    }
    /**//* fire and forget */
    public static void update(StockQuoteServiceStub stub){
        try{
            StockQuoteServiceStub.Update req = new StockQuoteServiceStub.Update();
            req.setSymbol ("ABC");
            req.setPrice (42.35);
            stub.update(req);
            System.err.println("price updated");
        } catch(Exception e){
            e.printStackTrace();
            System.err.println("\n\n\n");
        }
    }
    /**//* two way call/receive */
    public static void getPrice(StockQuoteServiceStub stub){
        try{
            StockQuoteServiceStub.GetPrice req = new StockQuoteServiceStub.GetPrice();
            req.setSymbol("ABC");
            StockQuoteServiceStub.GetPriceResponse res =
                stub.getPrice(req);
            System.err.println(res.get_return());
        } catch(Exception e){
            e.printStackTrace();
            System.err.println("\n\n\n");
        }
    }
}
 
 
这个类用你生成的Axis数据绑定创建了一个客户端存根。然后它调用web服务提供的 getPrice和update操作。该getPrice方法的操作创建GetPrice的有效载荷,并将符号设置为ABC。然后它发出请求,并显示当前 的股票价格。update方法创建了一个Update的有效载荷,并将标志设置为ABC,价格设置为42.35 。 
 
现在,请输入在Axis2_HOME/samples/quickstartadb目录中的命令” ant run.client”, 建立并运行客户端。 
 
你应该看到以下输出: 
 
42
price updated
42.35
 
 
 
使用XMLBeans生成客户端
 
若要使用XMLBeans生成客户端,请执行以下步骤。 
 
通过输入在xmlbeansClient目录中的下列命令生成数据绑定。 
 
%AXIS2_HOME%\bin\WSDL2Java -uri resources\META-INF\StockQuoteService.wsdl -p samples.quickstart.service.xmlbeans -d xmlbeans -s -o build\client
 
请注意,这将仅创建一个客户端存根代码而没有服务器端代码。 
 
下面看看quickstartxmlbeans/src/samples/quickstart/clients/XMLBEANSClient.java是如何定义的。见代码11 。
 
Code Listing 11: The XMLBEANSClient class
 
package samples.quickstart.clients;
import samples.quickstart.service.xmlbeans.StockQuoteServiceStub;
import samples.quickstart.service.xmlbeans.xsd.GetPriceDocument;
import samples.quickstart.service.xmlbeans.xsd.GetPriceResponseDocument;
import samples.quickstart.service.xmlbeans.xsd.UpdateDocument;
public class XMLBEANSClient{
    public static void main(java.lang.String args[]){
        try{
            StockQuoteServiceStub stub =
                new StockQuoteServiceStub
                ("http://localhost:8080/axis2/services/StockQuoteService");
            getPrice(stub);
            update(stub);
            getPrice(stub);
        } catch(Exception e){
            e.printStackTrace();
            System.err.println("\n\n\n");
        }
    }
    /**//* fire and forget */
    public static void update(StockQuoteServiceStub stub){
        try{
            UpdateDocument reqDoc = UpdateDocument.Factory.newInstance();
            UpdateDocument.Update req = reqDoc.addNewUpdate();
            req.setSymbol ("BCD");
            req.setPrice (42.32);
            stub.update(reqDoc);
            System.err.println("price updated");
        } catch(Exception e){
            e.printStackTrace();
            System.err.println("\n\n\n");
        }
    }
    /**//* two way call/receive */
    public static void getPrice(StockQuoteServiceStub stub){
        try{
            GetPriceDocument reqDoc = GetPriceDocument.Factory.newInstance();
            GetPriceDocument.GetPrice req = reqDoc.addNewGetPrice();
            req.setSymbol("BCD");
            GetPriceResponseDocument res =
                stub.getPrice(reqDoc);
            System.err.println(res.getGetPriceResponse().getReturn());
        } catch(Exception e){
            e.printStackTrace();
            System.err.println("\n\n\n");
        }
    }
}
 
 
这个类用你生成的XML Beans数据绑定产生一个客户端存根。然后他调用web服务的getPrice和update操作。这个getPrice 方法产生它的一个内部GetPrice类,GetPriceDocument,并将它的符号设置为ABC.它然后发送请求并提取出GetPriceResponseDocument,显示价格为42.32。
 
现在键入Axis2_HOME/samples/quickstartxmlbeans目录中的命令”ant run.client”来构建和运行项目。你将看到下列输出
 
42 
price updated 
42.32
 
 
 
使用JiBX生成一个客户端
 
若要使用JiBX生成一个客户端,请执行下列步骤
 
键入在Axis2_HOME/samples/quickstartjibx目录中的下列命令来生成客户端存根。
 
%AXIS2_HOME%\bin\wsdl2java -uri resources\META-INF\StockQuoteService.wsdl -p samples.quickstart.clients -d jibx -s -uw -o build\client
 
或者简单的运行命令"ant generate.client". 
 
下面看看quickstartjibx/src/samples/quickstart/clients/JiBXClient.java是如何定义的。见代码12.
 
Code Listing 12: The JiBXClient class
 
package samples.quickstart.clients;
import samples.quickstart.service.jibx.StockQuoteServiceStub;
public class JiBXClient{
    public static void main(java.lang.String args[]){
        try{
            StockQuoteServiceStub stub =
                new StockQuoteServiceStub
                ("http://localhost:8080/axis2/services/StockQuoteService");
            getPrice(stub);
            update(stub);
            getPrice(stub);
        } catch(Exception e){
            e.printStackTrace();
            System.err.println("\n\n\n");
        }
    }
    /**//* fire and forget */
    public static void update(StockQuoteServiceStub stub){
        try{
            stub.update("CDE", new Double(42.35));
            System.err.println("price updated");
        } catch(Exception e){
            e.printStackTrace();
            System.err.println("\n\n\n");
        }
    }
    /**//* two way call/receive */
    public static void getPrice(StockQuoteServiceStub stub){
        try{
            System.err.println(stub.getPrice("CDE"));
        } catch(Exception e){
            e.printStackTrace();
            System.err.println("\n\n\n");
        }
    }
}
 
 
这个类用你生成的JiBX 客户端存根访问web服务的getPrice和update操作。这个 getPrice 方法发送获取股票“ABC”价格的请求,然后显示当前股价。Update将“ABC”的股价设为42.35。 
 
现在,通过键入Axis2_HOME/samples/quickstartjibx目录中的命令"ant run.client"来构建和运行客户端。
 
您将看到下列输出:
 
 42 
 price updated 
 42.35 

    

posted on 2012-04-16 23:44  biGpython  阅读(1868)  评论(0编辑  收藏  举报