Class SessionManager<T>
- java.lang.Object
-
- org.marketcetera.util.ws.stateful.SessionManager<T>
-
public class SessionManager<T> extends Object
A session manager. It maps session IDs (SessionId) to session holders (SessionHolder), and (optionally) automatically expires sessions (map entries) which remain unused for a time interval that exceeds the session lifespan. This expiration is performed at regular intervals by a reaper thread, initiated during construction as part of the caller's thread group; the interval between scans is normally 5% of the session lifespan.- Since:
- 1.0.0
- Version:
- $Id: SessionManager.java 17411 2017-04-28 14:50:38Z colin $
- Author:
- tlerios@marketcetera.com
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description (package private) classSessionManager.ReaperThe reaper.
-
Field Summary
Fields Modifier and Type Field Description static longINFINITE_SESSION_LIFESPANA sentinel value for an infinite session lifespan (non-expiring sessions).static longMAX_SCAN_INTERVALThe maximum sleep interval between reaper scans, in ms.private HashMap<SessionId,SessionHolder<T>>mMapprivate NodeIdmServerIdprivate SessionFactory<T>mSessionFactoryprivate longmSessionLifeprivate ObjectsessionMapLock
-
Constructor Summary
Constructors Constructor Description SessionManager()Creates a new session manager whose sessions never expire.SessionManager(long sessionLife)Creates a new session manager whose sessions have the given lifespan, in ms.SessionManager(SessionFactory<T> sessionFactory)Creates a new session manager whose sessions are created by the given factory, and which never expire.SessionManager(SessionFactory<T> sessionFactory, long sessionLife)Creates a new session manager whose sessions are created by the given factory, and which have the given lifespan, in ms.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description SessionHolder<T>get(SessionId id)Returns the holder that the receiver associates with the given session ID.longgetLifespan()Returns the lifespan of the sessions managed by the receiver.protected Map<SessionId,SessionHolder<T>>getMap()Returns the receiver's map.NodeIdgetServerId()Returns the receiver's server ID.SessionFactory<T>getSessionFactory()Returns the receiver's session factory.voidput(SessionId id, SessionHolder<T> holder)Adds the given holder, associated with the given session ID, to the receiver.voidremove(SessionId id)Removes the holder that the receiver associates with the given session ID.(package private) voidsetServerId(NodeId serverId)Sets the receiver's server ID to the given one.
-
-
-
Field Detail
-
MAX_SCAN_INTERVAL
public static final long MAX_SCAN_INTERVAL
The maximum sleep interval between reaper scans, in ms.- See Also:
- Constant Field Values
-
INFINITE_SESSION_LIFESPAN
public static final long INFINITE_SESSION_LIFESPAN
A sentinel value for an infinite session lifespan (non-expiring sessions).- See Also:
- Constant Field Values
-
mServerId
private NodeId mServerId
-
mSessionLife
private final long mSessionLife
-
mSessionFactory
private final SessionFactory<T> mSessionFactory
-
sessionMapLock
private final Object sessionMapLock
-
mMap
private final HashMap<SessionId,SessionHolder<T>> mMap
-
-
Constructor Detail
-
SessionManager
public SessionManager(SessionFactory<T> sessionFactory, long sessionLife)
Creates a new session manager whose sessions are created by the given factory, and which have the given lifespan, in ms.- Parameters:
sessionFactory- The session factory. It may be null.sessionLife- The lifespan. UseINFINITE_SESSION_LIFESPANfor an infinite lifespan.
-
SessionManager
public SessionManager(long sessionLife)
Creates a new session manager whose sessions have the given lifespan, in ms.- Parameters:
sessionLife- The lifespan. UseINFINITE_SESSION_LIFESPANfor an infinite lifespan.
-
SessionManager
public SessionManager(SessionFactory<T> sessionFactory)
Creates a new session manager whose sessions are created by the given factory, and which never expire.- Parameters:
sessionFactory- The session factory. It may be null.
-
SessionManager
public SessionManager()
Creates a new session manager whose sessions never expire.
-
-
Method Detail
-
setServerId
void setServerId(NodeId serverId)
Sets the receiver's server ID to the given one.- Parameters:
serverId- The server ID, which may be null.
-
getServerId
public NodeId getServerId()
Returns the receiver's server ID.- Returns:
- The server ID, which may be null.
-
getSessionFactory
public SessionFactory<T> getSessionFactory()
Returns the receiver's session factory.- Returns:
- The factory. It may be null.
-
getLifespan
public long getLifespan()
Returns the lifespan of the sessions managed by the receiver.- Returns:
- The lifespan, in ms.
-
getMap
protected Map<SessionId,SessionHolder<T>> getMap()
Returns the receiver's map.- Returns:
- The map.
-
put
public void put(SessionId id, SessionHolder<T> holder)
Adds the given holder, associated with the given session ID, to the receiver. This addition counts as an access that renews the session's expiration counter.- Parameters:
id- The session ID.holder- The holder.
-
get
public SessionHolder<T> get(SessionId id)
Returns the holder that the receiver associates with the given session ID. This access renews the session's expiration counter.- Parameters:
id- The session ID.- Returns:
- The holder. It is null if there is no holder for the given ID.
-
remove
public void remove(SessionId id)
Removes the holder that the receiver associates with the given session ID. This method is a no-op if no such association exists.- Parameters:
id- The session ID.
-
-