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

CXF异步WebService发布和调用

发布时间:2020-12-16 21:52:54 所属栏目:安全 来源:网络整理
导读:原文地址:http://blog.csdn.net/d6619309/article/details/52694100 最近工作需要接触CXF异步webservice的发布和调用,在网上Google并捣鼓了好一阵子,总算成功了。毕竟这方面的资料比较少,自己总结一下写下这篇博文。本文将描述如何利用CXF来发布和调用异

原文地址:http://blog.csdn.net/d6619309/article/details/52694100

最近工作需要接触CXF异步webservice的发布和调用,在网上Google并捣鼓了好一阵子,总算成功了。毕竟这方面的资料比较少,自己总结一下写下这篇博文。本文将描述如何利用CXF来发布和调用异步的webservice,通过一个示例,带大家一步一步开发基于CXF的异步webservice及客户端调用程序。

【参考】

  • asynchronous-web-service-using-cxf
  • cxf-asynchronous-client
  • apache cxf docs

1.异步webservice简介

异步webservice可以让客户端调用线程在调用webservice的时候不必阻塞等待服务端返回结果,在调用请求后快速返回,然后做其他想做的事情。

1.1 客户端异步调用方式

(1) Callback

客户端通过实现javax.xml.ws.AsyncHandler,从而让服务端在做完操作后回调AsyncHandler的handleResponse方法进行异步处理。

(2) Polling

客户端拿到javax.xml.ws.Response类型的返回结果,然后不断轮询Response的isDone方法来判断是否调用已经完成。

1.2 服务端异步调用的实现

CXF提供了两种方法来响应客户端的异步webservice请求,包括:

(1) Continuations

CXF提供了API供开发者来创建并使用Continuation,关于continuation的详细介绍请查看以下链接:

continuations in cxf

(2) @UseAsyncMethod annotation

通过@UseAsyncMethod注解同步方法,让同步方法在满足异步调用条件下调用其对应的异步方法

   
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
@UseAsyncMethod public String sayHello(String username) { return "hello " + username; } public Future<String> sayHelloAsync(String username,AsyncHandler<String> asyncHandler) { // ... return null; } public Response<String> sayHelloAsync(String username) { null; }

如上所示,sayHello方法注解了@UseAsyncMethod,如果web容器支持异步调用,将会调用其对应的sayHelloAsync方法,如果不支持异步调用,那么将会调用同步的sayHello方法。

【注意】

CXF依赖于Web容器来发布webservice,必须确保Web容器支持异步机制才能使CXF发布的webservice成功处理来自客户端的异步请求。eg:通过Tomcat配置CXF发布webservice必须确保配置在web.xml的CXFServlet配置了async-supported为true

2. 示例

通过发布一个HelloService异步webservice以及开发HelloClient来向大家说明基于CXF如何开发和调用异步的webservice。示例代码可以在我的github拉取:CXFTutorial

2.1 环境说明

【软件环境】

  • JDK 6+
  • STS(Eclipse)
  • Tomcat7

【依赖库】

  • aopalliance-1.0.jar
  • asm-3.3.1.jar
  • commons-logging-1.1.1.jar
  • cxf-bundle-2.7.15.jar
  • httpasyncclient-4.0-beta3.jar
  • httpclient-4.2.5.jar
  • httpcore-4.2.4.jar
  • httpcore-nio-4.2.4.jar
  • neethi-3.0.3.jar
  • spring-aop-3.0.7.RELEASE.jar
  • spring-asm-3.0.7.RELEASE.jar
  • spring-beans-3.0.7.RELEASE.jar
  • spring-context-3.0.7.RELEASE.jar
  • spring-core-3.0.7.RELEASE.jar
  • spring-expression-3.0.7.RELEASE.jar
  • spring-web-3.0.7.RELEASE.jar
  • stax2-api-3.1.4.jar
  • woodstox-core-asl-4.4.1.jar
  • wsdl4j-1.6.3.jar
  • xmlschema-core-2.1.0.jar

2.2 开发webservice接口及实现类

2.2.1 开发HelloService接口

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    import javax.xml.ws.Response; import org.apache.cxf.annotations.UseAsyncMethod; import org.apache.cxf.jaxws.ServerAsyncResponse; @WebService(endpointInterface="com.hello.HelloService") class HelloServiceImpl implements @Override @UseAsyncMethod public String sayHello(String username) { System.out.println("execute sayHello method"); try { Thread.sleep(5000); } catch (InterruptedException e) { e.printStackTrace(); } "hello " + username; } @Override "execute sayHelloAsync method"); final ServerAsyncResponse<String> asyncResponse = new ServerAsyncResponse<String>(); new Thread() { public void run() { String result = sayHello(username); asyncResponse.set(result); System.out.println("Responding on background threadn"); asyncHandler.handleResponse(asyncResponse); } }.start(); return asyncResponse; } public Response<String> sayHelloAsync(String username) { // TODO Auto-generated method stub null; } }

    2.3 发布webservice

    2.3.1 配置cxf.xml

    通过spring配置CXF发布webservice的端口及地址

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    web-app "http://java.sun.com/xml/ns/javaee" version="3.0" "http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"> context-param> param-name>contextConfigLocationparam-name> param-value>WEB-INF/cxf.xmlparam-value> context-param> listener> listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class> listener> servlet> servlet-name>CXFServletservlet-name> servlet-class>org.apache.cxf.transport.servlet.CXFServletservlet-class> load-on-startup>1load-on-startup> async-supported>trueasync-supported> servlet> servlet-mapping> url-pattern>/services/*url-pattern> servlet-mapping> web-app>

    2.3.3 使用Tomcat加载应用

    完成以上步骤后通过Tomcat7加载应用,访问http://localhost:8080/CXFTutorial/services可以看到CXF发布的webservice

    ![show cxf publish services]({{ site.url }}/assets/image/async-webservice-using-cxf/show_cxf_publish_services.png)

    2.4 开发webservice客户端调用

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    TestAsyncHandler AsyncHandler<String> { private String reply; void handleResponse(Response<String> response) { try { System.out.println("handleResponse called"); reply = response.get(); } catch (Exception ex) { ex.printStackTrace(); } } public String getResponse() { return reply; } }

    (编辑:李大同)

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

      推荐文章
        热点阅读