From 17981b4347eff1c98505f6b602e29c6507380b62 Mon Sep 17 00:00:00 2001 From: Nikita Koksharov Date: Fri, 3 Nov 2023 12:50:31 +0300 Subject: [PATCH] Fixed - HttpSessionListener#sessionDestroyed() method isn't called if Tomcat Session deleted by the node which didn't create it #5408 --- .../tomcat/RedissonSessionManager.java | 19 ++++---- .../tomcat/RedissonSessionManagerTest.java | 45 +++++++++++++++---- .../org/redisson/tomcat/TomcatServer.java | 16 +++++-- .../tomcat/RedissonSessionManager.java | 21 ++++----- .../tomcat/RedissonSessionManager.java | 19 ++++---- .../tomcat/RedissonSessionManager.java | 19 ++++---- 6 files changed, 89 insertions(+), 50 deletions(-) diff --git a/redisson-tomcat/redisson-tomcat-10/src/main/java/org/redisson/tomcat/RedissonSessionManager.java b/redisson-tomcat/redisson-tomcat-10/src/main/java/org/redisson/tomcat/RedissonSessionManager.java index c8fe006a5..db738b44c 100644 --- a/redisson-tomcat/redisson-tomcat-10/src/main/java/org/redisson/tomcat/RedissonSessionManager.java +++ b/redisson-tomcat/redisson-tomcat-10/src/main/java/org/redisson/tomcat/RedissonSessionManager.java @@ -134,14 +134,6 @@ public class RedissonSessionManager extends ManagerBase { if (broadcastSessionEvents) { getTopic().publish(new SessionCreatedMessage(getNodeId(), session.getId())); - session.addSessionListener(new SessionListener() { - @Override - public void sessionEvent(SessionEvent event) { - if (event.getType().equals(Session.SESSION_DESTROYED_EVENT)) { - getTopic().publish(new SessionDestroyedMessage(getNodeId(), session.getId())); - } - } - }); } return session; } @@ -208,7 +200,16 @@ public class RedissonSessionManager extends ManagerBase { @Override public Session createEmptySession() { - return new RedissonSession(this, readMode, updateMode, broadcastSessionEvents, this.broadcastSessionUpdates); + Session session = new RedissonSession(this, readMode, updateMode, broadcastSessionEvents, this.broadcastSessionUpdates); + + if (broadcastSessionEvents) { + session.addSessionListener(event -> { + if (event.getType().equals(Session.SESSION_DESTROYED_EVENT)) { + getTopic().publish(new SessionDestroyedMessage(getNodeId(), session.getId())); + } + }); + } + return session; } @Override diff --git a/redisson-tomcat/redisson-tomcat-10/src/test/java/org/redisson/tomcat/RedissonSessionManagerTest.java b/redisson-tomcat/redisson-tomcat-10/src/test/java/org/redisson/tomcat/RedissonSessionManagerTest.java index 5e34d55ee..caa7938ba 100644 --- a/redisson-tomcat/redisson-tomcat-10/src/test/java/org/redisson/tomcat/RedissonSessionManagerTest.java +++ b/redisson-tomcat/redisson-tomcat-10/src/test/java/org/redisson/tomcat/RedissonSessionManagerTest.java @@ -84,7 +84,7 @@ public class RedissonSessionManagerTest { TomcatServer server3 = new TomcatServer("myapp", 8082, "src/test/"); server3.start(); - invalidate(executor); + invalidate(8080, executor); Thread.sleep(500); @@ -153,6 +153,34 @@ public class RedissonSessionManagerTest { } + @ParameterizedTest + @MethodSource("data") + public void testInvalidateListener(String contextName) throws Exception { + prepare(contextName); + TomcatServer server1 = new TomcatServer("myapp", 8080, "src/test/"); + server1.start(); + TomcatServer server2 = new TomcatServer("myapp", 8081, "src/test/"); + server2.start(); + + server1.getSessionManager().getRedisson().getKeys().flushall(); + + Executor executor = Executor.newInstance(); + BasicCookieStore cookieStore = new BasicCookieStore(); + executor.use(cookieStore); + + write(8080, executor, "test", "1234"); + read(8081, executor, "test", "1234"); + + TestHttpSessionListener.DESTROYED_INVOCATION_COUNTER = 0; + invalidate(8081, executor); + + Executor.closeIdleConnections(); + server1.stop(); + server2.stop(); + + Assertions.assertEquals(2, TestHttpSessionListener.DESTROYED_INVOCATION_COUNTER); + } + @ParameterizedTest @MethodSource("data") public void testExpiration(String contextName) throws Exception { @@ -276,22 +304,21 @@ public class RedissonSessionManagerTest { @MethodSource("data") public void testInvalidate(String contextName) throws Exception { prepare(contextName); - File f = Paths.get("").toAbsolutePath().resolve("src/test/webapp/WEB-INF/redisson.yaml").toFile(); - Config config = Config.fromYAML(f); - RedissonClient r = Redisson.create(config); - r.getKeys().flushall(); // start the server at http://localhost:8080/myapp TomcatServer server = new TomcatServer("myapp", 8080, "src/test/"); server.start(); + RedissonClient r = server.getSessionManager().getRedisson(); + r.getKeys().flushall(); + Executor executor = Executor.newInstance(); BasicCookieStore cookieStore = new BasicCookieStore(); executor.use(cookieStore); write(8080, executor, "test", "1234"); Cookie cookie = cookieStore.getCookies().get(0); - invalidate(executor); + invalidate(8080, executor); Executor.closeIdleConnections(); @@ -300,7 +327,7 @@ public class RedissonSessionManagerTest { cookieStore.addCookie(cookie); executor.use(cookieStore); read(8080, executor, "test", "null"); - invalidate(executor); + invalidate(8080, executor); Executor.closeIdleConnections(); server.stop(); @@ -327,8 +354,8 @@ public class RedissonSessionManagerTest { Assertions.assertEquals(value, response); } - private void invalidate(Executor executor) throws IOException { - String url = "http://localhost:8080/myapp/invalidate"; + private void invalidate(int port, Executor executor) throws IOException { + String url = "http://localhost:" + port + "/myapp/invalidate"; String response = executor.execute(Request.Get(url)).returnContent().asString(); Assertions.assertEquals("OK", response); } diff --git a/redisson-tomcat/redisson-tomcat-10/src/test/java/org/redisson/tomcat/TomcatServer.java b/redisson-tomcat/redisson-tomcat-10/src/test/java/org/redisson/tomcat/TomcatServer.java index f789cb054..4f4b297eb 100644 --- a/redisson-tomcat/redisson-tomcat-10/src/test/java/org/redisson/tomcat/TomcatServer.java +++ b/redisson-tomcat/redisson-tomcat-10/src/test/java/org/redisson/tomcat/TomcatServer.java @@ -1,13 +1,14 @@ package org.redisson.tomcat; -import java.net.MalformedURLException; - import jakarta.servlet.ServletException; +import org.apache.catalina.Context; import org.apache.catalina.LifecycleException; import org.apache.catalina.startup.Tomcat; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import java.net.MalformedURLException; + public class TomcatServer { private Tomcat tomcat = new Tomcat(); @@ -17,6 +18,9 @@ public class TomcatServer { private static final Logger LOG = LoggerFactory.getLogger(TomcatServer.class); private static final boolean isInfo = LOG.isInfoEnabled(); + private final Context c; + private RedissonSessionManager sessionManager; + public TomcatServer(String contextPath, int port, String appBase) throws MalformedURLException, ServletException { if(contextPath == null || appBase == null || appBase.length() == 0) { throw new IllegalArgumentException("Context path or appbase should not be null"); @@ -29,16 +33,17 @@ public class TomcatServer { tomcat.setPort(port); tomcat.getHost().setAppBase("."); - tomcat.addWebapp(contextPath, appBase + "/webapp"); + c = tomcat.addWebapp(contextPath, appBase + "/webapp"); } /** * Start the tomcat embedded server * @throws InterruptedException */ - public void start() throws LifecycleException, InterruptedException { + public void start() throws LifecycleException { tomcat.start(); tomcat.getConnector(); + sessionManager = (RedissonSessionManager) c.getManager(); isRunning = true; } @@ -63,4 +68,7 @@ public class TomcatServer { return isRunning; } + public RedissonSessionManager getSessionManager() { + return sessionManager; + } } \ No newline at end of file diff --git a/redisson-tomcat/redisson-tomcat-7/src/main/java/org/redisson/tomcat/RedissonSessionManager.java b/redisson-tomcat/redisson-tomcat-7/src/main/java/org/redisson/tomcat/RedissonSessionManager.java index b8090cc5d..9f61b164c 100644 --- a/redisson-tomcat/redisson-tomcat-7/src/main/java/org/redisson/tomcat/RedissonSessionManager.java +++ b/redisson-tomcat/redisson-tomcat-7/src/main/java/org/redisson/tomcat/RedissonSessionManager.java @@ -134,14 +134,6 @@ public class RedissonSessionManager extends ManagerBase { if (broadcastSessionEvents) { getTopic().publish(new SessionCreatedMessage(getNodeId(), session.getId())); - session.addSessionListener(new SessionListener() { - @Override - public void sessionEvent(SessionEvent event) { - if (event.getType().equals(Session.SESSION_DESTROYED_EVENT)) { - getTopic().publish(new SessionDestroyedMessage(getNodeId(), session.getId())); - } - } - }); } return session; } @@ -205,10 +197,19 @@ public class RedissonSessionManager extends ManagerBase { return result; } - + @Override public Session createEmptySession() { - return new RedissonSession(this, readMode, updateMode, broadcastSessionEvents, this.broadcastSessionUpdates); + Session session = new RedissonSession(this, readMode, updateMode, broadcastSessionEvents, this.broadcastSessionUpdates); + + if (broadcastSessionEvents) { + session.addSessionListener(event -> { + if (event.getType().equals(Session.SESSION_DESTROYED_EVENT)) { + getTopic().publish(new SessionDestroyedMessage(getNodeId(), session.getId())); + } + }); + } + return session; } @Override diff --git a/redisson-tomcat/redisson-tomcat-8/src/main/java/org/redisson/tomcat/RedissonSessionManager.java b/redisson-tomcat/redisson-tomcat-8/src/main/java/org/redisson/tomcat/RedissonSessionManager.java index 73681ee6d..7be4411d6 100644 --- a/redisson-tomcat/redisson-tomcat-8/src/main/java/org/redisson/tomcat/RedissonSessionManager.java +++ b/redisson-tomcat/redisson-tomcat-8/src/main/java/org/redisson/tomcat/RedissonSessionManager.java @@ -134,14 +134,6 @@ public class RedissonSessionManager extends ManagerBase { if (broadcastSessionEvents) { getTopic().publish(new SessionCreatedMessage(getNodeId(), session.getId())); - session.addSessionListener(new SessionListener() { - @Override - public void sessionEvent(SessionEvent event) { - if (event.getType().equals(Session.SESSION_DESTROYED_EVENT)) { - getTopic().publish(new SessionDestroyedMessage(getNodeId(), session.getId())); - } - } - }); } return session; } @@ -208,7 +200,16 @@ public class RedissonSessionManager extends ManagerBase { @Override public Session createEmptySession() { - return new RedissonSession(this, readMode, updateMode, broadcastSessionEvents, this.broadcastSessionUpdates); + Session session = new RedissonSession(this, readMode, updateMode, broadcastSessionEvents, this.broadcastSessionUpdates); + + if (broadcastSessionEvents) { + session.addSessionListener(event -> { + if (event.getType().equals(Session.SESSION_DESTROYED_EVENT)) { + getTopic().publish(new SessionDestroyedMessage(getNodeId(), session.getId())); + } + }); + } + return session; } @Override diff --git a/redisson-tomcat/redisson-tomcat-9/src/main/java/org/redisson/tomcat/RedissonSessionManager.java b/redisson-tomcat/redisson-tomcat-9/src/main/java/org/redisson/tomcat/RedissonSessionManager.java index 73681ee6d..7be4411d6 100644 --- a/redisson-tomcat/redisson-tomcat-9/src/main/java/org/redisson/tomcat/RedissonSessionManager.java +++ b/redisson-tomcat/redisson-tomcat-9/src/main/java/org/redisson/tomcat/RedissonSessionManager.java @@ -134,14 +134,6 @@ public class RedissonSessionManager extends ManagerBase { if (broadcastSessionEvents) { getTopic().publish(new SessionCreatedMessage(getNodeId(), session.getId())); - session.addSessionListener(new SessionListener() { - @Override - public void sessionEvent(SessionEvent event) { - if (event.getType().equals(Session.SESSION_DESTROYED_EVENT)) { - getTopic().publish(new SessionDestroyedMessage(getNodeId(), session.getId())); - } - } - }); } return session; } @@ -208,7 +200,16 @@ public class RedissonSessionManager extends ManagerBase { @Override public Session createEmptySession() { - return new RedissonSession(this, readMode, updateMode, broadcastSessionEvents, this.broadcastSessionUpdates); + Session session = new RedissonSession(this, readMode, updateMode, broadcastSessionEvents, this.broadcastSessionUpdates); + + if (broadcastSessionEvents) { + session.addSessionListener(event -> { + if (event.getType().equals(Session.SESSION_DESTROYED_EVENT)) { + getTopic().publish(new SessionDestroyedMessage(getNodeId(), session.getId())); + } + }); + } + return session; } @Override