加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

异步调用Webservice

发布时间:2020-12-16 22:57:12 所属栏目:安全 来源:网络整理
导读:异步,说到异步需要首先将以下同步。同步就是代码按照顺序执行,当前面的代码的请求没有正常返回结果的情况下,后面的代码是不能运行。而异步正好和这点不同,异步是代码运行后,不管当前的请求是否返回结果,后面的代码都会继续运行。 1. ? 编写服务端代码

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


1. ? 编写服务端代码:

?? ? ?

[java]? view plain copy
  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]? copy
    <service?name="AsyncService">??
  1. ????description>??
  2. ????????AsyncService??
  3. </ ????parameter?name="ServiceClass" ????????server.asynchronous.AsynchronousService??
  4. parametermessageReceivers ????????messageReceiver?mep="http://www.w3.org/2004/08/wsdl/in-out"??
  5. ????????????class="org.apache.axis2.rpc.receivers.RPCMessageReceiver"?/>??
  6. messageReceiver?mep="http://www.w3.org/2004/08/wsdl/in-only"??
  7. ????????????class="org.apache.axis2.rpc.receivers.RPCInOnlyMessageReceiver"? service>??

3. ? 客户端代码:

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读