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 login when detecting the timestamp changes System.out.println("Timestamp changed for session="+sessionId); // END TODO } |