", "name"));
System.out.println(result.getText());
synchronized (this) {
this.notify();
}
}
public boolean isComplete() {
return complete;
}
public void onFault(MessageContext msgContext) {
System.out.println(msgContext.getEnvelope().getBody().getFault()
.toString());
synchronized (this) {
this.notify();
}
}
public void onError(Exception e) {
e.printStackTrace();
synchronized (this) {
this.notify();
}
}
public void onComplete() {
this.complete = true;
synchronized (this) {
this.notify();
}
}
}
有了callback接口,我们来写我们的webservice调用类
HelloWorldWithReturnNonBlock.java
package org.sky.axis2.helloworld;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axiom.om.OMAbstractFactory;
import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.OMFactory;
import org.apache.axiom.om.OMNamespace;
import org.apache.axis2.AxisFault;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.client.ServiceClient;
import org.apache.axis2.client.async.AxisCallback;
import org.apache.axis2.context.MessageContext;
public class HelloWorldWithReturnNonBlock {
private static EndpointReference targetEPR = new EndpointReference(
"http://localhost:8080/Axis2Service/services/HelloWorld");
public void sayHello() {
OMFactory fac = OMAbstractFactory.getOMFactory();
OMNamespace omNs = fac.createOMNamespace(
"http://helloworld.axis2.sky.org", "");
OMElement method = fac.createOMElement("sayHello", omNs);
OMElement name = fac.createOMElement("name", omNs);
name.setText("ymk");
method.addChild(name);
method.build();
Options options = new Options();
options.setTo(targetEPR);
ServiceClient sender = null;
try {
HelloWorldNonBlockCB callback = new HelloWorldNonBlockCB();
sender = new ServiceClient();
sender.setOptions(options);
sender.sendReceiveNonBlocking(method, callback);
synchronized (callback) {
try {
callback.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
} catch (AxisFault e) {
e.printStackTrace();
} finally {
if (sender != null)
try {
sender.cleanup();
} catch (Exception e) {
}
}
}
public static void main(String[] args) {
HelloWorldWithReturnNonBlock testClient = new HelloWorldWithReturnNonBlock();
testClient.sayHello();
}
}
注意加粗标红处的代码。
4.3 非阻塞式双工HelloWorldWithReturnDualNonBlock.java
非阻塞式的Web Service客户端代码很简单,在此不做任何擅述,我们就说终极的非阻塞式双工模式的Web Service客户端使用Axis2的API如何实现。
记住以下几个口决:
- options.setUseSeparateListener(true);
非阻塞式双工模式,会在客户端也打开一个监听器,而且一直不断的监听着服务器的返回值,该进程一旦被吊用,会一直被挂在客户端这边。
options.setTransportInProtocol(Constants.TRANSPORT_HTTP);
在双工模式下Web Service的客户端需要寻址,即engageModule,这个engageModule需要这样的一个参数:
engageModule(“addressing”)
或者也可写成:
engageModule(Constants.MODULE_ADDRESSING);
这个engageModule就是需要访问你的工程的WEB-INF\modules\目录下的一个叫addressing