Fixed - HttpSessionListener#sessionDestoyed isn't invoked if session wasn't loaded by Tomcat instance. #2104

pull/2160/head
Nikita Koksharov 6 years ago
parent 68286c2683
commit b10e8c559c

@ -62,12 +62,15 @@ public class RedissonSession extends StandardSession {
private Set<String> removedAttributes = Collections.emptySet(); private Set<String> 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); super(manager);
this.redissonManager = manager; this.redissonManager = manager;
this.readMode = readMode; this.readMode = readMode;
this.updateMode = updateMode; this.updateMode = updateMode;
this.topic = redissonManager.getTopic(); this.topic = redissonManager.getTopic();
this.broadcastSessionEvents = broadcastSessionEvents;
if (updateMode == UpdateMode.AFTER_REQUEST) { if (updateMode == UpdateMode.AFTER_REQUEST) {
removedAttributes = Collections.newSetFromMap(new ConcurrentHashMap<String, Boolean>()); removedAttributes = Collections.newSetFromMap(new ConcurrentHashMap<String, Boolean>());
@ -143,7 +146,11 @@ public class RedissonSession extends StandardSession {
map = redissonManager.getMap(id); map = redissonManager.getMap(id);
} }
map.delete(); if (broadcastSessionEvents) {
map.expire(60, TimeUnit.SECONDS);
} else {
map.delete();
}
if (readMode == ReadMode.MEMORY) { if (readMode == ReadMode.MEMORY) {
topic.publish(new AttributesClearMessage(redissonManager.getNodeId(), getId())); topic.publish(new AttributesClearMessage(redissonManager.getNodeId(), getId()));
} }

@ -180,6 +180,10 @@ public class RedissonSessionManager extends ManagerBase implements Lifecycle {
@Override @Override
public Session findSession(String id) throws IOException { 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); Session result = super.findSession(id);
if (result == null) { if (result == null) {
if (id != null) { if (id != null) {
@ -197,7 +201,7 @@ public class RedissonSessionManager extends ManagerBase implements Lifecycle {
RedissonSession session = (RedissonSession) createEmptySession(); RedissonSession session = (RedissonSession) createEmptySession();
session.load(attrs); session.load(attrs);
session.setId(id); session.setId(id, notify);
session.access(); session.access();
session.endAccess(); session.endAccess();
@ -214,7 +218,7 @@ public class RedissonSessionManager extends ManagerBase implements Lifecycle {
@Override @Override
public Session createEmptySession() { public Session createEmptySession() {
return new RedissonSession(this, readMode, updateMode); return new RedissonSession(this, readMode, updateMode, broadcastSessionEvents);
} }
@Override @Override
@ -311,6 +315,14 @@ public class RedissonSessionManager extends ManagerBase implements Lifecycle {
throw new IllegalStateException("Unable to find session: " + msg.getSessionId()); 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) { } catch (Exception e) {

@ -62,12 +62,15 @@ public class RedissonSession extends StandardSession {
private Set<String> removedAttributes = Collections.emptySet(); private Set<String> 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); super(manager);
this.redissonManager = manager; this.redissonManager = manager;
this.readMode = readMode; this.readMode = readMode;
this.updateMode = updateMode; this.updateMode = updateMode;
this.topic = redissonManager.getTopic(); this.topic = redissonManager.getTopic();
this.broadcastSessionEvents = broadcastSessionEvents;
if (updateMode == UpdateMode.AFTER_REQUEST) { if (updateMode == UpdateMode.AFTER_REQUEST) {
removedAttributes = Collections.newSetFromMap(new ConcurrentHashMap<String, Boolean>()); removedAttributes = Collections.newSetFromMap(new ConcurrentHashMap<String, Boolean>());
@ -143,7 +146,11 @@ public class RedissonSession extends StandardSession {
map = redissonManager.getMap(id); map = redissonManager.getMap(id);
} }
map.delete(); if (broadcastSessionEvents) {
map.expire(60, TimeUnit.SECONDS);
} else {
map.delete();
}
if (readMode == ReadMode.MEMORY) { if (readMode == ReadMode.MEMORY) {
topic.publish(new AttributesClearMessage(redissonManager.getNodeId(), getId())); topic.publish(new AttributesClearMessage(redissonManager.getNodeId(), getId()));
} }

@ -19,7 +19,6 @@ import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.Map.Entry;
import java.util.UUID; import java.util.UUID;
import javax.servlet.http.HttpSession; import javax.servlet.http.HttpSession;
@ -159,6 +158,10 @@ public class RedissonSessionManager extends ManagerBase {
@Override @Override
public Session findSession(String id) throws IOException { 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); Session result = super.findSession(id);
if (result == null) { if (result == null) {
if (id != null) { if (id != null) {
@ -169,14 +172,14 @@ public class RedissonSessionManager extends ManagerBase {
log.error("Can't read session object by id: " + id, e); 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"); log.info("Session " + id + " can't be found");
return null; return null;
} }
RedissonSession session = (RedissonSession) createEmptySession(); RedissonSession session = (RedissonSession) createEmptySession();
session.load(attrs); session.load(attrs);
session.setId(id); session.setId(id, notify);
session.access(); session.access();
session.endAccess(); session.endAccess();
@ -190,10 +193,11 @@ public class RedissonSessionManager extends ManagerBase {
return result; return result;
} }
@Override @Override
public Session createEmptySession() { public Session createEmptySession() {
return new RedissonSession(this, readMode, updateMode); return new RedissonSession(this, readMode, updateMode, broadcastSessionEvents);
} }
@Override @Override
@ -291,6 +295,15 @@ public class RedissonSessionManager extends ManagerBase {
throw new IllegalStateException("Unable to find session: " + msg.getSessionId()); 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) { } catch (Exception e) {

@ -24,7 +24,7 @@ public class RedissonSessionManagerTest {
TomcatServer server2 = new TomcatServer("myapp", 8081, "src/test/"); TomcatServer server2 = new TomcatServer("myapp", 8081, "src/test/");
server2.start(); server2.start();
Executor executor = Executor.newInstance(); Executor executor = Executor.newInstance();
BasicCookieStore cookieStore = new BasicCookieStore(); BasicCookieStore cookieStore = new BasicCookieStore();
executor.use(cookieStore); executor.use(cookieStore);
@ -34,16 +34,20 @@ public class RedissonSessionManagerTest {
write(executor, "test", "1234"); write(executor, "test", "1234");
TomcatServer server3 = new TomcatServer("myapp", 8082, "src/test/");
server3.start();
invalidate(executor); invalidate(executor);
Thread.sleep(500); Thread.sleep(500);
Assert.assertEquals(2, TestHttpSessionListener.CREATED_INVOCATION_COUNTER); Assert.assertEquals(2, TestHttpSessionListener.CREATED_INVOCATION_COUNTER);
Assert.assertEquals(2, TestHttpSessionListener.DESTROYED_INVOCATION_COUNTER); Assert.assertEquals(3, TestHttpSessionListener.DESTROYED_INVOCATION_COUNTER);
Executor.closeIdleConnections(); Executor.closeIdleConnections();
server1.stop(); server1.stop();
server2.stop(); server2.stop();
server3.stop();
} }
@Test @Test

@ -62,12 +62,15 @@ public class RedissonSession extends StandardSession {
private Set<String> removedAttributes = Collections.emptySet(); private Set<String> 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); super(manager);
this.redissonManager = manager; this.redissonManager = manager;
this.readMode = readMode; this.readMode = readMode;
this.updateMode = updateMode; this.updateMode = updateMode;
this.topic = redissonManager.getTopic(); this.topic = redissonManager.getTopic();
this.broadcastSessionEvents = broadcastSessionEvents;
if (updateMode == UpdateMode.AFTER_REQUEST) { if (updateMode == UpdateMode.AFTER_REQUEST) {
removedAttributes = Collections.newSetFromMap(new ConcurrentHashMap<String, Boolean>()); removedAttributes = Collections.newSetFromMap(new ConcurrentHashMap<String, Boolean>());
@ -143,7 +146,11 @@ public class RedissonSession extends StandardSession {
map = redissonManager.getMap(id); map = redissonManager.getMap(id);
} }
map.delete(); if (broadcastSessionEvents) {
map.expire(60, TimeUnit.SECONDS);
} else {
map.delete();
}
if (readMode == ReadMode.MEMORY) { if (readMode == ReadMode.MEMORY) {
topic.publish(new AttributesClearMessage(redissonManager.getNodeId(), getId())); topic.publish(new AttributesClearMessage(redissonManager.getNodeId(), getId()));
} }
@ -187,7 +194,7 @@ public class RedissonSession extends StandardSession {
map.expire(maxInactiveInterval + 60, TimeUnit.SECONDS); map.expire(maxInactiveInterval + 60, TimeUnit.SECONDS);
} }
} }
protected AttributesPutAllMessage createPutAllMessage(Map<String, Object> newMap) { protected AttributesPutAllMessage createPutAllMessage(Map<String, Object> newMap) {
Map<String, Object> map = new HashMap<String, Object>(); Map<String, Object> map = new HashMap<String, Object>();
for (Entry<String, Object> entry : newMap.entrySet()) { for (Entry<String, Object> entry : newMap.entrySet()) {

@ -19,7 +19,6 @@ import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.Map.Entry;
import java.util.UUID; import java.util.UUID;
import javax.servlet.http.HttpSession; import javax.servlet.http.HttpSession;
@ -158,6 +157,10 @@ public class RedissonSessionManager extends ManagerBase {
@Override @Override
public Session findSession(String id) throws IOException { 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); Session result = super.findSession(id);
if (result == null) { if (result == null) {
if (id != null) { if (id != null) {
@ -168,14 +171,14 @@ public class RedissonSessionManager extends ManagerBase {
log.error("Can't read session object by id: " + id, e); 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"); log.info("Session " + id + " can't be found");
return null; return null;
} }
RedissonSession session = (RedissonSession) createEmptySession(); RedissonSession session = (RedissonSession) createEmptySession();
session.load(attrs); session.load(attrs);
session.setId(id); session.setId(id, notify);
session.access(); session.access();
session.endAccess(); session.endAccess();
@ -192,7 +195,7 @@ public class RedissonSessionManager extends ManagerBase {
@Override @Override
public Session createEmptySession() { public Session createEmptySession() {
return new RedissonSession(this, readMode, updateMode); return new RedissonSession(this, readMode, updateMode, broadcastSessionEvents);
} }
@Override @Override
@ -289,7 +292,16 @@ public class RedissonSessionManager extends ManagerBase {
if (s == null) { if (s == null) {
throw new IllegalStateException("Unable to find session: " + msg.getSessionId()); 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) { } catch (Exception e) {

@ -62,12 +62,15 @@ public class RedissonSession extends StandardSession {
private Set<String> removedAttributes = Collections.emptySet(); private Set<String> 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); super(manager);
this.redissonManager = manager; this.redissonManager = manager;
this.readMode = readMode; this.readMode = readMode;
this.updateMode = updateMode; this.updateMode = updateMode;
this.topic = redissonManager.getTopic(); this.topic = redissonManager.getTopic();
this.broadcastSessionEvents = broadcastSessionEvents;
if (updateMode == UpdateMode.AFTER_REQUEST) { if (updateMode == UpdateMode.AFTER_REQUEST) {
removedAttributes = Collections.newSetFromMap(new ConcurrentHashMap<String, Boolean>()); removedAttributes = Collections.newSetFromMap(new ConcurrentHashMap<String, Boolean>());
@ -143,7 +146,11 @@ public class RedissonSession extends StandardSession {
map = redissonManager.getMap(id); map = redissonManager.getMap(id);
} }
map.delete(); if (broadcastSessionEvents) {
map.expire(60, TimeUnit.SECONDS);
} else {
map.delete();
}
if (readMode == ReadMode.MEMORY) { if (readMode == ReadMode.MEMORY) {
topic.publish(new AttributesClearMessage(redissonManager.getNodeId(), getId())); topic.publish(new AttributesClearMessage(redissonManager.getNodeId(), getId()));
} }
@ -187,7 +194,7 @@ public class RedissonSession extends StandardSession {
map.expire(maxInactiveInterval + 60, TimeUnit.SECONDS); map.expire(maxInactiveInterval + 60, TimeUnit.SECONDS);
} }
} }
protected AttributesPutAllMessage createPutAllMessage(Map<String, Object> newMap) { protected AttributesPutAllMessage createPutAllMessage(Map<String, Object> newMap) {
Map<String, Object> map = new HashMap<String, Object>(); Map<String, Object> map = new HashMap<String, Object>();
for (Entry<String, Object> entry : newMap.entrySet()) { for (Entry<String, Object> entry : newMap.entrySet()) {

@ -19,7 +19,6 @@ import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.Map.Entry;
import java.util.UUID; import java.util.UUID;
import javax.servlet.http.HttpSession; import javax.servlet.http.HttpSession;
@ -158,6 +157,10 @@ public class RedissonSessionManager extends ManagerBase {
@Override @Override
public Session findSession(String id) throws IOException { 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); Session result = super.findSession(id);
if (result == null) { if (result == null) {
if (id != null) { if (id != null) {
@ -168,14 +171,14 @@ public class RedissonSessionManager extends ManagerBase {
log.error("Can't read session object by id: " + id, e); 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"); log.info("Session " + id + " can't be found");
return null; return null;
} }
RedissonSession session = (RedissonSession) createEmptySession(); RedissonSession session = (RedissonSession) createEmptySession();
session.load(attrs); session.load(attrs);
session.setId(id); session.setId(id, notify);
session.access(); session.access();
session.endAccess(); session.endAccess();
@ -192,7 +195,7 @@ public class RedissonSessionManager extends ManagerBase {
@Override @Override
public Session createEmptySession() { public Session createEmptySession() {
return new RedissonSession(this, readMode, updateMode); return new RedissonSession(this, readMode, updateMode, broadcastSessionEvents);
} }
@Override @Override
@ -289,7 +292,16 @@ public class RedissonSessionManager extends ManagerBase {
if (s == null) { if (s == null) {
throw new IllegalStateException("Unable to find session: " + msg.getSessionId()); 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) { } catch (Exception e) {

Loading…
Cancel
Save