webservice—— Axis2的客户端调用
???? Web services提供的服务多种多样,有的可以马上获得结果,有的要消耗很长的时间。所以,如果我们需要多种调用方式来对付不同的情况。 try { OMElement payload = ClientUtil.getEchoOMElement(); Options options = new Options(); options.setTo(targetEPR); // this sets the location of MyService service ServiceClient serviceClient = new ServiceClient(); serviceClient.setOptions(options); OMElement result = sender.sendReceive(payload); System.out.println(result); } catch (AxisFault axisFault) { axisFault.printStackTrace(); } ?? try { OMElement payload = ClientUtil.getPingOMElement(); Options options = new Options(); options.setTo(targetEPR); ServiceClient serviceClient = new ServiceClient(); serviceClient.setOptions(options); serviceClient.fireAndForget(payload); } catch (AxisFault axisFault) { axisFault.printStackTrace(); } ? public abstract void onComplete(AsyncResult result); public abstract void onError(Exception e); public boolean isComplete() {} ? try { OMElement payload = ClientUtil.getEchoOMElement(); Options options = new Options(); options.setTo(targetEPR); options.setAction("urn:echo"); //Callback to handle the response Callback callback = new Callback() { public void onComplete(AsyncResult result) { System.out.println(result.getResponseEnvelope()); } public void onError(Exception e) { e.printStackTrace(); } }; //Non-Blocking Invocation sender = new ServiceClient(); sender.setOptions(options); sender.sendReceiveNonBlocking(payload,callback); //Wait till the callback receives the response. while (!callback.isComplete()) { Thread.sleep(1000); } } catch (AxisFault axisFault) { axisFault.printStackTrace(); } catch (Exception ex) { ex.printStackTrace(); } finally { try { sender.cleanup(); } catch (AxisFault axisFault) { // } } ? EchoNonBlockingDualClient? try { OMElement payload = ClientUtil.getEchoOMElement(); Options options = new Options(); options.setTo(targetEPR); options.setTransportInProtocol(Constants.TRANSPORT_HTTP); options.setUseSeparateListener(true); options.setAction("urn:echo"); // this is the action mapping we put within the service.xml //Callback to handle the response Callback callback = new Callback() { public void onComplete(AsyncResult result) { System.out.println(result.getResponseEnvelope()); } public void onError(Exception e) { e.printStackTrace(); } }; //Non-Blocking Invocation sender = new ServiceClient(); sender.engageModule(new QName(Constants.MODULE_ADDRESSING)); sender.setOptions(options); sender.sendReceiveNonBlocking(payload,callback); //Wait till the callback receives the response. while (!callback.isComplete()) { Thread.sleep(1000); } //Need to close the Client Side Listener. } catch (AxisFault axisFault) { axisFault.printStackTrace(); } catch (Exception ex) { ex.printStackTrace(); } finally { try { sender.finalizeInvoke(); } catch (AxisFault axisFault) { //have to ignore this } } ?? package samples.quickstart.service.adb; import java.util.HashMap; import samples.quickstart.service.pojo.*; public class StockQuoteServiceSkeleton implements StockQuoteServiceSkeletonInterface { private static HashMap map = new HashMap(); public void update(Update param0) { map.put(param0.getSymbol(),new Double(param0.getPrice())); } public GetPriceResponse getPrice(GetPrice param1) { Double price = (Double) map.get(param1.getSymbol()); double ret = 42; if (price != null) { ret = price.doubleValue(); } GetPriceResponse res = new GetPriceResponse(); res.set_return(ret); return res; } } ? ?????? 3:客户端的Stub也同样需要生成,进入cmd,设置路径到工程的根下面,运行如下语句: ?????? 4:同样需要services.xml文件来描述,如下: <service name="StockQuoteService" scope="application"> <messageReceivers> <messageReceiver mep="http://www.w3.org/2004/08/wsdl/in-out" class="samples.quickstart.service.adb.StockQuoteServiceMessageReceiverInOut"/> <messageReceiver mep="http://www.w3.org/2004/08/wsdl/in-only" class="samples.quickstart.service.adb.StockQuoteServiceMessageReceiverInOnly"/> </messageReceivers> <parameter name="ServiceClass">samples.quickstart.service.adb.StockQuoteServiceSkeleton </parameter> <operation name="update" mep="http://www.w3.org/2004/08/wsdl/in-only"> <actionMapping>urn:update</actionMapping> </operation> <operation name="getPrice" mep="http://www.w3.org/2004/08/wsdl/in-out"> <actionMapping>urn:getPrice</actionMapping> <outputActionMapping>http://quickstart.samples/StockQuoteServicePortType/getPriceResponse</outputActionMapping> </operation> </service> ? package samples.quickstart.service.adb; import samples.quickstart.clients.StockQuoteServiceStub; public class Client { 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("nnn"); } } /* 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("nnn"); } } /* 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("nnn"); } } } ? ?????? 6:跟ADB的方式类似的还有:XMLBeans、JiBX进行服务邦定的方式,都是通过WSDL2Java来生成服务端的Skeleton和客户端的Stub,还有相应的接口和实现的架子,具体的服务和客户端的调用还是需要开发人员来写的。 ???????? 1:<portType> 元素是最重要的 WSDL 元素。它可描述一个 web service、可被执行的操作,以及相关的消息。可以把 <portType> 元素比作传统编程语言中的一个函数库(或一个模块、或一个类)。 <message name="newTermValues"> <part name="term" type="xs:string"/> <part name="value" type="xs:string"/> </message> <portType name="glossaryTerms"> <operation name="setTerm"> <input name="newTerm" message="newTermValues"/> </operation> </portType > ?? <message name="getTermRequest"> <part name="term" type="xs:string"/> </message> <message name="getTermResponse"> <part name="value" type="xs:string"/> </message> <portType name="glossaryTerms"> <operation name="getTerm"> <input message="getTermRequest"/> <output message="getTermResponse"/> </operation> </portType> ? ??? WSDL的绑定: <message name="getTermRequest"> <part name="term" type="xs:string" /> </message> <message name="getTermResponse"> <part name="value" type="xs:string" /> </message> <portType name="glossaryTerms"> <operation name="getTerm"> <input message="getTermRequest" /> <output message="getTermResponse" /> </operation> </portType> <binding type="glossaryTerms" name="b1"> <soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http" /> <operation> <soap:operation soapAction="http://example.com/getTerm" /> <input> <soap:body use="literal" /> </input> <output> <soap:body use="literal" /> </output> </operation> </binding> ?? operation 元素定义了每个端口提供的操作符。 例子: <?xml version="1.0" encoding="UTF-8"?> <wsdl:definitions xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:ns1="http://org.apache.axis2/xsd" xmlns:ns="http://pojo.service.quickstart.samples" xmlns:wsaw="http://www.w3.org/2006/05/addressing/wsdl" xmlns:http="http://schemas.xmlsoap.org/wsdl/http/" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/" targetNamespace="http://pojo.service.quickstart.samples"> <wsdl:types> <xs:schema attributeFormDefault="qualified" elementFormDefault="qualified" targetNamespace="http://pojo.service.quickstart.samples"> <xs:element name="getPrice"> <xs:complexType> <xs:sequence> <xs:element minOccurs="0" name="symbol" nillable="true" type="xs:string"/> </xs:sequence> </xs:complexType> </xs:element> <xs:element name="getPriceResponse"> <xs:complexType> <xs:sequence> <xs:element minOccurs="0" name="return" type="xs:double"/> </xs:sequence> </xs:complexType> </xs:element> <xs:element name="update"> <xs:complexType> <xs:sequence> <xs:element minOccurs="0" name="symbol" nillable="true" type="xs:string"/> <xs:element minOccurs="0" name="price" type="xs:double"/> </xs:sequence> </xs:complexType> </xs:element> </xs:schema> </wsdl:types> <wsdl:message name="getPriceRequest"> <wsdl:part name="parameters" element="ns:getPrice"/> </wsdl:message> <wsdl:message name="getPriceResponse"> <wsdl:part name="parameters" element="ns:getPriceResponse"/> </wsdl:message> <wsdl:message name="updateRequest"> <wsdl:part name="parameters" element="ns:update"/> </wsdl:message> <wsdl:portType name="StockQuoteServicePortType"> <wsdl:operation name="getPrice"> <wsdl:input message="ns:getPriceRequest" wsaw:Action="urn:getPrice"/> <wsdl:output message="ns:getPriceResponse" wsaw:Action="urn:getPriceResponse"/> </wsdl:operation> <wsdl:operation name="update"> <wsdl:input message="ns:updateRequest" wsaw:Action="urn:update"/> </wsdl:operation> </wsdl:portType> <wsdl:binding name="StockQuoteServiceSoap11Binding" type="ns:StockQuoteServicePortType"> <soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document"/> <wsdl:operation name="getPrice"> <soap:operation soapAction="urn:getPrice" style="document"/> <wsdl:input> <soap:body use="literal"/> </wsdl:input> <wsdl:output> <soap:body use="literal"/> </wsdl:output> </wsdl:operation> <wsdl:operation name="update"> <soap:operation soapAction="urn:update" style="document"/> <wsdl:input> <soap:body use="literal"/> </wsdl:input> </wsdl:operation> </wsdl:binding> <wsdl:binding name="StockQuoteServiceSoap12Binding" type="ns:StockQuoteServicePortType"> <soap12:binding transport="http://schemas.xmlsoap.org/soap/http" style="document"/> <wsdl:operation name="getPrice"> <soap12:operation soapAction="urn:getPrice" style="document"/> <wsdl:input> <soap12:body use="literal"/> </wsdl:input> <wsdl:output> <soap12:body use="literal"/> </wsdl:output> </wsdl:operation> <wsdl:operation name="update"> <soap12:operation soapAction="urn:update" style="document"/> <wsdl:input> <soap12:body use="literal"/> </wsdl:input> </wsdl:operation> </wsdl:binding> <wsdl:binding name="StockQuoteServiceHttpBinding" type="ns:StockQuoteServicePortType"> <http:binding verb="POST"/> <wsdl:operation name="getPrice"> <http:operation location="StockQuoteService/getPrice"/> <wsdl:input> <mime:content type="text/xml" part="getPrice"/> </wsdl:input> <wsdl:output> <mime:content type="text/xml" part="getPrice"/> </wsdl:output> </wsdl:operation> <wsdl:operation name="update"> <http:operation location="StockQuoteService/update"/> <wsdl:input> <mime:content type="text/xml" part="update"/> </wsdl:input> </wsdl:operation> </wsdl:binding> <wsdl:service name="StockQuoteService"> <wsdl:port name="StockQuoteServiceHttpSoap11Endpoint" binding="ns:StockQuoteServiceSoap11Binding"> <soap:address location="http://localhost:8080/axis2/services/StockQuoteService"/> </wsdl:port> <wsdl:port name="StockQuoteServiceHttpSoap12Endpoint" binding="ns:StockQuoteServiceSoap12Binding"> <soap12:address location="http://localhost:8080/axis2/services/StockQuoteService"/> </wsdl:port> <wsdl:port name="StockQuoteServiceHttpEndpoint" binding="ns:StockQuoteServiceHttpBinding"> <http:address location="http://localhost:8080/axis2/services/StockQuoteService"/> </wsdl:port> </wsdl:service> </wsdl:definitions> ? 七:SOAP <?xml version='1.0' ?> <env:Envelope xmlns:env="http://www.w3.org/2003/05/SOAP-envelope"> <env:Header> </env:Header> <env:Body> <cms:getNumberOfArticles xmlns:cms="http://www.daily-moon.com/cms"> <cms:category>classifieds</cms:category> <cms:subcategory>forsale</cms:subcategory> </cms:getNumberOfArticles> </env:Body> </env:Envelope> ?? <?xml version='1.0' ?> <env:Envelope xmlns:env="http://www.w3.org/2003/05/SOAP-envelope"> <env:Header> </env:Header> <env:Body> </env:Body> </env:Envelope> ?? <?xml version='1.0' ?> <env:Envelope xmlns:env="http://www.w3.org/2003/05/SOAP-envelope"> <env:Header> <wsa:ReplyTo xmlns:wsa= "http://schemas.xmlSOAP.org/ws/2004/08/addressing"> <wsa:Address> http://schemas.xmlSOAP.org/ws/2004/08/addressing/role/anonymous </wsa:Address> </wsa:ReplyTo> <wsa:From> <wsa:Address> http://localhost:8080/axis2/services/MyService</wsa:Address> </wsa:From> <wsa:MessageID>ECE5B3F187F29D28BC11433905662036</wsa:MessageID> </env:Header> <env:Body> </env:Body> </env:Envelope> ?? ?? 4:soap的Body <?xml version='1.0' ?> <env:Envelope xmlns:env="http://www.w3.org/2003/05/SOAP-envelope"> <env:Header> ... </env:Header> <env:Body> <cms:addArticle xmlns:cms="http://www.daily-moon.com/cms"> <cms:category>classifieds</category> <cms:subcategory>forsale</cms:subcategory> <cms:articleHeadline></cms:articleHeadline> <cms:articleText>Vintage 1963 T-Bird. Less than 300 miles. Driven by my daughter until I took it away. Serious inquires only. 555-3264 after 7 PM.</cms:articleText> </cms:addArticle> </env:Body> </env:Envelope> ?? ?? 4:样式和编码 <env:Envelope xmlns:env="http://www.w3.org/2003/05/SOAP-envelope"> <env:Header> </env:Header> <env:Body> <cms:addArticle xmlns:cms="http://www.daily-moon.com/cms"> <cms:category>classifieds</category> <cms:subcategory>forsale</cms:subcategory> <cms:articleHeadline></cms:articleHeadline> <cms:articleText>Vintage 1963 T-Bird. Less than 300 miles. Driven by my daughter until I took it away. Serious inquires only. 555-3264 after 7 PM.</cms:articleText> </cms:addArticle> </env:Body> </env:Envelope> ?? <?xml version='1.0' ?> <env:Envelope xmlns:env="http://www.w3.org/2003/05/SOAP-envelope"> <env:Header> </env:Header> <env:Body> <category>classifieds</category> <subcategory>forsale</subcategory> <articleHeadline></articleHeadline> <articleText>Vintage 1963 T-Bird. Less than 300 miles. Driven by my daughter until I took it away. Serious inquires only. 555-3264 after 7 PM.</articleText> </env:Body> </env:Envelope> ? ?? 在这种情况下,消息本身并不包含有关数据所提交到的进程的信息,此工作由路由软件进行。例如,所有对特定 URL 或端点的调用都可能指向特定的操作。另外,从技术上讲,可以使用 document/encoded 样式,但目前还没有人这样做,因此可以将其忽略。?? 5:消息交换模式????? 发送消息,就实质而言,只有两个选择:??????? 请求/响应:在请求/响应模式种,以 SOAP 消息的形式发送请求,然后直接等待发送回响应。请求可以为同步的,也可以是异步的。 ??????? 单向消息传递:这种情况也称为“Fire and Forget”方法,发送请求但并不等待响应。可以在仅传递信息时或并不关心接收者对此如何响应时使用此方法。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |