CXF之用Dispatch处理异步调用实例

服务接口:

package cxf.server; import javax.jws.WebService; @WebService public interface HelloWorld { String sayHi(String text); }  

 

服务接口实现:

package cxf.server; import javax.jws.WebService; @WebService(endpointInterface = "cxf.server.HelloWorld") public class HelloWorldImpl implements HelloWorld { public String sayHi(String text) { try { Thread.sleep(30); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("sayHi called"); return "Hello " + text; } }  

 

服务端配置:

<?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"> <import resource="classpath:META-INF/cxf/cxf.xml" /> <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" /> <import resource="classpath:META-INF/cxf/cxf-servlet.xml" /> <jaxws:server id="helloWorld" serviceClass="cxf.server.HelloWorld" address="/HelloWorld"> <jaxws:serviceBean> <bean class="cxf.server.HelloWorldImpl"/> </jaxws:serviceBean> <jaxws:features> <bean class="org.apache.cxf.feature.LoggingFeature"/> </jaxws:features> </jaxws:server> </beans>  

 

客户端调用1---轮询方式异步调用:

package cxf.client; import java.io.InputStream; import java.net.URL; import javax.xml.namespace.QName; import javax.xml.soap.MessageFactory; import javax.xml.soap.SOAPMessage; import javax.xml.transform.dom.DOMSource; import javax.xml.ws.Dispatch; import javax.xml.ws.Response; import javax.xml.ws.Service; public final class Client1 { public static void main(String args[]) throws Exception { URL wsdlUrl = new URL("http://localhost:8085/cxf-dispatch-test/service/HelloWorld?wsdl"); QName serviceName = new QName("http://server.cxf/", "HelloWorldService"); Service service = Service.create(wsdlUrl,serviceName); QName portName = new QName("http://server.cxf/", "HelloWorldPort"); Dispatch<DOMSource> disp = service.createDispatch(portName, DOMSource.class, Service.Mode.MESSAGE); InputStream input = Client1.class.getResourceAsStream("soap1.xml"); MessageFactory factory = MessageFactory.newInstance (); SOAPMessage soapMessage = factory.createMessage(null, input); DOMSource dom = new DOMSource(soapMessage.getSOAPPart()); //轮询方式 Response<DOMSource> rsp = disp.invokeAsync(dom); while(!rsp.isDone()){ Thread.sleep(5); System.out.println("sleep 5"); } DOMSource domRsp = rsp.get(); System.out.println(domRsp.getNode().getLastChild().getNodeName()); System.out.println(domRsp.getNode().getLastChild().getTextContent()); } }  

 

客户端调用2---回调方式异步调用:

package cxf.client; import java.io.InputStream; import java.net.URL; import java.util.concurrent.Future; import javax.xml.namespace.QName; import javax.xml.soap.MessageFactory; import javax.xml.soap.SOAPMessage; import javax.xml.ws.Dispatch; import javax.xml.ws.Service; public final class Client2 { public static void main(String args[]) throws Exception { URL wsdlUrl = new URL("http://localhost:8085/cxf-dispatch-test/service/HelloWorld?wsdl"); QName serviceName = new QName("http://server.cxf/", "HelloWorldService"); Service service = Service.create(wsdlUrl,serviceName); QName portName = new QName("http://server.cxf/", "HelloWorldPort"); Dispatch<SOAPMessage> disp = service.createDispatch(portName, SOAPMessage.class, Service.Mode.MESSAGE); InputStream input = Client2.class.getResourceAsStream("soap1.xml"); MessageFactory factory = MessageFactory.newInstance (); SOAPMessage soapMessage = factory.createMessage(null, input); MyAsyncHandler<SOAPMessage> handler = new MyAsyncHandler<SOAPMessage>(); //回调方式 Future<?> rsp = disp.invokeAsync(soapMessage, handler); while(!rsp.isDone()){ Thread.sleep(5); System.out.println("aaaaaaa"); } SOAPMessage soapRsp = (SOAPMessage)rsp.get(); System.out.println(soapRsp.getSOAPBody().getNodeName()); System.out.println(soapRsp.getSOAPBody().getLastChild().getTextContent()); } }  

 

回调方式的AsyncHandler实现类:

package cxf.client; import javax.xml.ws.AsyncHandler; import javax.xml.ws.Response; public class MyAsyncHandler<T> implements AsyncHandler<T> { /** * 这个回调类必须实现handleResponse()方法,它被调用由CXF运行时通知客户端响应已经到达。 */ @Override public void handleResponse(Response<T> res) { System.out.println("eeeeee"); } }  

 

请求的xml数据:soap1.xml

<?xml version="1.0" encoding="utf-8" ?> <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <SOAP-ENV:Body> <m:sayHi xmlns:m="http://server.cxf/"> <arg0>I like you!aha!!</arg0> </m:sayHi> </SOAP-ENV:Body> </SOAP-ENV:Envelope>  

 

 

还有一个单向调用(OneWay)

 

当一个请求不需要一个响应的时候,你的远程调用可以使用Dispatch对象上的invokeOneWay()方法。略。

 

 

你可能感兴趣的:(exception,String,webservice,service,Class,SOAP)