异步调用Webservice

来源:互联网 发布:开淘宝店成本 编辑:程序博客网 时间:2024/05/16 11:38

异步,说到异步需要首先将以下同步。同步就是代码按照顺序执行,当前面的代码的请求没有正常返回结果的情况下,后面的代码是不能运行。而异步正好和这点不同,异步是代码运行后,不管当前的请求是否返回结果,后面的代码都会继续运行。


1.   编写服务端代码:

      

[java] view plaincopy
  1. public class AsynchronousService {  
  2.   
  3.     public String execute() {  
  4.         System.out.println("正在执行此代码……");  
  5.         // 延迟5秒后,返回结果  
  6.         try {  
  7.             Thread.sleep(5000);  
  8.         } catch (InterruptedException e) {  
  9.             e.printStackTrace();  
  10.         }  
  11.         return "完成";  
  12.     }  
  13. }  

2.   编写services.xml代码,打包并发布至对应文件夹下

 

[html] view plaincopy
  1. <service name="AsyncService">  
  2.     <description>  
  3.         AsyncService  
  4.     </description>  
  5.     <parameter name="ServiceClass">  
  6.         server.asynchronous.AsynchronousService  
  7.     </parameter>  
  8.     <messageReceivers>  
  9.         <messageReceiver mep="http://www.w3.org/2004/08/wsdl/in-out"  
  10.             class="org.apache.axis2.rpc.receivers.RPCMessageReceiver" />  
  11.         <messageReceiver mep="http://www.w3.org/2004/08/wsdl/in-only"  
  12.             class="org.apache.axis2.rpc.receivers.RPCInOnlyMessageReceiver" />  
  13.     </messageReceivers>  
  14. </service>  

3.   客户端代码:

[java] view plaincopy
  1. package client;  
  2.   
  3. import java.io.IOException;  
  4.   
  5. import javax.xml.namespace.QName;  
  6.   
  7. import org.apache.axis2.addressing.EndpointReference;  
  8. import org.apache.axis2.client.Options;  
  9. import org.apache.axis2.client.async.AxisCallback;  
  10. import org.apache.axis2.context.MessageContext;  
  11. import org.apache.axis2.rpc.client.RPCServiceClient;  
  12.   
  13. public class AsynTest {  
  14.   
  15.       
  16.     public static void main(String[] args) throws IOException {  
  17.         String target = "http://localhost:8080/axis2/services/AsyncService";  
  18.         RPCServiceClient client = new RPCServiceClient();  
  19.         Options options = client.getOptions();  
  20.         options.setManageSession(true);  
  21.           
  22.         EndpointReference epr = new EndpointReference(target);  
  23.         options.setTo(epr);  
  24.           
  25.         QName qname = new QName("http://asynchronous.server""execute");  
  26.         //指定调用的方法和传递参数数据,及设置返回值的类型  
  27.         client.invokeNonBlocking(qname, new Object[] {}, new AxisCallback() {  
  28.               
  29.             public void onMessage(MessageContext ctx) {  
  30.                 System.out.println(ctx.getEnvelope());  
  31.                 System.out.println("Message:" + ctx.getEnvelope().getFirstElement().getFirstElement().getFirstElement().getText());  
  32.             }  
  33.               
  34.             public void onFault(MessageContext ctx) {  
  35.             }  
  36.               
  37.             public void onError(Exception ex) {  
  38.             }  
  39.               
  40.             public void onComplete() {  
  41.             }  
  42.         });  
  43.         //断点此处,查看异步结果  
  44.         System.out.println("异步WebService");  
  45.         //阻止程序退出  
  46.         System.in.read();  
  47.     }  
  48.       
  49. }  
0 0
原创粉丝点击