Tuesday, August 18, 2009

WebService invocation in Java using Axis2 (Async)

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
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.

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
    public static OMElement getSayHelloOMElement(){
        OMFactory fac=OMAbstractFactory.getOMFactory();
        OMNamespace omNs=fac.createOMNamespace("http://www.deepakmodi2006.blogspot.com/","deepak");
        OMElement method=fac.createOMElement("sayQuote",omNs);
        return method;
    public static void main(String[] args){
            Options options=new Options();
            ServiceClient sender=new ServiceClient();
            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) {

            System.out.println("Asynchronous web service already invoked...");

            while(!callback2.isComplete()) {
                System.out.println("Thread is waiting to get the response");
                //This block will keep executing till the response comes.
        }catch(Exception axisFault){
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/">
       <deepak:sayResponse xmlns:deepak="http://www.deepakmodi2006.blogspot.com/">0 to 9</deepak:sayResponse>
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.

1 comment:

  1. hai friend.. help me with hibernate .cfg mapping resource