When we invoke a service in a session-aware manner, i.e. deploy a service in a SOAP session we need to send the ServiceGroupId. This is not a problem if we use the same ServiceClient instance, however, if we are going to use two or more different instances of ServiceClient then it is somewhat difficult to do so. When we have single client we can get the SOAP session working using following two steps:
-
Engage addressing module
-
set the manage session flag in option object
Once we do so , the rest will be handle by Axis2 , meaning it will copy the necessary parameters from response message to subsequent request messages.
However when we want to use two or more ServiceClient we need to follow the following procedure. In simple word we need to copy the ServiceGropuId from one serviceClient to another. The ServiceGroupId is there in the replyTo header as the reference parameter. So we need to copy the reference parameter 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 into next request
Sending the ID in the next request , in this case we create a new ServiceClient and add the serviceGroupId.
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);
client.setOptions(opts2);
When we do the service invocation , you will be able to see the ServiceGroupId in the request and will be able to stay in the same session as well.