HttpSessionListener Example – Monitoring

1. Overview

This tutorial will show how to register a javax.servlet.http.HttpSessionListener in the web.xml and track the number of active sessions in the web application using metrics.

2. The listener in the web.xml

The HTTP Session listener can be registered in the web.xml:

<web-app ...>
    <listener>
        <listener-class>org.baeldung.web.SessionListenerWithMetrics</listener-class>
    </listener>
</web-app>

Alternatively, in a Servlet 3 environment, we can use the @WebListener to register the listener as well.

3. The basic listener

The simple listener will keep track of the number of active sessions at all times:

import java.util.concurrent.atomic.AtomicInteger;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

public class SessionListenerWithMetrics implements HttpSessionListener {

    private final AtomicInteger activeSessions;

    public SessionListenerWithMetrics() {
        super();

        activeSessions = new AtomicInteger();
    }

    public int getTotalActiveSession() {
        return activeSessions.get();
    }

    public void sessionCreated(final HttpSessionEvent event) {
        activeSessions.incrementAndGet();
    }
    public void sessionDestroyed(final HttpSessionEvent event) {
        activeSessions.decrementAndGet();
    }
}

The session listener will be triggered when the session is created – sessionCreated:

HttpSession session = request.getSession();

And destroyed – sessionDestroyed:

session.invalidate();

This mechanism allows the current session count to be retrieved from the listener, but in order to have real-time monitoring and transparency, we need additional logic to actually retrieve the value and publish it.

This is where the metrics library comes in – it comes with several out of the box reporters that allow this metric to be published with very little effort.

4. The listener with metrics

So, instead of rolling out our own custom monitoring solution, we’ll leverage the metrics library; we’ll need to add it to our pom:

<dependency>
    <groupId>com.codahale.metrics</groupId>
    <artifactId>metrics-core</artifactId>
    <version>3.0.1</version>
</dependency>

With metrics core is available on the classpath, we can write the same HttpSessionListener using a Counter object:

import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
import com.codahale.metrics.Counter;
import com.codahale.metrics.MetricRegistry;

public class SessionListenerWithMetrics implements HttpSessionListener {

    private final Counter counterOfActiveSessions;

    public SessionListenerWithMetrics() {
        super();
        counterOfActiveSessions = MetricRegistrySingleton.metrics.counter("web.sessions.active.count");
    }

    public void sessionCreated(final HttpSessionEvent event) {
        counterOfActiveSessions.inc();
    }
    public void sessionDestroyed(final HttpSessionEvent event) {
        counterOfActiveSessions.dec();
    }
}

The MetricRegistry – the central registry of all application metrics – is simply referenced in an application wide static field:

import com.codahale.metrics.MetricRegistry;

public final class MetricRegistrySingleton {
    public static final MetricRegistry metrics = new MetricRegistry();
}

Publishing this metric and making it readily available to be monitored – for example to the standard logging system of the application – is straightforward:

Logger logger = LoggerFactory.getLogger("org.baeldung.monitoring");
Slf4jReporter reporter = Slf4jReporter.forRegistry(metrics).outputTo(logger).
  convertRatesTo(TimeUnit.SECONDS).convertDurationsTo(TimeUnit.MILLISECONDS).build();
reporter.start(5, TimeUnit.MINUTES);

5. Conclusion

This tutorial illustrated how to register a HttpSessionListener in the deployment descriptor of the web application and how to monitor the active number of sessions using two mechanisms. The first mechanism is a hand-rolled counter and the second is based on the mature metrics library.

The implementation can be found in the example github project – this is an Eclipse based project, so it should be easy to import and run as it is.

When the project runs locally, the login.html can be accessed on localhost.

I usually post about REST APIs and HTTP on Google+ - you can follow me there:

Get My 3 Spring eBooks
Learn how to properly build Web App with Spring (and how to prototype 90% of it very quickly)
×
Download the 3 eBooks - Build Your App "The Right Way"

, ,