java: axis2 Web Service四种调用模式

package userguide.clients;

import org.apache.axiom.om.OMAbstractFactory;
import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.OMFactory;
import org.apache.axiom.om.OMNamespace;

public class ClientUtil {

public static OMElement getEchoOMElement() {
OMFactory fac = OMAbstractFactory.getOMFactory();
OMNamespace omNs = fac.createOMNamespace(
"http://example1.org/example1", "example1");
OMElement method = fac.createOMElement("echo", omNs);
OMElement value = fac.createOMElement("Text", omNs);
value.addChild(fac.createOMText(value, "Axis2 Echo String "));
method.addChild(value);

return method;
}

public static OMElement getPingOMElement() {
OMFactory fac = OMAbstractFactory.getOMFactory();
OMNamespace omNs = fac.createOMNamespace(
"http://example1.org/example1", "example1");
OMElement method = fac.createOMElement("ping", omNs);
OMElement value = fac.createOMElement("Text", omNs);
value.addChild(fac.createOMText(value, "Axis2 Ping String "));
method.addChild(value);

return method;
}


}


发送请求返回响应并且阻塞的客户端
package userguide.clients;

import org.apache.axiom.om.OMElement;
import org.apache.axis2.AxisFault;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.client.ServiceClient;


public class EchoBlockingClient {
private static EndpointReference targetEPR = new EndpointReference("http://localhost:8080/axis2/services/MyService");

public static void main(String[] args) {
try {
OMElement payload = ClientUtil.getEchoOMElement();
Options options = new Options();

options.setTo(targetEPR);
options.setAction("urn:echo");

//Blocking invocation
ServiceClient sender = new ServiceClient();
sender.setOptions(options);
OMElement result = sender.sendReceive(payload);

System.out.println(result);

System.out.println(options.getTo());

} catch (AxisFault axisFault) {
axisFault.printStackTrace();
}
}
}

单向传输,非阻塞的客户端

try {OMElement payload = ClientUtil.getPingOMElement();Options options = new Options();options.setTo(targetEPR);ServiceClient serviceClient = new ServiceClient();serviceClient.setOptions(options);serviceClient.fireAndForget(payload); Thread.sleep(500);} catch (AxisFault axisFault) {axisFault.printStackTrace();}
发送请求返回响应,非阻塞,使用一条连接传输
package userguide.clients;

import org.apache.axiom.om.OMElement;
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.AsyncResult;
import org.apache.axis2.client.async.Callback;


public class EchoNonBlockingClient {
private static EndpointReference targetEPR = new EndpointReference("http://127.0.0.1:8080/axis2/services/MyService");

public static void main(String[] args) {
ServiceClient sender = null;
try {
OMElement payload = ClientUtil.getEchoOMElement();
Options options = new Options();
options.setTo(targetEPR);
options.setAction("urn:echo");

//Callback to handle the response
Callback callback = new Callback() {
public void onComplete(AsyncResult result) {
System.out.println(result.getResponseEnvelope());
}

public void onError(Exception e) {
e.printStackTrace();
}
};

//Non-Blocking Invocation
sender = new ServiceClient();
sender.setOptions(options);
sender.sendReceiveNonBlocking(payload, callback);

//Wait till the callback receives the response.
while (!callback.isComplete()) {
Thread.sleep(1000);
}

} catch (AxisFault axisFault) {
axisFault.printStackTrace();
} catch (Exception ex) {
ex.printStackTrace();
} finally {
try {
sender.cleanup();
} catch (AxisFault axisFault) {
//
}
}

}
}


发送请求返回响应,非阻塞,使用二条连接传输

package userguide.clients;

import org.apache.axiom.om.OMElement;
import org.apache.axis2.AxisFault;
import org.apache.axis2.Constants;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.client.ServiceClient;
import org.apache.axis2.client.async.AsyncResult;
import org.apache.axis2.client.async.Callback;

import javax.xml.namespace.QName;


public class EchoNonBlockingDualClient {
private static EndpointReference targetEPR = new EndpointReference("http://127.0.0.1:8080/axis2/services/MyService");

public static void main(String[] args) {
ServiceClient sender = null;
try {
OMElement payload = ClientUtil.getEchoOMElement();

Options options = new Options();
options.setTo(targetEPR);
options.setAction("urn:echo");
options.setTransportInProtocol(Constants.TRANSPORT_HTTP);
options.setUseSeparateListener(true);
options.setAction("urn:echo"); // this is the action mapping we put within the service.xml

//Callback to handle the response
Callback callback = new Callback() {
public void onComplete(AsyncResult result) {
System.out.println(result.getResponseEnvelope());
}

public void onError(Exception e) {
e.printStackTrace();
}
};

//Non-Blocking Invocation
sender = new ServiceClient();
sender.engageModule(Constants.MODULE_ADDRESSING);
sender.setOptions(options);
sender.sendReceiveNonBlocking(payload, callback);

//Wait till the callback receives the response.
while (!callback.isComplete()) {
Thread.sleep(1000);
}
//Need to close the Client Side Listener.

} catch (AxisFault axisFault) {
axisFault.printStackTrace();
} catch (Exception ex) {
ex.printStackTrace();
} finally {
try {
sender.cleanup();
} catch (AxisFault axisFault) {
//have to ignore this
}
}

}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值