Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

how to kill sessions via webapp when using mod_jk cluster

Posted on 2009-07-10
4
Medium Priority
?
1,437 Views
Last Modified: 2013-12-02
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.
0
Comment
Question by:stevelopez
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 

Author Comment

by:stevelopez
ID: 24827829
This may be more difficult than I expected;  raising the point value...
0
 
LVL 28

Expert Comment

by:rrz
ID: 24828846
>We have a custom developed sessions admin screen
Is this a java program within the Tomcat ?  
>we will be storing the session ID's in a database.  
I guess you could also store the name of the instance as well.
You could use a Map. Something like
  map.put(sessionId, "A"); or  map.put(sessionId, "B");
You could use a HttpSessionListener  in each instance of Tomcat that could(in its sessionCreated  method) add each session id to an application-scoped set  and send the id and its instance name to the database.
Each Tomcat could have a servlet that could invalidate a session when it receives a request to do so  from the admin.    
If need help with the code, then just ask us here.

0
 

Author Comment

by:stevelopez
ID: 24829029
yes, the current admin feature is written as servlet/JSP and runs within Tomcat.

How do you identify (from within Java) which Tomcat instance a session is running. If that can be done than I think your suggestion would work, once coupled with a listener that would respond to some admin-initiated trigger to kill the session.
0
 
LVL 28

Accepted Solution

by:
rrz earned 1500 total points
ID: 24829275
>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.
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());
  }
}

Open in new window

0

Featured Post

Application Discovery Service in AWS

In the era of the cloud, customers migrating away from their existing on-premise infrastructure. This requires lots of planning, strategies, and effort to identify their existing resources and determine how best to migrate.  Datacenter migrations happen in four phases -

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Most of the developers using Tomcat find it easy to configure the datasource in Server.xml and use the JNDI name in the code to get the connection.  So the default connection pool using DBCP (or any other framework) is made available and the life go…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses

715 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question