CXF之用Dispatch处理异步调用实例 - fhd001的专栏 - 博客频道 - CSDN.NET

服务接口:

 

[java]  view plain copy
 
  1. package cxf.server;  
  2. import javax.jws.WebService;  
  3. @WebService  
  4. public interface HelloWorld {  
  5.     String sayHi(String text);  
  6. }  

 

 

 

服务接口实现:

 

[java]  view plain copy
 
  1. package cxf.server;  
  2. import javax.jws.WebService;  
  3. @WebService(endpointInterface = "cxf.server.HelloWorld")  
  4. public class HelloWorldImpl implements HelloWorld {  
  5.     public String sayHi(String text) {  
  6.           
  7.         try {  
  8.             Thread.sleep(30);  
  9.         } catch (InterruptedException e) {  
  10.             e.printStackTrace();  
  11.         }  
  12.           
  13.         System.out.println("sayHi called");  
  14.         return "Hello " + text;  
  15.     }  
  16. }  

 

 

 

服务端配置:

 

[xhtml]  view plain copy
 
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxws="http://cxf.apache.org/jaxws"  
  4.     xsi:schemaLocation="  
  5.     http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd  
  6.     http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">  
  7.     <import resource="classpath:META-INF/cxf/cxf.xml" />  
  8.     <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />  
  9.     <import resource="classpath:META-INF/cxf/cxf-servlet.xml" />  
  10.     <jaxws:server id="helloWorld" serviceClass="cxf.server.HelloWorld" address="/HelloWorld">  
  11.         <jaxws:serviceBean>  
  12.             <bean class="cxf.server.HelloWorldImpl"/>  
  13.         </jaxws:serviceBean>  
  14.         <jaxws:features>  
  15.             <bean class="org.apache.cxf.feature.LoggingFeature"/>  
  16.         </jaxws:features>  
  17.     </jaxws:server>  
  18.       
  19. </beans>  

 

 

 

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

 

[java]  view plain copy
 
  1. package cxf.client;  
  2. import java.io.InputStream;  
  3. import java.net.URL;  
  4. import javax.xml.namespace.QName;  
  5. import javax.xml.soap.MessageFactory;  
  6. import javax.xml.soap.SOAPMessage;  
  7. import javax.xml.transform.dom.DOMSource;  
  8. import javax.xml.ws.Dispatch;  
  9. import javax.xml.ws.Response;  
  10. import javax.xml.ws.Service;  
  11. public final class Client1 {  
  12.     public static void main(String args[]) throws Exception {  
  13.           
  14.         URL wsdlUrl = new URL("http://localhost:8085/cxf-dispatch-test/service/HelloWorld?wsdl");  
  15.         QName serviceName = new QName("http://server.cxf/""HelloWorldService");  
  16.         Service service = Service.create(wsdlUrl,serviceName);  
  17.         QName portName = new QName("http://server.cxf/""HelloWorldPort");  
  18.         Dispatch<DOMSource> disp = service.createDispatch(portName, DOMSource.class, Service.Mode.MESSAGE);  
  19.           
  20.         InputStream input = Client1.class.getResourceAsStream("soap1.xml");  
  21.         MessageFactory factory = MessageFactory.newInstance ();  
  22.         SOAPMessage soapMessage = factory.createMessage(null, input);  
  23.         DOMSource dom = new DOMSource(soapMessage.getSOAPPart());  
  24.           
  25.         //轮询方式  
  26.         Response<DOMSource> rsp = disp.invokeAsync(dom);  
  27.         while(!rsp.isDone()){  
  28.             Thread.sleep(5);  
  29.             System.out.println("sleep 5");  
  30.         }  
  31.         DOMSource domRsp = rsp.get();  
  32.         System.out.println(domRsp.getNode().getLastChild().getNodeName());  
  33.         System.out.println(domRsp.getNode().getLastChild().getTextContent());  
  34.     }  
  35. }  

 

 

 

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

 

[java]  view plain copy
 
  1. package cxf.client;  
  2. import java.io.InputStream;  
  3. import java.net.URL;  
  4. import java.util.concurrent.Future;  
  5. import javax.xml.namespace.QName;  
  6. import javax.xml.soap.MessageFactory;  
  7. import javax.xml.soap.SOAPMessage;  
  8. import javax.xml.ws.Dispatch;  
  9. import javax.xml.ws.Service;  
  10. public final class Client2 {  
  11.     public static void main(String args[]) throws Exception {  
  12.           
  13.         URL wsdlUrl = new URL("http://localhost:8085/cxf-dispatch-test/service/HelloWorld?wsdl");  
  14.         QName serviceName = new QName("http://server.cxf/""HelloWorldService");  
  15.         Service service = Service.create(wsdlUrl,serviceName);  
  16.         QName portName = new QName("http://server.cxf/""HelloWorldPort");  
  17.         Dispatch<SOAPMessage> disp = service.createDispatch(portName, SOAPMessage.class, Service.Mode.MESSAGE);  
  18.           
  19.         InputStream input = Client2.class.getResourceAsStream("soap1.xml");  
  20.         MessageFactory factory = MessageFactory.newInstance ();  
  21.         SOAPMessage soapMessage = factory.createMessage(null, input);  
  22.           
  23.         MyAsyncHandler<SOAPMessage> handler = new MyAsyncHandler<SOAPMessage>();  
  24.           
  25.         //回调方式   
  26.         Future<?> rsp = disp.invokeAsync(soapMessage, handler);  
  27.         while(!rsp.isDone()){  
  28.             Thread.sleep(5);  
  29.             System.out.println("aaaaaaa");  
  30.         }  
  31.         SOAPMessage soapRsp = (SOAPMessage)rsp.get();  
  32.         System.out.println(soapRsp.getSOAPBody().getNodeName());  
  33.         System.out.println(soapRsp.getSOAPBody().getLastChild().getTextContent());  
  34.           
  35.     }  
  36. }  

 

 

 

回调方式的AsyncHandler实现类:

 

[java]  view plain copy
 
  1. package cxf.client;  
  2. import javax.xml.ws.AsyncHandler;  
  3. import javax.xml.ws.Response;  
  4. public class MyAsyncHandler<T> implements AsyncHandler<T> {  
  5.     /**  
  6.      * 这个回调类必须实现handleResponse()方法,它被调用由CXF运行时通知客户端响应已经到达。 
  7.     */  
  8.     @Override  
  9.     public void handleResponse(Response<T> res) {  
  10.           
  11.         System.out.println("eeeeee");  
  12.           
  13.     }  
  14. }  

 

 

阅读全文……

你可能感兴趣的:(java,webservice)