diff --git a/redisson-tomcat/redisson-tomcat-6/src/main/java/org/redisson/tomcat/AttributeRemoveMessage.java b/redisson-tomcat/redisson-tomcat-6/src/main/java/org/redisson/tomcat/AttributeRemoveMessage.java index 557141a970fe3a086ebbb94170000c795ce4d289..466ad2e3c39fcc1adc721df76c22dfb8e8b5b64c 100644 --- a/redisson-tomcat/redisson-tomcat-6/src/main/java/org/redisson/tomcat/AttributeRemoveMessage.java +++ b/redisson-tomcat/redisson-tomcat-6/src/main/java/org/redisson/tomcat/AttributeRemoveMessage.java @@ -15,6 +15,8 @@ */ package org.redisson.tomcat; +import java.util.Set; + /** * * @author Nikita Koksharov @@ -22,19 +24,19 @@ package org.redisson.tomcat; */ public class AttributeRemoveMessage extends AttributeMessage { - private String name; + private Set names; public AttributeRemoveMessage() { super(); } - public AttributeRemoveMessage(String nodeId, String sessionId, String name) { + public AttributeRemoveMessage(String nodeId, String sessionId, Set names) { super(nodeId, sessionId); - this.name = name; + this.names = names; } - public String getName() { - return name; + public Set getNames() { + return names; } } 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 e3384dbce4d59a3b9621348921e37edda38a4920..77bfb1c392cbe63e31a5464d6657e65c82379959 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 @@ -25,6 +25,7 @@ import java.util.HashSet; import java.util.Map; import java.util.Map.Entry; import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.TimeUnit; import org.apache.catalina.session.StandardSession; @@ -59,6 +60,8 @@ public class RedissonSession extends StandardSession { private final RedissonSessionManager.ReadMode readMode; private final UpdateMode updateMode; + private Set removedAttributes; + public RedissonSession(RedissonSessionManager manager, ReadMode readMode, UpdateMode updateMode) { super(manager); this.redissonManager = manager; @@ -66,6 +69,10 @@ public class RedissonSession extends StandardSession { this.updateMode = updateMode; this.topic = redissonManager.getTopic(); + if (updateMode == UpdateMode.AFTER_REQUEST) { + removedAttributes = Collections.newSetFromMap(new ConcurrentHashMap()); + } + try { Field attr = StandardSession.class.getDeclaredField("attributes"); attrs = (Map) attr.get(this); @@ -257,6 +264,9 @@ public class RedissonSession extends StandardSession { if (updateMode == UpdateMode.DEFAULT && map != null && value != null) { fastPut(name, value); } + if (updateMode == UpdateMode.AFTER_REQUEST) { + removedAttributes.remove(name); + } } public void superRemoveAttributeInternal(String name, boolean notify) { @@ -270,9 +280,12 @@ public class RedissonSession extends StandardSession { if (updateMode == UpdateMode.DEFAULT && map != null) { map.fastRemove(name); if (readMode == ReadMode.MEMORY) { - topic.publish(new AttributeRemoveMessage(redissonManager.getNodeId(), getId(), name)); + topic.publish(new AttributeRemoveMessage(redissonManager.getNodeId(), getId(), new HashSet(Arrays.asList(name)))); } } + if (updateMode == UpdateMode.AFTER_REQUEST) { + removedAttributes.add(name); + } } public void save() { @@ -295,9 +308,18 @@ public class RedissonSession extends StandardSession { } map.putAll(newMap); + map.fastRemove(removedAttributes.toArray(new String[removedAttributes.size()])); + if (readMode == ReadMode.MEMORY) { topic.publish(createPutAllMessage(newMap)); + + if (updateMode == UpdateMode.AFTER_REQUEST) { + if (!removedAttributes.isEmpty()) { + topic.publish(new AttributeRemoveMessage(redissonManager.getNodeId(), getId(), removedAttributes)); + } + } } + removedAttributes.clear(); expireSession(); } 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 642d18ae5f14f3bc3a7bff7c72326ad19de11f5f..22cbd2a747913737d485794e61f98b95c9139c61 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 @@ -260,7 +260,9 @@ public class RedissonSessionManager extends ManagerBase implements Lifecycle { RedissonSession session = (RedissonSession) RedissonSessionManager.super.findSession(msg.getSessionId()); if (session != null && !msg.getNodeId().equals(nodeId)) { if (msg instanceof AttributeRemoveMessage) { - session.superRemoveAttributeInternal(((AttributeRemoveMessage)msg).getName(), true); + for (String name : ((AttributeRemoveMessage)msg).getNames()) { + session.superRemoveAttributeInternal(name, true); + } } if (msg instanceof AttributesClearMessage) { @@ -332,11 +334,11 @@ public class RedissonSessionManager extends ManagerBase implements Lifecycle { return; } - if (updateMode == UpdateMode.AFTER_REQUEST) { - RedissonSession sess = (RedissonSession) super.findSession(session.getId()); - if (sess != null) { - sess.save(); - } + RedissonSession sess = (RedissonSession) super.findSession(session.getId()); + if (sess != null) { + sess.access(); + sess.endAccess(); + sess.save(); } } diff --git a/redisson-tomcat/redisson-tomcat-6/src/main/java/org/redisson/tomcat/UpdateValve.java b/redisson-tomcat/redisson-tomcat-6/src/main/java/org/redisson/tomcat/UpdateValve.java index c0e676b33248a84d97c810b5fadae23214dc3975..bd9527ba5bfe6ad664758f93e7fafb30c6a99d8b 100644 --- a/redisson-tomcat/redisson-tomcat-6/src/main/java/org/redisson/tomcat/UpdateValve.java +++ b/redisson-tomcat/redisson-tomcat-6/src/main/java/org/redisson/tomcat/UpdateValve.java @@ -19,7 +19,6 @@ import java.io.IOException; import javax.servlet.ServletException; -import org.apache.catalina.Session; import org.apache.catalina.connector.Request; import org.apache.catalina.connector.Response; import org.apache.catalina.valves.ValveBase; @@ -41,19 +40,6 @@ public class UpdateValve extends ValveBase { @Override public void invoke(Request request, Response response) throws IOException, ServletException { - String sessionId = request.getRequestedSessionId(); - Session session = request.getContext().getManager().findSession(sessionId); - if (session != null) { - if (!session.isValid()) { - session.expire(); - request.getContext().getManager().remove(session); - } else { - manager.add(session); - session.access(); - session.endAccess(); - } - } - try { getNext().invoke(request, response); } finally { diff --git a/redisson-tomcat/redisson-tomcat-7/src/main/java/org/redisson/tomcat/AttributeRemoveMessage.java b/redisson-tomcat/redisson-tomcat-7/src/main/java/org/redisson/tomcat/AttributeRemoveMessage.java index 557141a970fe3a086ebbb94170000c795ce4d289..466ad2e3c39fcc1adc721df76c22dfb8e8b5b64c 100644 --- a/redisson-tomcat/redisson-tomcat-7/src/main/java/org/redisson/tomcat/AttributeRemoveMessage.java +++ b/redisson-tomcat/redisson-tomcat-7/src/main/java/org/redisson/tomcat/AttributeRemoveMessage.java @@ -15,6 +15,8 @@ */ package org.redisson.tomcat; +import java.util.Set; + /** * * @author Nikita Koksharov @@ -22,19 +24,19 @@ package org.redisson.tomcat; */ public class AttributeRemoveMessage extends AttributeMessage { - private String name; + private Set names; public AttributeRemoveMessage() { super(); } - public AttributeRemoveMessage(String nodeId, String sessionId, String name) { + public AttributeRemoveMessage(String nodeId, String sessionId, Set names) { super(nodeId, sessionId); - this.name = name; + this.names = names; } - public String getName() { - return name; + public Set getNames() { + return names; } } 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 6eeeff83c8f655887d1d0ea63e692a2aaf341d02..35131b046e8700a57ee4f32f60721f313bcb7d46 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 @@ -25,6 +25,7 @@ import java.util.HashSet; import java.util.Map; import java.util.Map.Entry; import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.TimeUnit; import org.apache.catalina.session.StandardSession; @@ -59,6 +60,8 @@ public class RedissonSession extends StandardSession { private final RedissonSessionManager.ReadMode readMode; private final UpdateMode updateMode; + private Set removedAttributes; + public RedissonSession(RedissonSessionManager manager, RedissonSessionManager.ReadMode readMode, UpdateMode updateMode) { super(manager); this.redissonManager = manager; @@ -66,6 +69,10 @@ public class RedissonSession extends StandardSession { this.updateMode = updateMode; this.topic = redissonManager.getTopic(); + if (updateMode == UpdateMode.AFTER_REQUEST) { + removedAttributes = Collections.newSetFromMap(new ConcurrentHashMap()); + } + try { Field attr = StandardSession.class.getDeclaredField("attributes"); attrs = (Map) attr.get(this); @@ -257,6 +264,9 @@ public class RedissonSession extends StandardSession { if (updateMode == UpdateMode.DEFAULT && map != null && value != null) { fastPut(name, value); } + if (updateMode == UpdateMode.AFTER_REQUEST) { + removedAttributes.remove(name); + } } public void superRemoveAttributeInternal(String name, boolean notify) { @@ -270,9 +280,12 @@ public class RedissonSession extends StandardSession { if (updateMode == UpdateMode.DEFAULT && map != null) { map.fastRemove(name); if (readMode == ReadMode.MEMORY) { - topic.publish(new AttributeRemoveMessage(redissonManager.getNodeId(), getId(), name)); + topic.publish(new AttributeRemoveMessage(redissonManager.getNodeId(), getId(), new HashSet(Arrays.asList(name)))); } } + if (updateMode == UpdateMode.AFTER_REQUEST) { + removedAttributes.add(name); + } } public void save() { @@ -295,9 +308,18 @@ public class RedissonSession extends StandardSession { } map.putAll(newMap); + map.fastRemove(removedAttributes.toArray(new String[removedAttributes.size()])); + if (readMode == ReadMode.MEMORY) { topic.publish(createPutAllMessage(newMap)); + + if (updateMode == UpdateMode.AFTER_REQUEST) { + if (!removedAttributes.isEmpty()) { + topic.publish(new AttributeRemoveMessage(redissonManager.getNodeId(), getId(), removedAttributes)); + } + } } + removedAttributes.clear(); expireSession(); } 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 8b89f8e908ca359a76d6507ed1e346d04dd34078..5cabce824f9e645de8a0e13fc5ba4c3edcb1e2d5 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 @@ -240,7 +240,9 @@ public class RedissonSessionManager extends ManagerBase { RedissonSession session = (RedissonSession) RedissonSessionManager.super.findSession(msg.getSessionId()); if (session != null && !msg.getNodeId().equals(nodeId)) { if (msg instanceof AttributeRemoveMessage) { - session.superRemoveAttributeInternal(((AttributeRemoveMessage)msg).getName(), true); + for (String name : ((AttributeRemoveMessage)msg).getNames()) { + session.superRemoveAttributeInternal(name, true); + } } if (msg instanceof AttributesClearMessage) { @@ -315,11 +317,11 @@ public class RedissonSessionManager extends ManagerBase { return; } - if (updateMode == UpdateMode.AFTER_REQUEST) { - RedissonSession sess = (RedissonSession) super.findSession(session.getId()); - if (sess != null) { - sess.save(); - } + RedissonSession sess = (RedissonSession) super.findSession(session.getId()); + if (sess != null) { + sess.access(); + sess.endAccess(); + sess.save(); } } diff --git a/redisson-tomcat/redisson-tomcat-7/src/main/java/org/redisson/tomcat/UpdateValve.java b/redisson-tomcat/redisson-tomcat-7/src/main/java/org/redisson/tomcat/UpdateValve.java index 2f43cabefcf3bfcc640ea2ef81f3496855b757a5..20e4a49ff4fce747dde9a02aac38fee8e62e2b22 100644 --- a/redisson-tomcat/redisson-tomcat-7/src/main/java/org/redisson/tomcat/UpdateValve.java +++ b/redisson-tomcat/redisson-tomcat-7/src/main/java/org/redisson/tomcat/UpdateValve.java @@ -19,7 +19,6 @@ import java.io.IOException; import javax.servlet.ServletException; -import org.apache.catalina.Session; import org.apache.catalina.connector.Request; import org.apache.catalina.connector.Response; import org.apache.catalina.valves.ValveBase; @@ -41,19 +40,6 @@ public class UpdateValve extends ValveBase { @Override public void invoke(Request request, Response response) throws IOException, ServletException { - String sessionId = request.getRequestedSessionId(); - Session session = request.getContext().getManager().findSession(sessionId); - if (session != null) { - if (!session.isValid()) { - session.expire(); - request.getContext().getManager().remove(session); - } else { - manager.add(session); - session.access(); - session.endAccess(); - } - } - try { getNext().invoke(request, response); } finally { diff --git a/redisson-tomcat/redisson-tomcat-8/src/main/java/org/redisson/tomcat/AttributeRemoveMessage.java b/redisson-tomcat/redisson-tomcat-8/src/main/java/org/redisson/tomcat/AttributeRemoveMessage.java index 557141a970fe3a086ebbb94170000c795ce4d289..466ad2e3c39fcc1adc721df76c22dfb8e8b5b64c 100644 --- a/redisson-tomcat/redisson-tomcat-8/src/main/java/org/redisson/tomcat/AttributeRemoveMessage.java +++ b/redisson-tomcat/redisson-tomcat-8/src/main/java/org/redisson/tomcat/AttributeRemoveMessage.java @@ -15,6 +15,8 @@ */ package org.redisson.tomcat; +import java.util.Set; + /** * * @author Nikita Koksharov @@ -22,19 +24,19 @@ package org.redisson.tomcat; */ public class AttributeRemoveMessage extends AttributeMessage { - private String name; + private Set names; public AttributeRemoveMessage() { super(); } - public AttributeRemoveMessage(String nodeId, String sessionId, String name) { + public AttributeRemoveMessage(String nodeId, String sessionId, Set names) { super(nodeId, sessionId); - this.name = name; + this.names = names; } - public String getName() { - return name; + public Set getNames() { + return names; } } 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 760e35f33b0bf0d82a5469f02f6268a37c26d17f..8eb2a062193ddda1c8010aca2897cfa78c8f354a 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 @@ -25,6 +25,7 @@ import java.util.HashSet; import java.util.Map; import java.util.Map.Entry; import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.TimeUnit; import org.apache.catalina.session.StandardSession; @@ -59,6 +60,8 @@ public class RedissonSession extends StandardSession { private final RedissonSessionManager.ReadMode readMode; private final UpdateMode updateMode; + private Set removedAttributes; + public RedissonSession(RedissonSessionManager manager, RedissonSessionManager.ReadMode readMode, UpdateMode updateMode) { super(manager); this.redissonManager = manager; @@ -66,6 +69,10 @@ public class RedissonSession extends StandardSession { this.updateMode = updateMode; this.topic = redissonManager.getTopic(); + if (updateMode == UpdateMode.AFTER_REQUEST) { + removedAttributes = Collections.newSetFromMap(new ConcurrentHashMap()); + } + try { Field attr = StandardSession.class.getDeclaredField("attributes"); attrs = (Map) attr.get(this); @@ -257,6 +264,9 @@ public class RedissonSession extends StandardSession { if (updateMode == UpdateMode.DEFAULT && map != null && value != null) { fastPut(name, value); } + if (updateMode == UpdateMode.AFTER_REQUEST) { + removedAttributes.remove(name); + } } public void superRemoveAttributeInternal(String name, boolean notify) { @@ -270,9 +280,12 @@ public class RedissonSession extends StandardSession { if (updateMode == UpdateMode.DEFAULT && map != null) { map.fastRemove(name); if (readMode == ReadMode.MEMORY) { - topic.publish(new AttributeRemoveMessage(redissonManager.getNodeId(), getId(), name)); + topic.publish(new AttributeRemoveMessage(redissonManager.getNodeId(), getId(), new HashSet(Arrays.asList(name)))); } } + if (updateMode == UpdateMode.AFTER_REQUEST) { + removedAttributes.add(name); + } } public void save() { @@ -295,9 +308,18 @@ public class RedissonSession extends StandardSession { } map.putAll(newMap); + map.fastRemove(removedAttributes.toArray(new String[removedAttributes.size()])); + if (readMode == ReadMode.MEMORY) { topic.publish(createPutAllMessage(newMap)); + + if (updateMode == UpdateMode.AFTER_REQUEST) { + if (!removedAttributes.isEmpty()) { + topic.publish(new AttributeRemoveMessage(redissonManager.getNodeId(), getId(), removedAttributes)); + } + } } + removedAttributes.clear(); expireSession(); } 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 813bc63218e3b1ed8ae50632f675ffe7bda23d72..000a9e6d4c8602735b86bbcc8f8f0aa62070aadf 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 @@ -239,7 +239,9 @@ public class RedissonSessionManager extends ManagerBase { RedissonSession session = (RedissonSession) RedissonSessionManager.super.findSession(msg.getSessionId()); if (session != null && !msg.getNodeId().equals(nodeId)) { if (msg instanceof AttributeRemoveMessage) { - session.superRemoveAttributeInternal(((AttributeRemoveMessage)msg).getName(), true); + for (String name : ((AttributeRemoveMessage)msg).getNames()) { + session.superRemoveAttributeInternal(name, true); + } } if (msg instanceof AttributesClearMessage) { @@ -314,11 +316,11 @@ public class RedissonSessionManager extends ManagerBase { return; } - if (updateMode == UpdateMode.AFTER_REQUEST) { - RedissonSession sess = (RedissonSession) super.findSession(session.getId()); - if (sess != null) { - sess.save(); - } + RedissonSession sess = (RedissonSession) super.findSession(session.getId()); + if (sess != null) { + sess.access(); + sess.endAccess(); + sess.save(); } } diff --git a/redisson-tomcat/redisson-tomcat-8/src/main/java/org/redisson/tomcat/UpdateValve.java b/redisson-tomcat/redisson-tomcat-8/src/main/java/org/redisson/tomcat/UpdateValve.java index 2f43cabefcf3bfcc640ea2ef81f3496855b757a5..20e4a49ff4fce747dde9a02aac38fee8e62e2b22 100644 --- a/redisson-tomcat/redisson-tomcat-8/src/main/java/org/redisson/tomcat/UpdateValve.java +++ b/redisson-tomcat/redisson-tomcat-8/src/main/java/org/redisson/tomcat/UpdateValve.java @@ -19,7 +19,6 @@ import java.io.IOException; import javax.servlet.ServletException; -import org.apache.catalina.Session; import org.apache.catalina.connector.Request; import org.apache.catalina.connector.Response; import org.apache.catalina.valves.ValveBase; @@ -41,19 +40,6 @@ public class UpdateValve extends ValveBase { @Override public void invoke(Request request, Response response) throws IOException, ServletException { - String sessionId = request.getRequestedSessionId(); - Session session = request.getContext().getManager().findSession(sessionId); - if (session != null) { - if (!session.isValid()) { - session.expire(); - request.getContext().getManager().remove(session); - } else { - manager.add(session); - session.access(); - session.endAccess(); - } - } - try { getNext().invoke(request, response); } finally { diff --git a/redisson-tomcat/redisson-tomcat-9/src/main/java/org/redisson/tomcat/AttributeRemoveMessage.java b/redisson-tomcat/redisson-tomcat-9/src/main/java/org/redisson/tomcat/AttributeRemoveMessage.java index 557141a970fe3a086ebbb94170000c795ce4d289..466ad2e3c39fcc1adc721df76c22dfb8e8b5b64c 100644 --- a/redisson-tomcat/redisson-tomcat-9/src/main/java/org/redisson/tomcat/AttributeRemoveMessage.java +++ b/redisson-tomcat/redisson-tomcat-9/src/main/java/org/redisson/tomcat/AttributeRemoveMessage.java @@ -15,6 +15,8 @@ */ package org.redisson.tomcat; +import java.util.Set; + /** * * @author Nikita Koksharov @@ -22,19 +24,19 @@ package org.redisson.tomcat; */ public class AttributeRemoveMessage extends AttributeMessage { - private String name; + private Set names; public AttributeRemoveMessage() { super(); } - public AttributeRemoveMessage(String nodeId, String sessionId, String name) { + public AttributeRemoveMessage(String nodeId, String sessionId, Set names) { super(nodeId, sessionId); - this.name = name; + this.names = names; } - public String getName() { - return name; + public Set getNames() { + return names; } } 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 d55c7f479fe2912f667149ac24aaca9d58867fc6..8eb2a062193ddda1c8010aca2897cfa78c8f354a 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 @@ -25,6 +25,7 @@ import java.util.HashSet; import java.util.Map; import java.util.Map.Entry; import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.TimeUnit; import org.apache.catalina.session.StandardSession; @@ -59,6 +60,8 @@ public class RedissonSession extends StandardSession { private final RedissonSessionManager.ReadMode readMode; private final UpdateMode updateMode; + private Set removedAttributes; + public RedissonSession(RedissonSessionManager manager, RedissonSessionManager.ReadMode readMode, UpdateMode updateMode) { super(manager); this.redissonManager = manager; @@ -66,6 +69,10 @@ public class RedissonSession extends StandardSession { this.updateMode = updateMode; this.topic = redissonManager.getTopic(); + if (updateMode == UpdateMode.AFTER_REQUEST) { + removedAttributes = Collections.newSetFromMap(new ConcurrentHashMap()); + } + try { Field attr = StandardSession.class.getDeclaredField("attributes"); attrs = (Map) attr.get(this); @@ -257,6 +264,9 @@ public class RedissonSession extends StandardSession { if (updateMode == UpdateMode.DEFAULT && map != null && value != null) { fastPut(name, value); } + if (updateMode == UpdateMode.AFTER_REQUEST) { + removedAttributes.remove(name); + } } public void superRemoveAttributeInternal(String name, boolean notify) { @@ -270,9 +280,12 @@ public class RedissonSession extends StandardSession { if (updateMode == UpdateMode.DEFAULT && map != null) { map.fastRemove(name); if (readMode == ReadMode.MEMORY) { - topic.publish(new AttributeRemoveMessage(redissonManager.getNodeId(), getId(), name)); + topic.publish(new AttributeRemoveMessage(redissonManager.getNodeId(), getId(), new HashSet(Arrays.asList(name)))); } } + if (updateMode == UpdateMode.AFTER_REQUEST) { + removedAttributes.add(name); + } } public void save() { @@ -295,9 +308,19 @@ public class RedissonSession extends StandardSession { } map.putAll(newMap); + map.fastRemove(removedAttributes.toArray(new String[removedAttributes.size()])); + if (readMode == ReadMode.MEMORY) { topic.publish(createPutAllMessage(newMap)); + + if (updateMode == UpdateMode.AFTER_REQUEST) { + if (!removedAttributes.isEmpty()) { + topic.publish(new AttributeRemoveMessage(redissonManager.getNodeId(), getId(), removedAttributes)); + } + } } + removedAttributes.clear(); + expireSession(); } 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 813bc63218e3b1ed8ae50632f675ffe7bda23d72..000a9e6d4c8602735b86bbcc8f8f0aa62070aadf 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 @@ -239,7 +239,9 @@ public class RedissonSessionManager extends ManagerBase { RedissonSession session = (RedissonSession) RedissonSessionManager.super.findSession(msg.getSessionId()); if (session != null && !msg.getNodeId().equals(nodeId)) { if (msg instanceof AttributeRemoveMessage) { - session.superRemoveAttributeInternal(((AttributeRemoveMessage)msg).getName(), true); + for (String name : ((AttributeRemoveMessage)msg).getNames()) { + session.superRemoveAttributeInternal(name, true); + } } if (msg instanceof AttributesClearMessage) { @@ -314,11 +316,11 @@ public class RedissonSessionManager extends ManagerBase { return; } - if (updateMode == UpdateMode.AFTER_REQUEST) { - RedissonSession sess = (RedissonSession) super.findSession(session.getId()); - if (sess != null) { - sess.save(); - } + RedissonSession sess = (RedissonSession) super.findSession(session.getId()); + if (sess != null) { + sess.access(); + sess.endAccess(); + sess.save(); } } diff --git a/redisson-tomcat/redisson-tomcat-9/src/main/java/org/redisson/tomcat/UpdateValve.java b/redisson-tomcat/redisson-tomcat-9/src/main/java/org/redisson/tomcat/UpdateValve.java index 2f43cabefcf3bfcc640ea2ef81f3496855b757a5..20e4a49ff4fce747dde9a02aac38fee8e62e2b22 100644 --- a/redisson-tomcat/redisson-tomcat-9/src/main/java/org/redisson/tomcat/UpdateValve.java +++ b/redisson-tomcat/redisson-tomcat-9/src/main/java/org/redisson/tomcat/UpdateValve.java @@ -19,7 +19,6 @@ import java.io.IOException; import javax.servlet.ServletException; -import org.apache.catalina.Session; import org.apache.catalina.connector.Request; import org.apache.catalina.connector.Response; import org.apache.catalina.valves.ValveBase; @@ -41,19 +40,6 @@ public class UpdateValve extends ValveBase { @Override public void invoke(Request request, Response response) throws IOException, ServletException { - String sessionId = request.getRequestedSessionId(); - Session session = request.getContext().getManager().findSession(sessionId); - if (session != null) { - if (!session.isValid()) { - session.expire(); - request.getContext().getManager().remove(session); - } else { - manager.add(session); - session.access(); - session.endAccess(); - } - } - try { getNext().invoke(request, response); } finally {