From b10e8c559cdfe44d6d56a38ae1bb25e469aef550 Mon Sep 17 00:00:00 2001 From: Nikita Koksharov Date: Mon, 27 May 2019 16:47:12 +0300 Subject: [PATCH] Fixed - HttpSessionListener#sessionDestoyed isn't invoked if session wasn't loaded by Tomcat instance. #2104 --- .../org/redisson/tomcat/RedissonSession.java | 11 ++++++-- .../tomcat/RedissonSessionManager.java | 16 ++++++++++-- .../org/redisson/tomcat/RedissonSession.java | 11 ++++++-- .../tomcat/RedissonSessionManager.java | 23 ++++++++++++---- .../tomcat/RedissonSessionManagerTest.java | 8 ++++-- .../org/redisson/tomcat/RedissonSession.java | 13 +++++++--- .../tomcat/RedissonSessionManager.java | 26 ++++++++++++++----- .../org/redisson/tomcat/RedissonSession.java | 13 +++++++--- .../tomcat/RedissonSessionManager.java | 24 ++++++++++++----- 9 files changed, 113 insertions(+), 32 deletions(-) diff --git a/redisson-tomcat/redisson-tomcat-6/src/main/java/org/redisson/tomcat/RedissonSession.java b/redisson-tomcat/redisson-tomcat-6/src/main/java/org/redisson/tomcat/RedissonSession.java index 516e5a8e2..7a679677f 100644 --- a/redisson-tomcat/redisson-tomcat-6/src/main/java/org/redisson/tomcat/RedissonSession.java +++ b/redisson-tomcat/redisson-tomcat-6/src/main/java/org/redisson/tomcat/RedissonSession.java @@ -62,12 +62,15 @@ public class RedissonSession extends StandardSession { private Set removedAttributes = Collections.emptySet(); - public RedissonSession(RedissonSessionManager manager, ReadMode readMode, UpdateMode updateMode) { + private final boolean broadcastSessionEvents; + + public RedissonSession(RedissonSessionManager manager, RedissonSessionManager.ReadMode readMode, UpdateMode updateMode, boolean broadcastSessionEvents) { super(manager); this.redissonManager = manager; this.readMode = readMode; this.updateMode = updateMode; this.topic = redissonManager.getTopic(); + this.broadcastSessionEvents = broadcastSessionEvents; if (updateMode == UpdateMode.AFTER_REQUEST) { removedAttributes = Collections.newSetFromMap(new ConcurrentHashMap()); @@ -143,7 +146,11 @@ public class RedissonSession extends StandardSession { map = redissonManager.getMap(id); } - map.delete(); + if (broadcastSessionEvents) { + map.expire(60, TimeUnit.SECONDS); + } else { + map.delete(); + } if (readMode == ReadMode.MEMORY) { topic.publish(new AttributesClearMessage(redissonManager.getNodeId(), getId())); } diff --git a/redisson-tomcat/redisson-tomcat-6/src/main/java/org/redisson/tomcat/RedissonSessionManager.java b/redisson-tomcat/redisson-tomcat-6/src/main/java/org/redisson/tomcat/RedissonSessionManager.java index e76b772fe..125ea8e91 100644 --- a/redisson-tomcat/redisson-tomcat-6/src/main/java/org/redisson/tomcat/RedissonSessionManager.java +++ b/redisson-tomcat/redisson-tomcat-6/src/main/java/org/redisson/tomcat/RedissonSessionManager.java @@ -180,6 +180,10 @@ public class RedissonSessionManager extends ManagerBase implements Lifecycle { @Override public Session findSession(String id) throws IOException { + return findSession(id, true); + } + + private Session findSession(String id, boolean notify) throws IOException { Session result = super.findSession(id); if (result == null) { if (id != null) { @@ -197,7 +201,7 @@ public class RedissonSessionManager extends ManagerBase implements Lifecycle { RedissonSession session = (RedissonSession) createEmptySession(); session.load(attrs); - session.setId(id); + session.setId(id, notify); session.access(); session.endAccess(); @@ -214,7 +218,7 @@ public class RedissonSessionManager extends ManagerBase implements Lifecycle { @Override public Session createEmptySession() { - return new RedissonSession(this, readMode, updateMode); + return new RedissonSession(this, readMode, updateMode, broadcastSessionEvents); } @Override @@ -311,6 +315,14 @@ public class RedissonSessionManager extends ManagerBase implements Lifecycle { throw new IllegalStateException("Unable to find session: " + msg.getSessionId()); } } + + if (msg instanceof SessionDestroyedMessage) { + Session s = findSession(msg.getSessionId(), false); + if (s == null) { + throw new IllegalStateException("Unable to find session: " + msg.getSessionId()); + } + s.expire(); + } } } catch (Exception e) { diff --git a/redisson-tomcat/redisson-tomcat-7/src/main/java/org/redisson/tomcat/RedissonSession.java b/redisson-tomcat/redisson-tomcat-7/src/main/java/org/redisson/tomcat/RedissonSession.java index a6a04e901..4fe201f19 100644 --- a/redisson-tomcat/redisson-tomcat-7/src/main/java/org/redisson/tomcat/RedissonSession.java +++ b/redisson-tomcat/redisson-tomcat-7/src/main/java/org/redisson/tomcat/RedissonSession.java @@ -62,12 +62,15 @@ public class RedissonSession extends StandardSession { private Set removedAttributes = Collections.emptySet(); - public RedissonSession(RedissonSessionManager manager, RedissonSessionManager.ReadMode readMode, UpdateMode updateMode) { + private final boolean broadcastSessionEvents; + + public RedissonSession(RedissonSessionManager manager, RedissonSessionManager.ReadMode readMode, UpdateMode updateMode, boolean broadcastSessionEvents) { super(manager); this.redissonManager = manager; this.readMode = readMode; this.updateMode = updateMode; this.topic = redissonManager.getTopic(); + this.broadcastSessionEvents = broadcastSessionEvents; if (updateMode == UpdateMode.AFTER_REQUEST) { removedAttributes = Collections.newSetFromMap(new ConcurrentHashMap()); @@ -143,7 +146,11 @@ public class RedissonSession extends StandardSession { map = redissonManager.getMap(id); } - map.delete(); + if (broadcastSessionEvents) { + map.expire(60, TimeUnit.SECONDS); + } else { + map.delete(); + } if (readMode == ReadMode.MEMORY) { topic.publish(new AttributesClearMessage(redissonManager.getNodeId(), getId())); } 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 b0d980dcc..2e39b1f8b 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 @@ -19,7 +19,6 @@ import java.io.File; import java.io.IOException; import java.util.HashMap; import java.util.Map; -import java.util.Map.Entry; import java.util.UUID; import javax.servlet.http.HttpSession; @@ -159,6 +158,10 @@ public class RedissonSessionManager extends ManagerBase { @Override public Session findSession(String id) throws IOException { + return findSession(id, true); + } + + private Session findSession(String id, boolean notify) throws IOException { Session result = super.findSession(id); if (result == null) { if (id != null) { @@ -169,14 +172,14 @@ public class RedissonSessionManager extends ManagerBase { log.error("Can't read session object by id: " + id, e); } - if (attrs.isEmpty()) { + if (attrs.isEmpty()) { log.info("Session " + id + " can't be found"); - return null; + return null; } RedissonSession session = (RedissonSession) createEmptySession(); session.load(attrs); - session.setId(id); + session.setId(id, notify); session.access(); session.endAccess(); @@ -190,10 +193,11 @@ public class RedissonSessionManager extends ManagerBase { return result; } + @Override public Session createEmptySession() { - return new RedissonSession(this, readMode, updateMode); + return new RedissonSession(this, readMode, updateMode, broadcastSessionEvents); } @Override @@ -291,6 +295,15 @@ public class RedissonSessionManager extends ManagerBase { throw new IllegalStateException("Unable to find session: " + msg.getSessionId()); } } + + if (msg instanceof SessionDestroyedMessage) { + Session s = findSession(msg.getSessionId(), false); + if (s == null) { + throw new IllegalStateException("Unable to find session: " + msg.getSessionId()); + } + s.expire(); + } + } } catch (Exception e) { diff --git a/redisson-tomcat/redisson-tomcat-7/src/test/java/org/redisson/tomcat/RedissonSessionManagerTest.java b/redisson-tomcat/redisson-tomcat-7/src/test/java/org/redisson/tomcat/RedissonSessionManagerTest.java index c1aa80630..04e8eaf72 100644 --- a/redisson-tomcat/redisson-tomcat-7/src/test/java/org/redisson/tomcat/RedissonSessionManagerTest.java +++ b/redisson-tomcat/redisson-tomcat-7/src/test/java/org/redisson/tomcat/RedissonSessionManagerTest.java @@ -24,7 +24,7 @@ public class RedissonSessionManagerTest { TomcatServer server2 = new TomcatServer("myapp", 8081, "src/test/"); server2.start(); - + Executor executor = Executor.newInstance(); BasicCookieStore cookieStore = new BasicCookieStore(); executor.use(cookieStore); @@ -34,16 +34,20 @@ public class RedissonSessionManagerTest { write(executor, "test", "1234"); + TomcatServer server3 = new TomcatServer("myapp", 8082, "src/test/"); + server3.start(); + invalidate(executor); Thread.sleep(500); Assert.assertEquals(2, TestHttpSessionListener.CREATED_INVOCATION_COUNTER); - Assert.assertEquals(2, TestHttpSessionListener.DESTROYED_INVOCATION_COUNTER); + Assert.assertEquals(3, TestHttpSessionListener.DESTROYED_INVOCATION_COUNTER); Executor.closeIdleConnections(); server1.stop(); server2.stop(); + server3.stop(); } @Test diff --git a/redisson-tomcat/redisson-tomcat-8/src/main/java/org/redisson/tomcat/RedissonSession.java b/redisson-tomcat/redisson-tomcat-8/src/main/java/org/redisson/tomcat/RedissonSession.java index 83584497c..4fe201f19 100644 --- a/redisson-tomcat/redisson-tomcat-8/src/main/java/org/redisson/tomcat/RedissonSession.java +++ b/redisson-tomcat/redisson-tomcat-8/src/main/java/org/redisson/tomcat/RedissonSession.java @@ -62,12 +62,15 @@ public class RedissonSession extends StandardSession { private Set removedAttributes = Collections.emptySet(); - public RedissonSession(RedissonSessionManager manager, RedissonSessionManager.ReadMode readMode, UpdateMode updateMode) { + private final boolean broadcastSessionEvents; + + public RedissonSession(RedissonSessionManager manager, RedissonSessionManager.ReadMode readMode, UpdateMode updateMode, boolean broadcastSessionEvents) { super(manager); this.redissonManager = manager; this.readMode = readMode; this.updateMode = updateMode; this.topic = redissonManager.getTopic(); + this.broadcastSessionEvents = broadcastSessionEvents; if (updateMode == UpdateMode.AFTER_REQUEST) { removedAttributes = Collections.newSetFromMap(new ConcurrentHashMap()); @@ -143,7 +146,11 @@ public class RedissonSession extends StandardSession { map = redissonManager.getMap(id); } - map.delete(); + if (broadcastSessionEvents) { + map.expire(60, TimeUnit.SECONDS); + } else { + map.delete(); + } if (readMode == ReadMode.MEMORY) { topic.publish(new AttributesClearMessage(redissonManager.getNodeId(), getId())); } @@ -187,7 +194,7 @@ public class RedissonSession extends StandardSession { map.expire(maxInactiveInterval + 60, TimeUnit.SECONDS); } } - + protected AttributesPutAllMessage createPutAllMessage(Map newMap) { Map map = new HashMap(); for (Entry entry : newMap.entrySet()) { 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 2b0703099..9a881d317 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 @@ -19,7 +19,6 @@ import java.io.File; import java.io.IOException; import java.util.HashMap; import java.util.Map; -import java.util.Map.Entry; import java.util.UUID; import javax.servlet.http.HttpSession; @@ -158,6 +157,10 @@ public class RedissonSessionManager extends ManagerBase { @Override public Session findSession(String id) throws IOException { + return findSession(id, true); + } + + private Session findSession(String id, boolean notify) throws IOException { Session result = super.findSession(id); if (result == null) { if (id != null) { @@ -168,14 +171,14 @@ public class RedissonSessionManager extends ManagerBase { log.error("Can't read session object by id: " + id, e); } - if (attrs.isEmpty()) { + if (attrs.isEmpty()) { log.info("Session " + id + " can't be found"); - return null; + return null; } - + RedissonSession session = (RedissonSession) createEmptySession(); session.load(attrs); - session.setId(id); + session.setId(id, notify); session.access(); session.endAccess(); @@ -192,7 +195,7 @@ public class RedissonSessionManager extends ManagerBase { @Override public Session createEmptySession() { - return new RedissonSession(this, readMode, updateMode); + return new RedissonSession(this, readMode, updateMode, broadcastSessionEvents); } @Override @@ -289,7 +292,16 @@ public class RedissonSessionManager extends ManagerBase { if (s == null) { throw new IllegalStateException("Unable to find session: " + msg.getSessionId()); } - } + } + + if (msg instanceof SessionDestroyedMessage) { + Session s = findSession(msg.getSessionId(), false); + if (s == null) { + throw new IllegalStateException("Unable to find session: " + msg.getSessionId()); + } + s.expire(); + } + } } catch (Exception e) { diff --git a/redisson-tomcat/redisson-tomcat-9/src/main/java/org/redisson/tomcat/RedissonSession.java b/redisson-tomcat/redisson-tomcat-9/src/main/java/org/redisson/tomcat/RedissonSession.java index 83584497c..4fe201f19 100644 --- a/redisson-tomcat/redisson-tomcat-9/src/main/java/org/redisson/tomcat/RedissonSession.java +++ b/redisson-tomcat/redisson-tomcat-9/src/main/java/org/redisson/tomcat/RedissonSession.java @@ -62,12 +62,15 @@ public class RedissonSession extends StandardSession { private Set removedAttributes = Collections.emptySet(); - public RedissonSession(RedissonSessionManager manager, RedissonSessionManager.ReadMode readMode, UpdateMode updateMode) { + private final boolean broadcastSessionEvents; + + public RedissonSession(RedissonSessionManager manager, RedissonSessionManager.ReadMode readMode, UpdateMode updateMode, boolean broadcastSessionEvents) { super(manager); this.redissonManager = manager; this.readMode = readMode; this.updateMode = updateMode; this.topic = redissonManager.getTopic(); + this.broadcastSessionEvents = broadcastSessionEvents; if (updateMode == UpdateMode.AFTER_REQUEST) { removedAttributes = Collections.newSetFromMap(new ConcurrentHashMap()); @@ -143,7 +146,11 @@ public class RedissonSession extends StandardSession { map = redissonManager.getMap(id); } - map.delete(); + if (broadcastSessionEvents) { + map.expire(60, TimeUnit.SECONDS); + } else { + map.delete(); + } if (readMode == ReadMode.MEMORY) { topic.publish(new AttributesClearMessage(redissonManager.getNodeId(), getId())); } @@ -187,7 +194,7 @@ public class RedissonSession extends StandardSession { map.expire(maxInactiveInterval + 60, TimeUnit.SECONDS); } } - + protected AttributesPutAllMessage createPutAllMessage(Map newMap) { Map map = new HashMap(); for (Entry entry : newMap.entrySet()) { 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 f7990caaf..9a881d317 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 @@ -19,7 +19,6 @@ import java.io.File; import java.io.IOException; import java.util.HashMap; import java.util.Map; -import java.util.Map.Entry; import java.util.UUID; import javax.servlet.http.HttpSession; @@ -158,6 +157,10 @@ public class RedissonSessionManager extends ManagerBase { @Override public Session findSession(String id) throws IOException { + return findSession(id, true); + } + + private Session findSession(String id, boolean notify) throws IOException { Session result = super.findSession(id); if (result == null) { if (id != null) { @@ -168,14 +171,14 @@ public class RedissonSessionManager extends ManagerBase { log.error("Can't read session object by id: " + id, e); } - if (attrs.isEmpty()) { + if (attrs.isEmpty()) { log.info("Session " + id + " can't be found"); - return null; + return null; } RedissonSession session = (RedissonSession) createEmptySession(); session.load(attrs); - session.setId(id); + session.setId(id, notify); session.access(); session.endAccess(); @@ -192,7 +195,7 @@ public class RedissonSessionManager extends ManagerBase { @Override public Session createEmptySession() { - return new RedissonSession(this, readMode, updateMode); + return new RedissonSession(this, readMode, updateMode, broadcastSessionEvents); } @Override @@ -289,7 +292,16 @@ public class RedissonSessionManager extends ManagerBase { if (s == null) { throw new IllegalStateException("Unable to find session: " + msg.getSessionId()); } - } + } + + if (msg instanceof SessionDestroyedMessage) { + Session s = findSession(msg.getSessionId(), false); + if (s == null) { + throw new IllegalStateException("Unable to find session: " + msg.getSessionId()); + } + s.expire(); + } + } } catch (Exception e) {