CXF之从wsdl first开始的异步调用实例

客户端调用配置:

<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxws="http://cxf.apache.org/jaxws" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd"> <jaxws:client id="client" serviceClass="cxf.server.HelloWorld" address="http://localhost:8085/java_first_spring_support1/service/HelloWorld" /> </beans>  

 

wsdl合约文档:

<?xml version='1.0' encoding='UTF-8'?> <wsdl:definitions name="HelloWorldService" targetNamespace="http://server.cxf/" xmlns:ns1="http://cxf.apache.org/bindings/xformat" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://server.cxf/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <wsdl:types> <xs:schema elementFormDefault="unqualified" targetNamespace="http://server.cxf/" version="1.0" xmlns:tns="http://server.cxf/" xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:element name="sayHi" type="tns:sayHi" /> <xs:element name="sayHiResponse" type="tns:sayHiResponse" /> <xs:complexType name="sayHi"> <xs:sequence> <xs:element minOccurs="0" name="arg0" type="xs:string" /> </xs:sequence> </xs:complexType> <xs:complexType name="sayHiResponse"> <xs:sequence> <xs:element minOccurs="0" name="return" type="xs:string" /> </xs:sequence> </xs:complexType> </xs:schema> </wsdl:types> <wsdl:message name="sayHiResponse"> <wsdl:part element="tns:sayHiResponse" name="parameters"> </wsdl:part> </wsdl:message> <wsdl:message name="sayHi"> <wsdl:part element="tns:sayHi" name="parameters"> </wsdl:part> </wsdl:message> <wsdl:portType name="HelloWorld"> <wsdl:operation name="sayHi"> <wsdl:input message="tns:sayHi" name="sayHi"> </wsdl:input> <wsdl:output message="tns:sayHiResponse" name="sayHiResponse"> </wsdl:output> </wsdl:operation> </wsdl:portType> <wsdl:binding name="HelloWorldServiceSoapBinding" type="tns:HelloWorld"> <soap:binding style="document" mce_style="document" transport="http://schemas.xmlsoap.org/soap/http" /> <wsdl:operation name="sayHi"> <soap:operation soapAction="" style="document" mce_style="document" /> <wsdl:input name="sayHi"> <soap:body use="literal" /> </wsdl:input> <wsdl:output name="sayHiResponse"> <soap:body use="literal" /> </wsdl:output> </wsdl:operation> </wsdl:binding> <wsdl:service name="HelloWorldService"> <wsdl:port binding="tns:HelloWorldServiceSoapBinding" name="HelloWorldPort"> <soap:address location="http://localhost:8085/java_first_spring_support1/service/HelloWorld" /> </wsdl:port> </wsdl:service> </wsdl:definitions> 

 

wsdl的外部绑定声明:

<bindings xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" wsdlLocation="java_first_spring_support1.wsdl" xmlns="http://java.sun.com/xml/ns/jaxws"> <bindings node="wsdl:definitions"> <enableAsyncMapping>true</enableAsyncMapping> </bindings> </bindings> 

 

生成的端点接口:

package cxf.server; import java.util.concurrent.Future; import javax.jws.WebMethod; import javax.jws.WebParam; import javax.jws.WebResult; import javax.jws.WebService; import javax.xml.bind.annotation.XmlSeeAlso; import javax.xml.ws.AsyncHandler; import javax.xml.ws.RequestWrapper; import javax.xml.ws.Response; import javax.xml.ws.ResponseWrapper; /** * This class was generated by Apache CXF 2.2.5 * Wed Jun 16 19:43:50 CST 2010 * Generated source version: 2.2.5 * */ @WebService(targetNamespace = "http://server.cxf/", name = "HelloWorld") @XmlSeeAlso({ObjectFactory.class}) public interface HelloWorld { @RequestWrapper(localName = "sayHi", targetNamespace = "http://server.cxf/", className = "cxf.server.SayHi") @ResponseWrapper(localName = "sayHiResponse", targetNamespace = "http://server.cxf/", className = "cxf.server.SayHiResponse") @WebMethod(operationName = "sayHi") public Response<cxf.server.SayHiResponse> sayHiAsync( @WebParam(name = "arg0", targetNamespace = "") java.lang.String arg0 ); @RequestWrapper(localName = "sayHi", targetNamespace = "http://server.cxf/", className = "cxf.server.SayHi") @ResponseWrapper(localName = "sayHiResponse", targetNamespace = "http://server.cxf/", className = "cxf.server.SayHiResponse") @WebMethod(operationName = "sayHi") public Future<?> sayHiAsync( @WebParam(name = "arg0", targetNamespace = "") java.lang.String arg0, @WebParam(name = "asyncHandler", targetNamespace = "") AsyncHandler<cxf.server.SayHiResponse> asyncHandler ); @WebResult(name = "return", targetNamespace = "") @RequestWrapper(localName = "sayHi", targetNamespace = "http://server.cxf/", className = "cxf.server.SayHi") @ResponseWrapper(localName = "sayHiResponse", targetNamespace = "http://server.cxf/", className = "cxf.server.SayHiResponse") @WebMethod public java.lang.String sayHi( @WebParam(name = "arg0", targetNamespace = "") java.lang.String arg0 ); }  

 

回调方式所用的回调类:

package cxf.operate; import javax.xml.ws.AsyncHandler; import javax.xml.ws.Response; import cxf.server.SayHiResponse; public class MyAsyncHandler implements AsyncHandler<SayHiResponse> { @Override public void handleResponse(Response<SayHiResponse> res) { System.out.println("yyyyyyyyyyyyyyyyyyyyyyyyyyyyyy"); } }  

 

客户端执行异步调用:

package cxf.operate; import java.util.concurrent.Future; import java.util.concurrent.TimeUnit; import javax.xml.ws.Response; import org.springframework.context.support.ClassPathXmlApplicationContext; import cxf.server.HelloWorld; import cxf.server.SayHiResponse; /** * 测试异步调用(轮询,回调) * @author fhd * */ public class HelloWorldAsyncTest { public static void main(String[] args) throws Exception { ClassPathXmlApplicationContext app = new ClassPathXmlApplicationContext("beans.xml"); HelloWorld hw = (HelloWorld) app.getBean("client"); String rsp1 = hw.sayHi("aaaaaaaaaaaaaa"); System.out.println("同步方法--sayHi(): " + rsp1); Response<SayHiResponse> rsp2 = hw.sayHiAsync("bbbbbb"); while(!rsp2.isDone()){ Thread.sleep(10); System.out.println("Non-blocking polling(无阻塞轮询)-------"); } SayHiResponse sayHiRsp = rsp2.get(); String return1 = sayHiRsp.getReturn(); System.out.println("异步方法(无阻塞轮询)--sayHiAsync: " + return1); Response<SayHiResponse> rsp3 = hw.sayHiAsync("cccccc"); SayHiResponse return2 = rsp3.get(5, TimeUnit.MILLISECONDS); String s2 = return2.getReturn(); System.out.println("异步方法(阻塞轮询)--sayHiAsync: " + s2); MyAsyncHandler handler = new MyAsyncHandler(); Future<?> rsp4 = hw.sayHiAsync("ddddddd", handler); while(!rsp4.isDone()){ Thread.sleep(10); System.out.println("异步方法(回调)-----------------------------"); } } }  

 

 

你可能感兴趣的:(CXF之从wsdl first开始的异步调用实例)