Runtime components : Channels components : Key features : Tasks : Managing session
  
Managing session
This section describes how to configure and handle application sessions.
See
Implementing the callback APIs of the ISessionCallBack interface
Configuring session timeout handler
Marking the session expired
See also
Tasks
Implementing the callback APIs of the ISessionCallBack interface
In V8.2.0.2 or later, the ISessionCallBack interface is provided to help you monitor the session status. A group of callback APIs are available in the ISessionCallBack interface. You can implement these callback APIs according to your business requirements.
The ISessionCallBack interface is provided to monitor the session status changes when the session is created, removed, updated, retrieved, and timeout. You can use the APIs of the ISessionCallBack interface to capture the session status change and implement the APIs to perform response actions according to your requirements. For details about the ISessionCallBack interface, see Session status callback.
To implement the callback APIs of the ISessionCallBack interface, complete the following steps:
1 Create a class to implement the callback APIs of the ISessionCallBack interface, for example, you can create a SessionCallBack class.
2 In the SessionCallBack class, implement the following callback APIs according to your requirements:
Implementing the callback APIs of the ISessionCallBack interface
 
Callback APIs
Implementation description
onNewSession(String sessionId)
calls back this API after the framework successfully establishes a session.
The callback occurs at the end of com.ibm.btt.sm.CSSessionHandler.addSession().
Implementation example:
public void onNewSession(String sessionId) {
Context ctx=null;
try {
ctx = (Context) CSSessionHandler.getCurrentContextForSession(sessionId);
//After a new session is established, clean the environment;
CacheFactory.cleanStatus(ctx.getKeyedCollection());
} catch (Exception e) {
e.printStackTrace();
}

System.out.println("In Callback Adding Session id="+sessionId+"\tContext="+ctx);
}
onRemove(String sessionId)
calls back this API before the framework removes a session.
The callback occurs at the beginning of com.ibm.btt.sm.CSSessionHandler.removeSession().
Implementation example:
public void onRemove(String sessionId) {
System.out.println(" In Callback Removing Session id="+sessionId);
}
onIdle(String sessionId)
calls back this API when the session idle threshold is reached. You can specify the session idle threshold in the services.xml file.
The callback occurs in com.ibm.btt.cs.servlet.CSServer.checkExpiredSessions().
Implementation example:
public void onIdle(String sessionId) {
SessionEntry se=null;
try {
se = CSSessionHandler.getSession(sessionId);
if(!se.getIdle()){ //only set idle for the first time
se.setIdle(true);
// System.out.println("Set session mode to idle In Callback Idle Session id="+sessionId);
//TODO: This is an intentional marker, you can create your logic here
// se.setCurrentContext(null);
//END TODO
}
} catch (BTTSMException e) {
// e.printStackTrace();
}
}
onTimeout(String sessionId)
calls back this API when the session timeout threshold is reached. You can specify the session timeout threshold in the services.xml file.
The callback occurs in com.ibm.btt.cs.servlet.CSServer.checkExpiredSessions().
Implementation example:
public void onTimeout(String sessionId) {
System.out.println(" In Callback Timeout Session id="+sessionId);
}
onRetrieveIdleContext(String sessionId)
calls back this API when the framework checks whether the processor is timeout.
The callback occurs in com.ibm.btt.sm.SessionEntry.getCurrentContext().
Implementation example:
public void onRetrieveIdleContext(String sessionId) {
Context ctx = null;

//TODO: This is an intentional marker, retrieve context from your remote cache server
// Context ctx = cacheclient.get(machineID + sessionid) etc
//END TODO
//Then update this part into session table
SessionEntry se=null;
try {
se = CSSessionHandler.getSession(sessionId);
se.setIdle(false);
// System.out.println("reset session mode to nonidle In Callback Idle Session id="+sessionId);
//TODO: This is an intentional marker, you can remove your session context here by using the following code
// se.setCurrentContext(ctx);
//END TODO
} catch (BTTSMException e) {
e.printStackTrace();
}
}
 
Note onRetrieveIdleContext is called only when the idle status of the session context is true.
Note Each time when the session context is retrieved, the session status is changed from idle to active, which leads to repetitive status callback. To prevent calling back session context again, add the following codes in the handleCSProcessorInactivityEvent function. You can find this function in the implemented APIs in the CSProcessorInactivityListener interface.
SessionEntry se = CSSessionHandler.getSession(sessionId);
boolean idle = se.getIdle();
if(idle)
se.setIdle(false);
//TODO: After the session traversal is completed, set the session status back to idle by using the following code
//se.setIdle(idle);
//END TODO
onChange(String sessionId)
calls back this API to get the change list, after all pages are rendered and each session response ends.
The callback occurs at the end of com.ibm.btt.cs.html.HtmlPresentationHandler.processReply().
Implementation example:
public void onChange(String sessionId) {
try{
Context ctx = (Context) CSSessionHandler.getCurrentContextForSession(sessionId);
List changes = CacheFactory.generateChangesAndClearStatus(ctx.getKeyedCollection());

System.out.println(" In Callback Updating Session id="+sessionId+"\t changes="+changes + "\n changed size="+ changes.size());
}catch(Exception e){
e.printStackTrace();
}
}
onGenerateSessionId(String orginal)
Before you use this API, in the channelHandlers -> html kColl of the btt.xmlfile, set the value of the cookies field to false. The following codes show an example.
<kColl id="channelHandlers">
......
<kColl id="html">
......
<field id="cookies" value="false" />
This API is called when needs to confirm a session ID with the client. Use onGenerateSessionId with caution because this API overrides the default behavior of and .
The callback occurs in com.ibm.btt.http.JavaEstablishSessionRequest.preProcessRequest().
Implementation example:
public String onGenerateSessionId (String orginal){
return StringUtils.contactString("test",orginal);
}
Note After onGenerateSessionId is called, the old default session ID is replaced with a new session ID. Do not retrieve the session context by using the HttpSession ID.
onRetrieveSessionById(String sessionId)
This API is called before processes the session ID.
The callback occurs in com.ibm.btt.sm.CSSessionHandler.getSession().
Implementation example:
public void onRetrieveSessionById(String sessionId) {
SessionEntry session = (SessionEntry) CSSessionHandler.getSession(sessionId);
System.out.println(" The type of current session"+ session.getType());
}
onTimeStampChange(String sessionId)
calls back this API when the time stamp is changed.
The callback occurs in com.ibm.btt.sm.SessionEntry.setTimeStamp().
Implementation example:
public void onTimeStampChange(String sessionId) {
// TODO: you can create your logi when detecting the timestamp changes
System.out.println("Timestamp changed for session="+sessionId);
// END TODO
}
3 Configure the settings kColl in the btt.xml file.
Set the value of the SessionMode field to rewriting.
Add the following sessionCallBack field after the SessionMode field.
<field id="sessionCallBack" value="com.ibm.btt.cache.SessionCallBack" />
The following codes show an example:
<kColl id="settings">
<field id="SessionMode" value="rewriting" />
<field id="sessionCallBack" value="com.ibm.btt.cache.SessionCallBack" />
See also
Managing session
Configuring session timeout handler
UNICOM® Digital Transformation Toolkit (UDTT™) provides an HttpSessionListener to destroy the session context and the HttpSession object. You must configure it in the listener section of the web deployment descriptor (web.xml):
<listener>
  <listener-class>com.ibm.btt.sm.TimeoutHandler</listener-class>
</listener>
See also
Managing session
Marking the session expired
UNICOM® Digital Transformation Toolkit (UDTT™) provides a utility method in the com.ibm.btt.sm.CSSessionHandler class:
public static void markSessionExpired(String sessionId) throws BTTSMException
After calling this method, the session will be marked as expired and the session will be removed at the end of operation.
You need to call this method in the logoff operation. Then the session context will be cleaned and the http session object will be destroyed after you log off the application.
Sample code
public class LogoutOpStep extends BTTServerOperation {
  public void execute() throws Exception {
    System.out.println("====== LogoutOpStep Start =====");
    // do some clean up, after that mark session expired     HttpSession session= WebChannelContext.getHttpServletRequest().getSession();
    String sessionId=session.getId();
    CSSessionHandler.markSessionExpired(sessionId);
    System.out.println("====== LogoutOpStep End =====");
  }
}
See also
Managing session