Fixed - HttpSessionListener#sessionDestroyed() method isn't called if Tomcat Session deleted by the node which didn't create it #5408

pull/5420/head
Nikita Koksharov 1 year ago
parent bd4a8f67d4
commit 17981b4347

@ -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

@ -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);
}

@ -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;
}
}

@ -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

@ -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

@ -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

Loading…
Cancel
Save