KB - How to validate Axis2/Java session handling capability with transportsession scope
Submitted on February 24, 2008 - 23:29. Story : Level : Project :
Apache Axis2 provides session management capabilities for both client and server sides. Axis2 supports four levels of session scopes. Charitha Kankanamge explains.
- Request
- soapsession
- application
- transportsession
You can deploy your Web service using any of the above session scopes. In this story, we are discussing Transportsession scope, in which we highlight how a service is deployed in transport session scope and how to test transport session management, in a simple RESTful manner.
Applies To
| Apache Axis2 | 1.3 or later |
Procedure:
Axis2 transportsession uses transport level session management techniques to manage sessions of a Web service. Life time of a session is governed by the underlined transport, not by Axis2 itself. If a service is deployed in a transportsession scope, then, there will be service instances for each of the transport session.
The following steps demonstrate, how an Axis2 service is deployed in transportsession scope and a simple RESTful invocation mechanism, to test its session handling capabilities.
Step 1
Create a service implementation class as given below, that sets a property in serviceContext object and increments the value of the property in each invocation.
public class Transportsessionservice {
public int multiply(int k,int j){
ServiceContext serviceContext =
MessageContext.getCurrentMessageContext().getServiceContext();
if((Integer) serviceContext.getProperty("VALUE") == null){
serviceContext.setProperty("VALUE", new Integer(k*j));
return((Integer) serviceContext.getProperty("VALUE")).intValue();}
else{
serviceContext.setProperty("VALUE1", (Integer) serviceContext.getProperty("VALUE"));
int result = ((Integer) serviceContext.getProperty("VALUE1")).intValue()+((Integer) serviceContext.getProperty("VALUE")).intValue();
serviceContext.setProperty("VALUE", (Integer) result);
return result;}
}
}Step 2
Write the service descriptor(services.xml) for the above pojo.
<service name="Transportsessionservice" scope="transportsession">
<Description>
This is a sample service to test transport scope
</Description>
<messageReceivers>
<messageReceiver mep="http://www.w3.org/2006/01/wsdl/in-only"
class="org.apache.axis2.rpc.receivers.RPCInOnlyMessageReceiver"/>
<messageReceiver mep="http://www.w3.org/2006/01/wsdl/in-out"
class="org.apache.axis2.rpc.receivers.RPCMessageReceiver"/>
</messageReceivers>
<parameter name="ServiceClass" locked="false">
org.test.Transportsessionservice
</parameter>
</service>
You may notice that the sesion scope is defined in the services.xml, using scope=”transportsession” attribute.
Step3
Create the service archive and deploy it in Axis2. For this demonstration, I assume axis2 is run on top of apache Tomcat. You can bundle the services.xml and service implementation class in an Axis2 archive (.aar file) and copy it to tomcat_home\webapps\axis2\WEB-INF\services directory.
Now, go to Axis2 admin page (http://localhost:8080/axis2/services/listServices) and check whether your service is deployed correctly.
Step4
Now, the service deployment task is over. We can verify the session handling features by writing a client or RESTfully, as explained in the following steps:
Open your favorite browser and issue the following url.
http://localhost:8080/axis2/services/Transportsessionservice/multiply?k=3&j=5
You will get the following response:
<ns:multiplyResponse>
<ns:return>15</ns:return>
</ns:multiplyResponse>
Now, refresh your browser and check the response again.
<ns:multiplyResponse>
<ns:return>30</ns:return>
</ns:multiplyResponse>
Repeat above steps and you will see that the property we set in serviceContext is multiplied in each step. Here, we can observe that Axis2 uses http cookies to manage the session. Axis2 stores the service context and serviceGroup context in the transport session object, so that the service can access these contexts as long as the session exists. If you clear the browser cache and invoke the service, the value of the property will be changed to its original.
- Login or register to post comments
- Printer friendly version
- 802 reads










