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
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionint
Get the number of currently registered sessions.boolean
Register the current Vaadin session.void
setMaxSessions
(int maxSessions) Configure the maximum number of allowed sessions.<T> Map<VaadinSession,
T> surveySessions
(Function<? super VaadinSession, ? extends T> extractor) Survey all registeredVaadinSession
s and extract some info from each one.boolean
unregisterSession
(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
- ifmaxSessions
is 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
- ifmaxSessions
is 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
VaadinSession
and be holding its lock.If the session is already registered, or in the
VaadinSessionState.CLOSING
state, 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 noVaadinSession
associated with the current threadIllegalStateException
- if the currentVaadinSession
is not lockedIllegalArgumentException
- theVaadinSession
is 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
- ifsession
is null
-
surveySessions
public <T> Map<VaadinSession,T> surveySessions(Function<? super VaadinSession, ? extends T> extractor) throws InterruptedExceptionSurvey all registeredVaadinSession
s and extract some info from each one.This method guarantees that each session is locked while
extractor
is 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
VaadinSession
to the corresponding extracted information - Throws:
IllegalArgumentException
- ifextractor
is nullIllegalStateException
- if there is a lockedVaadinSession
associated with the current threadInterruptedException
- if interrupted while waiting to lock a session
-