java webservice 异步_八、 异步调用WebService

本文介绍了如何在Java中使用Axis2进行异步调用WebService。通过创建异步服务端代码、配置services.xml文件并打包为aar,然后编写客户端测试代码,展示了如何利用RPCServiceClient的invokeNonBlocking方法进行异步调用,并实现回调处理结果。
摘要由CSDN通过智能技术生成

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

关于异步在此就不再赘述了,有兴趣的可以去网上查查这方面的资料。

1、 编写服务器端的代码。

9310e85a14af99de4811ff4c77f1f911.png

24a924a57ba6b3f2b51fc9edb7ea4186.png代码package com.hoo.service;

/**

* function:异步WebService服务器端代码

* @author hoojo

* @createDate 2011-3-14 上午08:16:59

* @file AsynchronousService.java

* @package com.hoo.service

* @project Axis2WebService

* @blog http://blog.csdn.net/IBM_hoojo

* @email hoojo_@126.com

* @version 1.0

*/

public class AsynchronousService {

public String execute() {

System.out.println("正在执行此代码……");

//延迟5秒后,返回结果

try {

Thread.sleep(5000);

} catch (InterruptedException e) {

e.printStackTrace();

}

return "完成";

}

}

2、 services.xml文件,创建aar文件,然后复制aar文件到[tomcat-home]\webapps\axis2\WEB-INF\services目录下

services.xml

9310e85a14af99de4811ff4c77f1f911.png

24a924a57ba6b3f2b51fc9edb7ea4186.png代码

AsyncService

com.hoo.service.AsynchronousService

class="org.apache.axis2.rpc.receivers.RPCMessageReceiver" />

class="org.apache.axis2.rpc.receivers.RPCInOnlyMessageReceiver" />

3、 编写客户端测试代码

9310e85a14af99de4811ff4c77f1f911.png

24a924a57ba6b3f2b51fc9edb7ea4186.png代码package com.hoo.service;

import java.io.IOException;

import javax.xml.namespace.QName;

import org.apache.axis2.addressing.EndpointReference;

import org.apache.axis2.client.Options;

import org.apache.axis2.client.async.AxisCallback;

import org.apache.axis2.context.MessageContext;

import org.apache.axis2.rpc.client.RPCServiceClient;

/**

* function:异步WebService客户端代码

* @author hoojo

* @createDate 2011-3-14 上午09:00:03

* @file AsynchronousServiceClient.java

* @package com.hoo.service

* @project Axis2WebService

* @blog http://blog.csdn.net/IBM_hoojo

* @email hoojo_@126.com

* @version 1.0

*/

public class AsynchronousServiceClient {

public static void main(String[] args) throws IOException {

String target = "http://localhost:8080/axis2/services/AsyncService?wsdl";

RPCServiceClient client = new RPCServiceClient();

Options options = client.getOptions();

options.setManageSession(true);

EndpointReference epr = new EndpointReference(target);

options.setTo(epr);

QName qname = new QName("http://service.hoo.com", "execute");

//指定调用的方法和传递参数数据,及设置返回值的类型

client.invokeNonBlocking(qname, new Object[] {}, new AxisCallback() {

public void onMessage(MessageContext ctx) {

System.out.println(ctx.getEnvelope());

System.out.println("Message:" + ctx.getEnvelope().getFirstElement().getFirstElement().getFirstElement().getText());

}

public void onFault(MessageContext ctx) {

}

public void onError(Exception ex) {

}

public void onComplete() {

}

});

System.out.println("异步WebService");

//阻止程序退出

System.in.read();

}

}

上面是异步调用WebService的代码,调用的方法是client.invokeNonBlocking,这个方法有三个参数,参数一是执行的方法签名,参数二是执行该方法的参数,参数三是异步回调,这里隐式实现AxiaCallback接口

注意的是运行程序的时候要用Debug方式运行。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值