When we invoke a service in a session-aware manner, deployed using SOAP, we need to retrieve the ServiceGroupId. This is not a problem if we use the same ServiceClient instance, however, when we use two or more different instances of a ServiceClient, then it is somewhat difficult to do so. In this story by Deepal Jayasinghe, he explains how to this can be done.
Applies To
| Environment | Axis2 Java |
Background
If we have only a single client, then we can have a SOAP session functioning, simply by following the two steps given below:
-
Engage addressing module
-
set 'manage session flag' in option object
Once we do so, the rest will be handled by Axis2. This means, that it will copy the necessary parameters from a response message to subsequent request messages.
However, when we want to use two or more ServiceClient s, we need to follow the following procedure. In few words, we need to copy the ServiceGropuId from one serviceClient to another. The ServiceGroupId can be found in the replyTo header, as the reference parameter. So what we need to do is to copy this value from one client to another.
Following sample code illustrates copying the servicegroupid to file:
ServiceClient client = new ServiceClient();
client.engageModule(“addressing”);
Options opts = new Options();
EndpointReference to = new EndpointReference();
to.setAddress("to address");
opts.setTo(to);
client.setOptions(opts);
//do the invocation code here
OMElement serviceGroupID = (OMElement) client.getServiceContext().getTargetEPR().getAllReferenceParameters(
).get(new QName("http://ws.apache.org/namespaces/axis2","ServiceGroupId"));FileOutputStream out = new FileOutputStream("foo.txt");
serviceGroupID.serialize(out);
out.flush();
Copying the ID to the Next Request
For the purpose of sending the ID in the next request, we create a new ServiceClient and add the serviceGroupId:
ServiceClient newclient = new ServiceClient();
Options option = new Options();
option.setAction("urn:add");
EndpointReference newTo = new EndpointReference();
StAXBuilder builder = new StAXOMBuilder(new FileInputStream("foo.txt"));
OMElement element = builder.getDocumentElement();
newTo.setAddress("to address");
// adding the reference parameter
newTo.addReferenceParameter(element);
option.setTo(newTo);
newclient.setOptions(opts2);
Reference:
- www.developer.com/java/web/article.php/3620661
Author:
Deepal Jayasinghe, deepal at wso2 dot com