Sunday, November 30, 2008

Axis2 service Invocations

Axis2 client API provides a comprehensive set of ways to access a web service. In fact there are four ways depending on the following two properties.

1.Synchronous/Asynchronous invocations – whether client thread wait until response comes or not
2.Single/Dual Channel invocations. - whether the response comes on the back channel or a separate channel initiated by the server.

Now lets see each and every method separately.

1.Synchronous Single channel
In this way the client side invocation thread blocks until the response message comes. If the transport is Http it can be seen that the response comes at the back channel with Http status 200 ok. By default Axis2 works on the single channel mode. Therefore following method does the job. Note that this method returns the response.
serviceClient.sendReceive(getTestOMElement())

2.Asynchronous Single channel
Unlike in the earlier way this kind of invocation returns the client thread immediately. User should register a callback to get the response. But response comes in the back channel as in the earlier case. Following code can be used for this kind of invocation.

AxisCallback axisCallback = new AxisCallback() {
public void onMessage(MessageContext msgContext) {
System.out.println("Got the message ==> " + msgContext.getEnvelope().getBody().getFirstElement());
}

public void onFault(MessageContext msgContext) {

}

public void onError(Exception e) {
System.out.println("Received an error ...");
}

public void onComplete() {

}
};
serviceClient.sendReceiveNonBlocking(getTestOMElement(), axisCallback);


3.Synchronous Dual channel mode
The only difference of this invocation compare to others is that the invocation happens using two different http channels. This type of invocation can only be done using Addressing. i.e both client and service should have addressing enabled. Before sending the request Axis2 client starts a Simple Http server at client side and set the Endpoint reference address of this server to the reply-To addressing header. The request channel receives a Http 202 Accepted header. Server reads the reply-to header from the request and starts a new channel to the reply-to header address. Here is the code for this.
serviceClient.engageModule("addressing")
serviceClient.getOptions().setUseSeparateListener(true);
serviceClient.sendReceive(getTestOMElement());


The useSeparateListener attribute is used to specify this dual channel mode.

4.Asynchronous Dual channel mode
This is the complete asynchronous invocation method. Client immediately returns the thread and response message receive in a separate channel. This kind of access can be made by adding the addressing engaging and useSeperateListner code for the second type of invocation.

AxisCallback axisCallback = new AxisCallback() {
public void onMessage(MessageContext msgContext) {
System.out.println("Got the message ==> " + msgContext.getEnvelope().getBody().getFirstElement());
}

public void onFault(MessageContext msgContext) {

}

public void onError(Exception e) {
e.printStackTrace();
System.out.println("Received an error ...");
}

public void onComplete() {

}
};
serviceClient.engageModule("addressing")
serviceClient.getOptions().setUseSeparateListener(true);
serviceClient.sendReceiveNonBlocking(getTestOMElement(), axisCallback);

No comments: