Handling session timeout

The toolkit uses the com.ibm.dse.cs.servlet.CSInactivityClientEvent to notify an application that a session has timed out. If WebSphere Application Server is detecting timed out sessions, it broadcasts an event that the CSServer service receives, which causes the CSServer to launch the CSInactivityClientEvent.

To perform the cleanup involved with session expiry, an application must register to listen for this event. In the Base Sample Application, the com.ibm.dse.samples.appl.StartServerServlet class provides an example of how to do this.

To handle the cleanup after being notified, the application must use a class that implements the com.ibm.dse.cs.servlet.CSInactivityClientListener interface, as in the following example:

public class StartServerServlet extends javax.servlet.http.HttpServlet 
    implements CSInactivityClientListener,CSProcessorInactivityListener{

This class must also register to listen to the event using the CSServer service, as follows:

((CSServer)context.getService("CSServer")).addCSInactivityClientListener(this);

To actually perform the cleanup, the class must contain a method that handles the CSInactivityClientEvent. The following is an example of one of these methods:

public void handleCSInactivityClientEvent(CSInactivityClientEvent event)
{
  // Get the ID of the expired session encapsulated in the event
  String sessionId = event.getSessionExpired();
  try
  {
    // If this session has underlying application sessions, clean up the   
    // application sessions here. Typical toolkit applications do not have 
    // any application sessions. This is a feature implemented by the application. 
    // If your application is not implementing it, do nothing.
    // Get the table with the stored processors.
    java.util.Hashtable procTable = HtmlProcessorManager.
        getProcessorInfoTable(sessionId);
    // Remove each entry from cache and from the table.
    java.util.Enumeration procKeyEnum = procTable.keys();
    while (procKeyEnum.hasMoreElements())
    {
      String processorId = (String) procKeyEnum.nextElement();
      String procName = ((HtmlProcessorInfo) procTable.get(processorId)).
          getProcessorName();
      // Remove the processor instance from the cache.
      ProcessorExternalizer.getFromCache(procName);
      // Remove the processor instance from the processors table and registry.
      HtmlProcessorManager.getUniqueHTMLInstance().
      removeProcessor(sessionId, processorId);
    }
    // Get the session context.
    Context ctx = Context.getCurrentContextForSession(sessionId);
    // If the session context has a service, terminate it here.
    // Remove the session from the session table.
    Context.removeSession(sessionId);
    // Unchain the session context from the context hierarchy.
    ctx.prune();
  }
  catch (Exception e)
  {
    // Perform any exception handling.
  }
}

Note that this method is just performing internally required toolkit processes. The application can use this method to stop other processes (services or application-specific processes) that are running in the session.