Question : how to kill sessions via webapp when using mod_jk cluster

Our webapp currently runs on Tomcat 5.5 w/ Apache webserver.    Our sysadmin has setup two tomcat instances (A & B) to enable us to do updates to one instance while the other takes over all the traffic.

We have a custom developed sessions admin screen which allows us to view current sessions, the accounts linked to those sessions and do actions such as kill the session if needed.  This works simply enough when there is only one instance of Tomcat, hence only one application-level hash of sessions.

We want to write a new version of the admin screen which will show all sessions from both instances of Tomcat and also allow us to kill a particular session, regardless of which Tomcat instance it is running.  How does one do this?

Note: While references to sessions are currently stored within a app-scoped variable we will be storing the session ID's in a database.  So we will have a single place to enumerate all existing sessions regardless of Tomcat instance.  The problem is how to know which Tomcat instance a particular session is running upon and how to make the call to kill the session on that instance.

Answer : how to kill sessions via webapp when using mod_jk cluster

>We want to write a new version of the admin screen which will show all sessions from both instances of Tomcat
Will there be multiple apps (contexts) in each Tomcat ? Do you want to include all sessions in all apps ?  If yes, then you can set up your context with the attribute  crossContext="true"  Look at  
http://tomcat.apache.org/tomcat-6.0-doc/config/context.html    
The other instance will have to make a request ( using a java.net.HttpURLConnection object or a JSTL import tag) to a servlet or a JSP in the first instance.

>How do you identify (from within Java) which Tomcat instance a session is running.  
By java you mean to say some utility class or javabean ? Look at my last comment at
http://www.experts-exchange.com/Programming/Languages/Java/Q_24490440.html  

I might have time tomorrow to help you with code.  I will online early morning and in the evening.  Below is a start.
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
public class SessionListener implements HttpSessionListener {
  HashMap sessionMap = null;
  ServletContext application = null;
  public void sessionCreated(HttpSessionEvent event) {
             HttpSession session = event.getSession();
             if(application == null)application = session.getServletContext();
             sessionMap = (HashMap)application.getAttribute("sessionMap");
             if(sessionMap == null){
                        application.setAttribute("sessionMap",new HashMap());  
                        sessionMap = (HashMap)application.getAttribute("sessionMap");
             }
             sessionMap.put(session.getId(),"homeInstance"); 
             //the other remote instance will a make request to a JSP that will put entry.                    
  }
  public void sessionDestroyed(HttpSessionEvent event) {
                                       sessionMap.remove(event.getSession().getId());
  }
}
Random Solutions  
 
programming4us programming4us