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();
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<String, Boolean>());
@ -143,7 +146,11 @@ public class RedissonSession extends StandardSession {
map = redissonManager.getMap(id);
}
if (broadcastSessionEvents) {
map.expire(60, TimeUnit.SECONDS);
} else {
map.delete();
}
if (readMode == ReadMode.MEMORY) {
topic.publish(new AttributesClearMessage(redissonManager.getNodeId(), getId()));
}

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

@ -62,12 +62,15 @@ public class RedissonSession extends StandardSession {
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);
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<String, Boolean>());
@ -143,7 +146,11 @@ public class RedissonSession extends StandardSession {
map = redissonManager.getMap(id);
}
if (broadcastSessionEvents) {
map.expire(60, TimeUnit.SECONDS);
} else {
map.delete();
}
if (readMode == ReadMode.MEMORY) {
topic.publish(new AttributesClearMessage(redissonManager.getNodeId(), getId()));
}

@ -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) {
@ -176,7 +179,7 @@ public class RedissonSessionManager extends ManagerBase {
RedissonSession session = (RedissonSession) createEmptySession();
session.load(attrs);
session.setId(id);
session.setId(id, notify);
session.access();
session.endAccess();
@ -191,9 +194,10 @@ 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) {

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

@ -62,12 +62,15 @@ public class RedissonSession extends StandardSession {
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);
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<String, Boolean>());
@ -143,7 +146,11 @@ public class RedissonSession extends StandardSession {
map = redissonManager.getMap(id);
}
if (broadcastSessionEvents) {
map.expire(60, TimeUnit.SECONDS);
} else {
map.delete();
}
if (readMode == ReadMode.MEMORY) {
topic.publish(new AttributesClearMessage(redissonManager.getNodeId(), getId()));
}

@ -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) {
@ -175,7 +178,7 @@ public class RedissonSessionManager extends ManagerBase {
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
@ -290,6 +293,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) {

@ -62,12 +62,15 @@ public class RedissonSession extends StandardSession {
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);
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<String, Boolean>());
@ -143,7 +146,11 @@ public class RedissonSession extends StandardSession {
map = redissonManager.getMap(id);
}
if (broadcastSessionEvents) {
map.expire(60, TimeUnit.SECONDS);
} else {
map.delete();
}
if (readMode == ReadMode.MEMORY) {
topic.publish(new AttributesClearMessage(redissonManager.getNodeId(), getId()));
}

@ -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) {
@ -175,7 +178,7 @@ public class RedissonSessionManager extends ManagerBase {
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
@ -290,6 +293,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) {

Loading…
Cancel
Save