提交 4b023aa3 编写于 作者: N Nikita Koksharov

Fixed - Tomcat Session Manager doesn't remove session attributes in updateMode=AFTER_REQUEST. #1971

上级 d75f6102
......@@ -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<String> names;
public AttributeRemoveMessage() {
super();
}
public AttributeRemoveMessage(String nodeId, String sessionId, String name) {
public AttributeRemoveMessage(String nodeId, String sessionId, Set<String> names) {
super(nodeId, sessionId);
this.name = name;
this.names = names;
}
public String getName() {
return name;
public Set<String> getNames() {
return names;
}
}
......@@ -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<String> 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<String, Boolean>());
}
try {
Field attr = StandardSession.class.getDeclaredField("attributes");
attrs = (Map<String, Object>) 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<String>(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();
}
......
......@@ -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();
}
}
......
......@@ -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 {
......
......@@ -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<String> names;
public AttributeRemoveMessage() {
super();
}
public AttributeRemoveMessage(String nodeId, String sessionId, String name) {
public AttributeRemoveMessage(String nodeId, String sessionId, Set<String> names) {
super(nodeId, sessionId);
this.name = name;
this.names = names;
}
public String getName() {
return name;
public Set<String> getNames() {
return names;
}
}
......@@ -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<String> 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<String, Boolean>());
}
try {
Field attr = StandardSession.class.getDeclaredField("attributes");
attrs = (Map<String, Object>) 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<String>(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();
}
......
......@@ -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();
}
}
......
......@@ -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 {
......
......@@ -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<String> names;
public AttributeRemoveMessage() {
super();
}
public AttributeRemoveMessage(String nodeId, String sessionId, String name) {
public AttributeRemoveMessage(String nodeId, String sessionId, Set<String> names) {
super(nodeId, sessionId);
this.name = name;
this.names = names;
}
public String getName() {
return name;
public Set<String> getNames() {
return names;
}
}
......@@ -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<String> 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<String, Boolean>());
}
try {
Field attr = StandardSession.class.getDeclaredField("attributes");
attrs = (Map<String, Object>) 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<String>(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();
}
......
......@@ -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();
}
}
......
......@@ -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 {
......
......@@ -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<String> names;
public AttributeRemoveMessage() {
super();
}
public AttributeRemoveMessage(String nodeId, String sessionId, String name) {
public AttributeRemoveMessage(String nodeId, String sessionId, Set<String> names) {
super(nodeId, sessionId);
this.name = name;
this.names = names;
}
public String getName() {
return name;
public Set<String> getNames() {
return names;
}
}
......@@ -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<String> 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<String, Boolean>());
}
try {
Field attr = StandardSession.class.getDeclaredField("attributes");
attrs = (Map<String, Object>) 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<String>(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();
}
......
......@@ -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();
}
}
......
......@@ -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 {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册