views:

828

answers:

1

Hi there,

I manage a J2EE application which is deployed on JBoss 3.2.1. As part of my service to my client I want to provide reports which show the number of active sessions. I have implemented a simple class and a JSP page which retrieve the number of active sessions, but I have discovered a flaw: the process of checking the number of sessions itself creates a new session, and therefore inflates the number of sessions.

Here is the code for my class:

package com.hudsongates;

import javax.servlet.http.*;

public class SessionCount implements HttpSessionListener
{
  private static int numberOfSessions = 0;

  public void sessionCreated (HttpSessionEvent evt)
  {
    numberOfSessions++;
  }

  public void sessionDestroyed (HttpSessionEvent evt)
  {
    numberOfSessions--;
  }

  // here is our own method to return the number of current sessions
  public static int getNumberOfSessions()
  {
    return numberOfSessions;
  }

}

The JSP page looks like this:

<html>
<head>
<title>Active Sessions</title>
</head>
<body>
activeSessions=<%=com.hudsongates.SessionCount.getNumberOfSessions()%>
</body>
</html>

I would like to change the approach slightly so that instead of using a JSP page, I use a simple batch file. For example, I created a batch file called getSessions.bat:

REM Setup environment
call environment.bat

set LOG_PATH=%INSTALL_PATH%\log
set =%INSTALL_PATH%\lib\app.jar
set CLASSPATH=%INSTALL_PATH%\lib\app.jar

%JDK_HOME%\bin\java -cp "%CLASSPATH%" com.hudsongates.SessionCount.getNumberOfSessions() > %LOG_PATH%\test.log

The problem is that when I execute the batch file I get the following exception:

Exception in thread "main" java.lang.NoClassDefFoundError: com/hudsongates/SessionCount/getNumberOfSessions()

Do I need to add a "main" method to my class? If so, what would it do? Is there a better way of achieving my end goal of accurately counting the number of active sessions? Bear in mind that the session count needs to be written to a log file in the following format:

activeSessions=24

Thanks in advance,

Paul

+1  A: 

Two possibilities spring to mind: Your JSP can be configured to not create a new session. Stick this at the top of your JSP:

<%@ page session="false"%>

Alternatively, write a JMX MBean which implements JBoss's ServiceMBean interface, and deploy it as a JBos service. This MBean would query the session count, and would appear on JBoss's JMX console. You can use your web browser to monitor it from there, without interfering with your session count.

skaffman
Using <%@ page session="false"%>seems to have done the trick. Thanks!

related questions