Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.


  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • DownloadDownload
  • PrintPrint
Share this Page URL
Help

Chapter 4. RPC-Style Services > Deploying with Session-Level Scope

4.6. Deploying with Session-Level Scope

Apache SOAP provides for session management by passing cookies via the HTTP headers. Earlier in this chapter, we saw an HTTP response from the server that included HTTP header entries called Set-Cookie and Set-Cookie2. The client application uses these cookies if it needs to make subsequent method invocations against the same session as the original request. The Apache SOAP API uses a simple technique for handling this. org.apache.soap.transport.http.SOAPHTTPConnection includes a method called setMaintainSession( ) that takes a single boolean parameter. This parameter, when set to true, tells the connection object to maintain the current session. The connection object implements this by keeping track of the cookies and sending them back to the server when the next method invocation takes place.

Go ahead and edit the deployment descriptor, setting the scope attribute to Session. Now run the client program again. You'll get the following output:

Result is 3

This output proves that the same instance of the service object was used for all of the method invocations. If you run it again you'll get the same result because a new instance of the session object is created on the first call to doSomething( ), and each subsequent method call uses the same service object. Because we're using the same instance of org.apache.soap.rpc.Call for every service call, we're also using the same instance of org.apache.soap.transport.http.SOAPHTTPConnection. You've probably guessed that the default behavior of org.apache.soap.transport.http.SOAPHTTPConnection is to maintain the session.

A common use for session-level scope is a shopping cart, where you want to keep track of the session between service method invocations. However, there may be times when you do not want to use the same session for all of your method calls, even though the service is deployed using session scope. In the next example, the SOAPHTTPConnection object is retrieved from the Call object after the first method invocation. Then a call to the connection's setMaintainSession( ) method is made with a parameter of false. This stops the connection from using the same session on subsequent service method calls.

package javasoap.book.ch4;
import java.net.*;
import org.apache.soap.*;
import org.apache.soap.rpc.*;
import org.apache.soap.transport.http.*;
public class SessionApp {
   public static void main(String[] args) 
      throws Exception {
      
      URL url = 
        new URL(
          "http://georgetown:8080/soap/servlet/rpcrouter");
    
      Call call = new Call(  );
      call.setTargetObjectURI("urn:CallCounterService");
      try {
         call.setMethodName("doSomething");
         Response resp = call.invoke(url, "");
         SOAPHTTPConnection st = 
          (SOAPHTTPConnection)call.getSOAPTransport(  );
         st.setMaintainSession(false);
         resp = call.invoke(url, "");
         st.setMaintainSession(true);
         resp = call.invoke(url, "");
         call.setMethodName("getCount");
         resp = call.invoke(url, "");
         Parameter ret = resp.getReturnValue(  );
         Object value = ret.getValue(  );
         System.out.println("Result is " + value);
      }
      catch (SOAPException e) {
         System.err.println("Caught SOAPException (" +
                         e.getFaultCode(  ) + "): " +
                         e.getMessage(  ));
      }
   }
} 

					  

Before the third call to doSomething( ), we call setMaintainSession( ) again with a parameter of true. Therefore, after the third call to doSomething( ), the SOAPHTTPConnection object will keep track of the session cookies. Then, when we call getCount( ), those cookies will be sent back to the server, ensuring that the same session object is used again. You should get the following output when you run the SessionApp application:

Result is 1

If you want to maintain a single session across multiple calls, make sure that you establish the session before the first call that should be part of that session. However, programming this way is a little sloppy, at least for my taste. I would prefer to create a new instance of the SOAPHTTPConnection and use it when I begin making calls using a single session. Of course, these considerations apply only to services deployed using session scope. If you'd like to give this a try, change the code within the try block to look like this:

call.setMethodName("doSomething");
Response resp = call.invoke(url, "");
resp = call.invoke(url, "");
SOAPHTTPConnection st = new SOAPHTTPConnection(  );
call.setSOAPTransport(st);
resp = call.invoke(url, "");
call.setMethodName("getCount");
resp = call.invoke(url, "");
Parameter ret = resp.getReturnValue(  );
Object value = ret.getValue(  );
System.out.println("Result is " + value);

The lifetime of a server session is implementation dependent. The server platform you are using, the way it's configured, and the available configuration options all play a part in how long a session lasts. In most cases, the server terminates a session after some period of idle time has passed. GLUE manages sessions automatically, and lets you set the session lifetime by calling electric.server.http.HTTP.setSessionTimeout( ). The parameter passed to this method is an integer that contains the number of seconds that the session can remain inactive before it is invalidated.