异步调用webservice

一、异步调用

asynchronous call(异步调用):一个可以无需等待被调用函数的返回值就让操作继续进行的方法
举例:
  异步调用就是你 喊 你朋友吃饭 ,你朋友说知道了 ,待会忙完去找你 ,你就去做别的了。
  同步调用就是你 喊 你朋友吃饭 ,你朋友在忙 ,你就一直在那等,等你朋友忙完了 ,你们一起去。
 

二、同步调用异步调用比较

同步调用:

 

异步调用:

 

 

 

三、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

 

 

参考和扩展:

5天学会jaxws-webservice编程第一天

同步调用、回调和异步调用区别

同步调用WebService和异步调用WebService

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值