Blocking API - 調(diào)用端要等被調(diào)用的函數(shù)運行完畢才繼續(xù)往下走。
Non-Bloking API - 調(diào)用端運行完調(diào)用函數(shù)以后就直接往下走了,調(diào)用端和被調(diào)用端是異步執(zhí)行的。返回值是用回調(diào)函數(shù)來實現(xiàn)的。
這種異步叫做API層異步(API Level Asynchrony)。他們只用到一個連接來發(fā)送和接收消息,而且,假如是那種需要運行很長時間的函數(shù),還會碰到Time Out 錯誤,假如用兩個連接分別處理發(fā)送和接收消息,調(diào)用的時間就可以縮短,也可以解決Time Out 問題。用兩個連接來分別處理發(fā)送和接收消息,叫做傳輸層異步(Transport Level Asynchrony)。
EchoBlockingClient.java public class EchoBlockingClient { private static EndpointReference targetEPR = new EndpointReference("http://localhost:8080/axis2/services/MyService");
EchoBlockingDualClient.java public class EchoBlockingDualClient { private static EndpointReference targetEPR = new EndpointReference("http://127.0.0.1:8080/axis2/services/MyService");
public class EchoNonBlockingClient { private static EndpointReference targetEPR = new EndpointReference("http://127.0.0.1:8080/axis2/services/MyService");
public class EchoNonBlockingDualClient { private static EndpointReference targetEPR = new EndpointReference("http://127.0.0.1:8080/axis2/services/MyService");
//The boolean flag informs the axis2 engine to use two separate transport connection //to retrieve the response. call.engageModule(new QName(Constants.MODULE_ADDRESSING)); call.setTransportInfo(Constants.TRANSPORT_HTTP, Constants.TRANSPORT_HTTP, true);
//Callback to handle the response Callback callback = new Callback() { public void onComplete(AsyncResult result) { try { StringWriter writer = new StringWriter(); result.getResponseEnvelope().serializeWithCache(XMLOutputFactory.newInstance() .createXMLStreamWriter(writer)); writer.flush(); System.out.println(writer.toString());
//Wait till the callback receives the response. while (!callback.isComplete()) { Thread.sleep(1000); } //Need to close the Client Side Listener. call.close();