首先使用axis2发布WebService
http://songjianyong.iteye.com/blog/1920078
下载axis2-1.6.2-bin.zip文件并解压到E:\tools\axis2-1.6.2-bin\axis2-1.6.2中,如下图:
![](http://static.oschina.net/uploads/space/2013/0805/104106_S2C4_47886.png)
然后设置axis2_home环境变量并生成WebService客户端代码,如下图:
其中
D:\Workspaces\java\songjy\axis2-demo\target是生成的WebService客户端代码保存路径
在D:\Workspaces\java\songjy\axis2-demo\target\src\client中则有一个java文件HelloStub.java
同步调用示例代码
public void test(){
try {
HelloStub.Hello hello = new HelloStub.Hello();
hello.setName("猪八戒");
HelloStub stub = new HelloStub();
System.out.println(stub.hello(hello).local_return);
} catch (AxisFault e) {
e.printStackTrace();
} catch (RemoteException e) {
e.printStackTrace();
}
}
执行效果如下:
您好,猪八戒,现在的时间是:Mon Aug 05 11:01:08 CST 2013
异步调用前需生成异步调用客户端代码,执行如下图红色部分命令:
相比之下,在生成异步客户端代码时多了个参数:-a
异步调用示例代码:
public class Test extends TestCase {
public void test(){
try {
HelloStub stub = new HelloStub();
HelloStub.Hello hello = new HelloStub.Hello();
hello.setName("沙和尚");
System.out.println(stub.hello(hello).local_return);//同步
hello.setName("孙行者");
stub.starthello(hello, new AsynCallback());//异步
Thread.sleep(1000);
} catch (AxisFault e) {
e.printStackTrace();
} catch (RemoteException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
private class AsynCallback extends HelloCallbackHandler {
@Override
public void receiveResulthello(HelloResponse result) {
System.out.println(result.local_return);
}
}
}