Reputation: 2644
My question is regarding how Hazelcast removes expired sessions from the underlying map.
I am running a large Java web application on Tomcat which uses cookie based sessions. Requests go through Hazelcast's WebFilter. WebFilter is configured to connect to an external Hazelcast cluster and store sessions in a map. Here's a subset of the web.xml filter configuration.
<init-param>
<param-name>use-client</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>map-name</param-name>
<param-value>SessionMap</param-value>
</init-param>
In web.xml, my session-timeout
is set to 120 (i.e. 2 hours). The Hazelcast map, named SessionMap, has a TTL of 24 hours. The thinking here is that we want to end sessions that are inactive for 2 hours but if someone is continuously using the application we want to let the session to continue for a max of 24 hours.
The problem is that if I leave a session idle, Tomcat will terminate the session after 2 hours but the session will still remain in the Hazelcast map (SessionMap) until the TTL. Is there any way to have Hazelcast automatically remove sessions from the underlying map when they are expired in Tomcat?
The application can have a large number of sessions and very few will ever remain active for the full 24 hours so we don't want to leave sessions in Hazelcast's memory that are already expired.
I am using Hazelcast 3.8.
EDIT In Hazelcast documentation (http://docs.hazelcast.org/docs/3.5/manual/html/websessionreplication.html), it says:
Hazelcast automatically removes sessions from the cluster if the sessions are expired on the Web Container. This removal is done by com.hazelcast.web.SessionListener, which is an implementation of javax.servlet.http.HttpSessionListener.
Does this only apply if the Hazelcast cluster being used to store sessions is embedded? If so, is there a solution to achieve similar behavior when the sessions are stored in an external cluster?
Upvotes: 1
Views: 2117
Reputation: 1294
Whatever the setup you have mentioned looks to be correct. The SessionListener
should take care of removing the expired sessions after 2 hours and the map's ttl should take care of expiring the entry after 12 hours. You can remove the session-ttl-seconds
from your init-param
of web.xml as it doesn't have effect on Client/Server mode.
To confirm whether the expired sessions are being removed from map or not, you can implement a light weight MapEventListener.
IMap map = hzInstance.getMap("sesssion-map");
map.addEntryListener(new SessionMapEntryListener(), false);
Implement the MapEntry Listener. You can implement other interfaces as well based on the need. e.g., EntryAddedListener, EntryRemovedListener, EntryUpdatedListener, EntryEvictedListener
private static class SessionMapEntryListener implements MapListener, EntryExpiredListener {
@Override
public void entryExpired(EntryEvent entryEvent) {
System.out.println("EntryExpiredEvent triggered. Expired Entry = " + entryEvent.getKey());
}
}
Upvotes: 3