Class VaadinSessionTracker
VaadinSession's in a Vaadin application and provides
a thread-safe way to poll them for information.
The application is responsible for registering new sessions via registerCurrentSession().
This should happen as soon as possible, e.g., on a login screen. If that method returns false, then the maximum
number of allowed sessions has already been reached, and so the application should take appropriate action, e.g.,
display an error message and invalidate the new session.
Sessions are automatically unregistered when they close.
All of the currently registered sessions can be surveyed via surveySessions().
This class does not care if the registered sessions come from multiple Vaadin servlets; if there are multiple servlets, all of the registered sessions will end up being tracked and counted together in one common pool.
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionintGet the number of currently registered sessions.booleanRegister the current Vaadin session.voidsetMaxSessions(int maxSessions) Configure the maximum number of allowed sessions.<T> Map<VaadinSession,T> surveySessions(Function<? super VaadinSession, ? extends T> extractor) Survey all registeredVaadinSessions and extract some info from each one.booleanunregisterSession(VaadinSession session) Manually unregister a session.
-
Field Details
-
log
-
-
Constructor Details
-
VaadinSessionTracker
public VaadinSessionTracker()Default constructor.This instance will be configured with no limit on the number of sessions.
-
VaadinSessionTracker
public VaadinSessionTracker(int maxSessions) Constructor.- Parameters:
maxSessions- maximum number of allowed sessions, or zero for infinity- Throws:
IllegalArgumentException- ifmaxSessionsis negative
-
-
Method Details
-
setMaxSessions
public void setMaxSessions(int maxSessions) Configure the maximum number of allowed sessions.Default is zero, which means no limit.
- Parameters:
maxSessions- maximum number of allowed sessions, or zero for infinity- Throws:
IllegalArgumentException- ifmaxSessionsis negative
-
getNumSessions
public int getNumSessions()Get the number of currently registered sessions.- Returns:
- number of registered sessions
-
registerCurrentSession
public boolean registerCurrentSession()Register the current Vaadin session.The current thread must have an associated
VaadinSessionand be holding its lock.If the session is already registered, or in the
VaadinSessionState.CLOSINGstate, nothing happens and this method returns true. Otherwise the session is added if there is room, otherwise false is returned.- Returns:
- true if the new total number of sessions does not exceed the maximum number allowed, otherwise false
- Throws:
IllegalStateException- if there is noVaadinSessionassociated with the current threadIllegalStateException- if the currentVaadinSessionis not lockedIllegalArgumentException- theVaadinSessionis in stateVaadinSessionState.CLOSED
-
unregisterSession
Manually unregister a session.It is not necessary to explicitly call this method; sessions are unregistered automatically on close.
- Parameters:
session-- Returns:
- true if session was unregistered, false if session was already not registered
- Throws:
IllegalArgumentException- ifsessionis null
-
surveySessions
public <T> Map<VaadinSession,T> surveySessions(Function<? super VaadinSession, ? extends T> extractor) throws InterruptedExceptionSurvey all registeredVaadinSessions and extract some info from each one.This method guarantees that each session is locked while
extractoris extracting from it.This method must not be invoked with a current and locked
VaadinSession.- Parameters:
extractor- returns info about a session (may be null)- Returns:
- mapping from
VaadinSessionto the corresponding extracted information - Throws:
IllegalArgumentException- ifextractoris nullIllegalStateException- if there is a lockedVaadinSessionassociated with the current threadInterruptedException- if interrupted while waiting to lock a session
-