Web services using Asynchronous communication
Dear reader,
Web services can be invoked by two ways:
1) Synchronous communication, here client will be waiting till it gets the response from web service. This
is called sequence execution of web service.
2) Asynchronous communication, here client will not wait for response from web service, once it sends the
request. This is like a parallel execution (calling web service and doing its own task).
How to work with Web service and Synchronous Communication is already discussed in my another blog in link
"http://deepakmodi2006.blogspot.com/2009/08/web-service-in-java-using-axis2.html".
This is for Asynchronous communication. I am writing only client code here as web service at
Server side will be same, only Client execution needs to be changed. Please see the below link for
Web service deployed at Server.
//Writing
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.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 ArithmeticAsyncClient {
private static EndpointReference targetEPR=new EndpointReference
("http://10.200.101.137:8080/axis2/services/QuoteService?wsdl");
public static OMElement getSayHelloOMElement(){
OMFactory fac=OMAbstractFactory.getOMFactory();
OMNamespace omNs=fac.createOMNamespace("http://www.deepakmodi2006.blogspot.com/","deepak");
OMElement method=fac.createOMElement("sayQuote",omNs);
method.setText("Numbers");
System.out.println(method);
return method;
}
public static void main(String[] args){
try{
Options options=new Options();
options.setTo(targetEPR);
ServiceClient sender=new ServiceClient();
sender.setOptions(options);
OMElement sayQuote=TestClient.getSayHelloOMElement();
Callback callback2 = new Callback() {
public void onComplete(AsyncResult result) {
String responseString = result.getResponseEnvelope().toString();
System.out.println("Got response :"+responseString);
}
public void onError(Exception e) {
e.printStackTrace();
}
};
sender.sendReceiveNonBlocking(sayQuote,callback2);
System.out.println("Asynchronous web service already invoked...");
while(!callback2.isComplete()) {
Thread.sleep(100);
System.out.println("Thread is waiting to get the response");
//This block will keep executing till the response comes.
}
}catch(Exception axisFault){
axisFault.printStackTrace();
}
}
}
//Output:
log4j:WARN No appenders could be found for logger (org.apache.axis2.util.Loader).
log4j:WARN Please initialize the log4j system properly.
<deepak:sayQuote xmlns:deepak="http://www.deepakmodi2006.blogspot.com/">Numbers</deepak:sayQuote>
Asynchronous web service already invoked...
Thread is waiting to get the response
Thread is waiting to get the response
Thread is waiting to get the response
Got response :
<?xml version='1.0' encoding='utf-8'?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Body>
<deepak:sayResponse xmlns:deepak="http://www.deepakmodi2006.blogspot.com/">0 to 9</deepak:sayResponse>
</soapenv:Body>
</soapenv:Envelope>
Thread is waiting to get the response
---------------------------------------------------------
//The containts "Got response :" and then XML contents come in single line in output, just to show reader,
I have broken this in line.
==========================================END==============================
Tuesday, August 18, 2009
WebService invocation in Java using Axis2 (Async)
Subscribe to:
Post Comments (Atom)
hai friend.. help me with hibernate .cfg mapping resource
ReplyDelete