4 Replies Latest reply: Sep 17, 2009 6:11 AM by 791266 RSS

    Monitor the number of active sessions (for web app deployed on JBoss 4.0.5)

    843798
      Hi Gurus,

      Firstly please forgive the somewhat lengthy posting, but I want to make sure I don't leave out any pertinent information.

      I have a J2EE web application deployed on JBoss (let's say it's called MyApp), and as part of my monitoring I want to know how many active sessions there are at any one time (ie: how many users are currently logged in and have a session that hasn't timed out).

      Initially I had hoped that I could use some monitoring software (eg: Hyperic, AdventNet App Manager, etc) to simply monitor JBoss, but so far none of the software I have evaluated will actually monitor active sessions for a web app.

      On the bright side, one of the monitoring applications I have been looking at (AdventNet App Manager) allows me to create a custom monitor which will:

      a) execute a script (eg: generate_session_count.bat)
      b) read the output in a log file

      So I thought maybe I can create a custom class which I can deploy as part of the web app, and it will get the session count and write the number to a log file.

      I did some searching and found the following code ([click here|http://www.java.happycodings.com/Java_Servlets/code10.html]) which is supposed to count active sessions:

      *--- JSP page for testing (session_count.jsp) --*
      <%
      Integer icount = (Integer)session.getAttribute("count");
      out.println("sessions="+icount);
      %>
      *--- Servlet that handles session count (CounterListener.java) --*
      import javax.servlet.*;
      import javax.servlet.http.*;
      
      public final class CounterListener implements HttpSessionListener
          {
           private int count = 1;
           private ServletContext context = null;
          
           public synchronized void sessionCreated(HttpSessionEvent se)
               {
               count++;
               log("sessionCreated("+se.getSession().getId()+") count="+count);
               se.getSession().setAttribute("count",new Integer(count));
           }
          
           public synchronized void sessionDestroyed(HttpSessionEvent se)
               {
               count--;
               log("sessionDestroyed("+se.getSession().getId()+") count="+count);
               se.getSession().setAttribute("count",new Integer(count));
           }
          
           public int getCount()
               {
               return this.count;
           }
          
           public void addCount()
               {
               count++;
           }
          
           private void log(String message)
               {
               if (context != null)
               context.log("SessionListener: " + message);
               else
               System.out.println("SessionListener: " + message);
           }
          
      }//close class CounterListener
      *--- web.xml file modification --*
      <?xml version="1.0" encoding="ISO-8859-1"?>
      
      <!DOCTYPE web-app
      PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
      "http://java.sun.com/dtd/web-app_2_3.dtd">
      
      <web-app>
      <display-name>My Application</display-name>
      
      <!-- add this line of code -->
      <listener>
      <listener-class>CounterListener</listener-class>
      </listener>
      
      <description>
      Welcome to MyApp
      </description>
      
      </web-app>
      It took me a while and some more searching to figure out how to use/deploy the above code, and I was finally able to start JBoss, and when I requested the folowing URL:

      http://localhost/MyApp/session_count.jsp

      the response was:

      sessions=n

      where n is some number.

      Initially I was very excited, but after doing some testing I found that the number of sessions being returned was not correct. I found that even if no-one was logged in to the app, the session count was 2, and when I launched a few separate browsers and logged in, the session count was wrong (consistently seemed to be double the actual number of users logged in - is that some quirky characteristic of HttpSessionListener which reports two sessions for each browser?).

      Finally we get to the question part of my post. :)

      1. Why is the session count wrong? Can someone suggest some alternative code which will report the correct number of sessions?
      2. How do I get the class to output the session count to a file which can be read by the monitoring software?
      3. Is there a better way? (This is a very open-ended question and I am open to any suggestions)

      Cheers,

      Paul