Presentation is loading. Please wait.

Presentation is loading. Please wait.

Apache Axis2 Sessions with Axis2. 2 Agenda Why do we need sessions? Types of sessions Sessions and their lifetime Deploying services in different session.

Similar presentations


Presentation on theme: "Apache Axis2 Sessions with Axis2. 2 Agenda Why do we need sessions? Types of sessions Sessions and their lifetime Deploying services in different session."— Presentation transcript:

1 Apache Axis2 Sessions with Axis2

2 2 Agenda Why do we need sessions? Types of sessions Sessions and their lifetime Deploying services in different session scopes When a session start and ends Getting access to contexts Session aware clients Exercises

3 3 Why Do We Need Sessions? Web services were originally completely stateless. But now the trend has changed..... Can we write advanced Web services without using sessions?  Yes, but it can be simpler and more efficient to use a session

4 4 Using ConfigurationContext ConfigurationContext represents the Axis2 runtime. This can be used to maintain state with some limitations. public class MyService { public void serviceMethod(String x) { MessageContext mCtx = MessageContext.getCurrentMessageContext(); ConfigurationContext cCtx = mCtx.getConfigurationContext(); cCtx.setProperty(“name”, x); }

5 5 Using ConfigurationContext... Each invocation of serviceMethod() overrides the property Property is globally visible!  For every client  For every service  For every service group We surely need more control!

6 6 Types of Session in Axis2 There are four types of sessions in Axis2  Request (request)  Transport (transportsession)  SOAPSession (soapsession)  Application (application)

7 7 Request Default session scope of a service Lifetime of the session is equal to message processing time Time from TransportReceiver to TransportSender Not really a “session” in Servlet terminology You can store session information in OperationContext For each and every request a new service implementation object instance will be created

8 8 Request Cont... public class MyHandler extends AbstractHandler { public InvocationResponse invoke(mCtx) throws... { OperationContext oCtx = mCtx.getOperationContext(); oCtx.setProperty(“pName”, value); } public class MyService { public void serviceMethod(String x) { MessageContext mCtx = MessageContext.getCurrentMessageContext(); OperationContext oCtx = mCtx.getOperationContext(); Type value = (Type) oCtx.getProperty(“pName”); }

9 9 Transport Transport dependent session management technique In the case of HTTP, it is HTTP cookies Lifetime of the session is the lifetime of the actual transport level session  As long as the client sends the cookie ID, client will be in the same session even if the client talks to a different service of the same service group. To store session data, use ServiceContext or ServiceGroupContext Client data is isolated!

10 10 Transport Cont... Need to explicitly specify Need to change axis2.xml too (need to add following parameter).............. true

11 11 SOAPSession Like a transport session, except at the SOAP level  Uses WS-Addressing reference parameters instead of cookies Axis2 manages sessions across all the services in a ServiceGroup The client needs to send the ServiceGroupId And engage addressing in both client and service

12 12 SOAPSession Cont... There will be many SOAP Sessions for a given ServiceGroup at any given time  One per client A new service instance will be created for each session You need to specify the scope explicitly..............

13 13 Application Session’s lifetime is equal to the application server's lifetime There will be only one service instance throughout the lifetime of the system No need to send any session cookies or WS-A headers to use the session Need to specify explicitly as:..............

14 14 When Session Start and Ends Immediately before a session starts, Axis2 calls the following method in your service implementation class And after session finishes it calls public void init(ServiceContext serviceContext) { } public void destroy(ServiceContext serviceContext) { }

15 15 Session Aware Client How to enable ServiceClient to work as a session aware client? When you set the above flag, the Axis2 service client will send necessary session data (HTTP cookies or ServiceGroupId) ServiceClient sc = new ServiceClient(); Options opts = new Options(); opts.setManageSession(true); sc.setOptions(opts);

16 16 Summary Web services are stateless by default Sessions make it simpler and more efficient to write complex Web services Types of Sessions  Request, Transport, SOAPSession, Application ServiceClient can be enabled to work as a session aware client

17 17 Exercises Look at the resources section for the sample code of a stateful shopping cart service. Deploy the service with SOAPSession enabled. Run two instances of the client application almost at the same time. Look at the sample code to learn the details. A demo is provided to assist you with the above exercise


Download ppt "Apache Axis2 Sessions with Axis2. 2 Agenda Why do we need sessions? Types of sessions Sessions and their lifetime Deploying services in different session."

Similar presentations


Ads by Google