异步调用webservice
一、异步调用
二、同步调用异步调用比较
同步调用:

异步调用:

三、jax-ws的同步和异步
在旧的基于JAX-RPC的webservice编程model中,是不支持异步的service 调用的,在最新的Jax-ws webservice 编程model中,加入了对webservice的异步调用的支持。
首先我来讲一下它的原理,大家不要以为在异步的调用下,从client到server 之间的soap message 流也是异步的,其实不是,Soap/Http 协议在同步跟异步的调用下是一样的,都是客户端的service在运行时打开一个connectin,发送请求,然后接收返回,这些都在同一个connection中。这种方式对我们有什么影响呢?从客户端程序的角度来讲,没有影响,客户端的编程模型是由WSDL中的messages跟port types 来定义的,只要这些东西没有改变,request 跟response是不是在同一个Tcp/ip 的session 中来发送对与我们来说没由影响,然后从架构跟资源的角度来讲,对我们的影响就大了,把连接层的资源跟应用层的程序运行状态绑定起来是由许多弊端的,假如在异步调用时,程序运行出现了异常,将会导致连接层的资源被一直占用,这样会极大的影响我们程序的,稳定性,可靠性,资源的使用跟性能。
四、异步调用实现
接用上一篇的例子,JAX-WS(三)构建简单webservice部署到tomcat上
先在工程目录下建一个binding.xml的文件:
<?xml version="1.0" encoding="UTF-8"?>
<bindings xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" wsdlLocation="http://localhost:8080/JaxwsWebServer/HelloService?wsdl"
xmlns="http://java.sun.com/xml/ns/jaxws">
<bindings node="wsdl:definitions">
<enableAsyncMapping>true</enableAsyncMapping>
</bindings>
</bindings>
用wsimport命令:
wsimport -b binding.xml -s ./src -d ./bin -p org.ccnt.jax.web.asyclient http://localhost:8080/JaxwsWebServer/HelloService?wsdl
生成如下文件:

新建类ClientAsyncMain调用:
package org.ccnt.jax.web.asyclient;
import javax.xml.ws.Response;
public class ClientAsyncMain {
public static void main(String[] args) {
HelloService service = new HelloService();
Hello port = service.getHelloPort();
Response<SayResponse> sayAsync = port.sayAsync("loull");
while (!sayAsync.isDone()) {
System.out.println("is not down");
}
try {
SayResponse callNameResponse = sayAsync.get();
String message = callNameResponse.getReturn();
System.out.println("~~~" + message);
} catch(Exception exception) {
exception.printStackTrace();
}
}
}
结果:
...
is not down
is not down
is not down
is not down
is not down
is not down
is not down
is not down
is not down
is not down
is not down
is not down
is not down
is not down
is not down
~~~hello, loull
五、异步的另一种实现,回调
package org.ccnt.jax.web.asyclient; import java.util.concurrent.ExecutionException; import javax.xml.ws.AsyncHandler;
import javax.xml.ws.Response; public class ClientAsyncCallback { public static void main(String[] args) throws InterruptedException {
HelloService service = new HelloService();
Hello port = service.getHelloPort();
port.sayAsync("loull", new AsyncHandler<SayResponse>() {
@Override
public void handleResponse(Response<SayResponse> res) {
SayResponse response = null;
try {
response = res.get();
String message = response.getReturn();
System.out.println(message);
} catch (InterruptedException | ExecutionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
for (int i=0; i<10; i++) {
System.out.println("Zzz...");
}
Thread.sleep(1500);
}
}
结果:
Zzz...
Zzz...
Zzz...
Zzz...
Zzz...
Zzz...
Zzz...
Zzz...
Zzz...
Zzz...
hello, loull
参考和扩展:
同步调用、回调和异步调用区别
异步调用webservice的更多相关文章
- 一个简单的webservice的demo(下)winform异步调用webservice
绕了一大圈,又开始接触winform的项目来了,虽然很小吧.写一个winform的异步调用webservice的demo,还是简单的. 一个简单的Webservice的demo,简单模拟服务 一个简单 ...
- Axis2(8):异步调用WebService
在前面几篇文章中都是使用同步方式来调用WebService.也就是说,如果被调用的WebService方法长时间不返回,客户端将一直被阻塞,直到该方法返回为止.使用同步方法来调用WebService虽 ...
- asp.net中异步调用WebService(异步页)[转]
由于asp2.0提供了异步页的支持使异步调用WebService的性能有了真正的提升.使用异步页,首先要设置Async="true",异步页是在Prerender和Prerende ...
- asp.net中异步调用webservice
WebService方法是不需要作任何修改的,只是在调用时采用异步的方式,这样在循环中,速度会显得快一点. 原来的方式: HotelMagWeb.com.china_sms.www.MainServi ...
- Asp.net 异步调用WebService
//服务代码 [WebMethod] public string Test(int sleepTimes, int val) { Thread.Sleep(sleepTimes); var log = ...
- 浅谈WebService开发二(同步与异步调用)转
上文 <http://www.dotnetgeek.cn/xuexiwebservice1.html>已经跟大家说了,如果创建一个webservice和简单的调用,本文将注重webserv ...
- 在Android 中使用KSOAP2调用WebService
WebService 是一种基于SOAP协议的远程调用标准.通过WebService可以将不同操作系统平台,不同语言.不同技术整合到一起.在Android SDK中并没有提供调用WebService的 ...
- Axis2之异步调用
本章主要介绍axis2接口的异步调用方式. 一般情况下,我们使用同步方法(invokeBlocking)调用axis2接口,如果被调用的WebService方法长时间不返回,客户端将一直被阻塞,直到该 ...
- 浅谈WebService开发三(动态调用WebService)转
在前两讲里,我已经向大家演示了如何使用WebService.同步, 异步调用WebService,而在实际开发过程中,可能会有多个WebService接口供你选择,而在程序执行过程中才决定使用哪一个 ...
随机推荐
- MYSQL导入导出.sql文件
MYSQL导入导出.sql文件 一.MYSQL的命令行模式的设置:桌面->我的电脑->属性->环境变量->新建->PATH=“:path\mysql\bin;”其中p ...
- PL/SQL Developer 显示中文乱码问题
简单版本: 首先,通过 select userenv('language') from dual; 查询oracle服务器端的编码,如为:AMERICAN_AMERICA.ZHS16GBK; 在我们的 ...
- 1st-code-review summary
每次做code review,先贤谆谆教诲便在耳畔响起: "There are only two hard problems in Computer Science: cache inval ...
- Bootstrap页面布局1 - 下载BS(bootstrap简称)
1.bootstrap 官方网站:http://wrongwaycn.github.io/bootstrap/docs/index.html 2.如图: 3.下载后得到如下目录结构 bootstrap ...
- 【转载】Http协议
HTTP是一个属于应用层的面向对象的协议,由于其简捷.快速的方式,适用于分布式超媒体信息系统.它于1990年提出,经过几年的使用与发展,得到不断地完善和扩展.目前在WWW中使用的是HTTP/1.0的第 ...
- Jsp入门学习笔记
#Jsp入门 一.JSP基础语法 1.JSP指令: page inlcude taglib 2.JSP注释: a.html注释: <!-- abcdefghijklmn --> b.jsp ...
- w-BIG TABLE-view+where-small table
w-BIG TABLE DROP PROCEDURE IF EXISTS truncate_insert_sales_rank_toparow_month; DELIMITER /w/ CREATE ...
- linux命令篇
普通用户登陆,充值root密码: sudo passwd root
- docker nexus oss
docker login/search x.x.x.x:8081 sonatype/docker-nexus Docker images for Sonatype Nexus with the Ora ...
- C++位操作符总结
#include <stdio.h> #include <memory.h> #include <malloc.h> #define MaxBinLength 16 ...