异步调用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接口供你选择,而在程序执行过程中才决定使用哪一个 ...
随机推荐
- Natural Language Toolkit
http://www.nltk.org/ >>> import nltk >>> nltk.download()
- w-WAITING---
<p id="w_last" style="color: red; font-size: 6em;">w-WAITING---</p>& ...
- Artificial Intelligence Research Methodologies 人工智能研究方法
Computer Science An Overview _J. Glenn Brookshear _11th Edition To appreciate the field of artificia ...
- SVM神经网络的术语理解
SVM(Support Vector Machine)翻译成中文是支持向量机, 这里的“机(machine,机器)”实际上是一个算法.而支持向量则是指那些在间隔区边缘的训练样本点[1]. 当初看到这个 ...
- Redis学习一 五种基本的数据类型
首先一定要确保Redis是运行这的. 不知道为啥,我的电脑陪完服务以后,刚配完的时候好使,已重启就不行了,死活起不来. 好吧,这些暂时不是很重要. 运行 redis-server.exe 在开一个窗口 ...
- CDH商业版本的搭建(hadoop+hive+sqoop)
一:准备工作 1.步骤 1)hadoop ->下载解压 ->修改配置文件 ->hadoop-env JAVA_HOME ->core-site fs.defaultFS had ...
- C++ 线程类的一个实现
.h #ifndef CTHREAD_H_ #define CTHREAD_H_ #include "plat.h" class CThread { public: enum { ...
- 自动换行的矢量文字(android demo)
由于矢量字体的宽度不同,自测android字体,发现当中文字体大小为100像素时,字母s等 宽度大概在52,字母l等 宽度大概在26,这样自动换行就不可以按字符的个数计算截取每行显示的字串. 直接上代 ...
- interview review
缘起: 因为最近要找工作,自己总结了一下面试的注意事项. 1自我介绍方法 1.基本情况:姓名.年龄.学历.家庭与理想. 简单明了,不要啰嗦. 2.学习能力:专业知识.勤奋好学. 用事实说明学习能力不错 ...
- asp.net字符串的数学表达式计算结果
using System; using System.Collections.Generic; using System.Web; using System.CodeDom.Compiler; usi ...