Class 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
    • 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
      • sessionMapLock

        private final Object sessionMapLock
    • 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. Use INFINITE_SESSION_LIFESPAN for 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. Use INFINITE_SESSION_LIFESPAN for 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.
      • 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.