diff --git a/spring-security-mvc-custom/pom.xml b/spring-security-mvc-custom/pom.xml
index f41caec946..6e0391e1dd 100644
--- a/spring-security-mvc-custom/pom.xml
+++ b/spring-security-mvc-custom/pom.xml
@@ -92,6 +92,14 @@
runtime
+
+
+
+ com.codahale.metrics
+ metrics-core
+ 3.0.1
+
+
diff --git a/spring-security-mvc-custom/src/main/java/org/baeldung/monitoring/MetricRegistrySingleton.java b/spring-security-mvc-custom/src/main/java/org/baeldung/monitoring/MetricRegistrySingleton.java
new file mode 100644
index 0000000000..8720283f17
--- /dev/null
+++ b/spring-security-mvc-custom/src/main/java/org/baeldung/monitoring/MetricRegistrySingleton.java
@@ -0,0 +1,13 @@
+package org.baeldung.monitoring;
+
+import com.codahale.metrics.MetricRegistry;
+
+public final class MetricRegistrySingleton {
+
+ public static final MetricRegistry metrics = new MetricRegistry();
+
+ private MetricRegistrySingleton() {
+ throw new AssertionError();
+ }
+
+}
diff --git a/spring-security-mvc-custom/src/main/java/org/baeldung/web/SessionListenerWithMetrics.java b/spring-security-mvc-custom/src/main/java/org/baeldung/web/SessionListenerWithMetrics.java
index 7119b7023e..46bf2708f7 100644
--- a/spring-security-mvc-custom/src/main/java/org/baeldung/web/SessionListenerWithMetrics.java
+++ b/spring-security-mvc-custom/src/main/java/org/baeldung/web/SessionListenerWithMetrics.java
@@ -1,32 +1,43 @@
package org.baeldung.web;
+import java.util.concurrent.atomic.AtomicInteger;
+
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
+import org.baeldung.monitoring.MetricRegistrySingleton;
+
+import com.codahale.metrics.Counter;
+
public class SessionListenerWithMetrics implements HttpSessionListener {
- private static int totalActiveSessions;
+ private final AtomicInteger activeSessions;
+
+ private final Counter counterOfActiveSessions;
public SessionListenerWithMetrics() {
super();
+
+ activeSessions = new AtomicInteger();
+ counterOfActiveSessions = MetricRegistrySingleton.metrics.counter("web.sessions.active.count");
}
// API
- public static int getTotalActiveSession() {
- return totalActiveSessions;
+ public final int getTotalActiveSession() {
+ return activeSessions.get();
}
@Override
- public void sessionCreated(final HttpSessionEvent arg0) {
- totalActiveSessions++;
- System.out.println("sessionCreated - add one session into counter");
+ public final void sessionCreated(final HttpSessionEvent event) {
+ activeSessions.incrementAndGet();
+ counterOfActiveSessions.inc();
}
@Override
- public void sessionDestroyed(final HttpSessionEvent arg0) {
- totalActiveSessions--;
- System.out.println("sessionDestroyed - deduct one session from counter");
+ public final void sessionDestroyed(final HttpSessionEvent event) {
+ activeSessions.decrementAndGet();
+ counterOfActiveSessions.dec();
}
}
\ No newline at end of file