Interface Session
- All Superinterfaces:
Map<Serializable,
Serializable>
- All Known Implementing Classes:
InMemorySession
Session objects are used to store data related to the browser session. Servers with a small number of clients usually keep session objects and the associated data in memory. Servers with a large number of clients may choose to only keep a LRU cache of session objects in memory and swap out (persist) other session objects. Therefore the keys and values stored using a session object must be serializable.
Occasionally, data associated with a session object is already
persisted anyway, because its lifetime is beyond that of a session.
To avoid persisting such data twice, the session provides a special
area for “transient data”. If components choose to store data
in this area, they must always check before use if the data is
available and recover it if necessary. Using this approach, the
components automatically profit from the LRU caching mechanism
provided by the session manager. As an alternative, components
can use the session id()
as key to manage the data
on their own.
Before removal, the SessionManager
calls the close()
method. The default implementation iterates through all values
in the transient data map and calls AutoCloseable.close()
for each one that implements the AutoCloseable
interface.
Implementations should override Object.hashCode()
and Object.equals(Object)
in such a way
that the session id is the only relevant attribute (cannot be
done by default methods of the interface).
Note that a browser can issue several requests related to the same
session in parallel. Implementations of this interface must
therefore ensure the thread safety of all methods provided by Map
.
Operations that require consistency across several operations should
synchronize on the session object.
-
Nested Class Summary
-
Method Summary
Modifier and TypeMethodDescriptiondefault void
close()
Iterates through all values in the transient data map and callsAutoCloseable.close()
for each that implements theAutoCloseable
interface.Returns the creation time stamp.static Session
from
(Associator associator) Obtains aSession
from anAssociator
.id()
Returns the session id.Returns the last used (referenced in request) time stamp.default Locale
locale()
Convenience method for retrieving the locale set byLanguageSelector
from the session.Return the storage area for transient data.void
Updates the last used time stamp.Methods inherited from interface java.util.Map
clear, compute, computeIfAbsent, computeIfPresent, containsKey, containsValue, entrySet, equals, forEach, get, getOrDefault, hashCode, isEmpty, keySet, merge, put, putAll, putIfAbsent, remove, remove, replace, replace, replaceAll, size, values
-
Method Details
-
from
Obtains aSession
from anAssociator
.The associator must be a
Request
(seeSessionManager.onRequest(org.jgrapes.http.events.Request.In)
) or a web socket channel (seeSessionManager.onProtocolSwitchAccepted(org.jgrapes.http.events.ProtocolSwitchAccepted, org.jgrapes.io.IOSubchannel)
). As the existence of an association can safely be assumed in handlers handlingRequest
s or handlers handling events on web socket channels, the method returnsSession
and notOptional<Session>
.The method should only be used in contexts where an existing association can be assumed.
- Parameters:
associator
- the associator- Returns:
- the session
-
id
Returns the session id.- Returns:
- the id
-
createdAt
Returns the creation time stamp.- Returns:
- the creation time stamp
-
lastUsedAt
Returns the last used (referenced in request) time stamp.- Returns:
- the last used timestamp
-
updateLastUsedAt
void updateLastUsedAt()Updates the last used time stamp. -
transientData
Return the storage area for transient data.Usually implemented by a
ConcurrentHashMap
. Other implementations must at least provide the same support for concurrency asConcurrentHashMap
.- Returns:
- the storage area
-
close
Iterates through all values in the transient data map and callsAutoCloseable.close()
for each that implements theAutoCloseable
interface.Any exceptions are ignored.
-
locale
Convenience method for retrieving the locale set byLanguageSelector
from the session.- Returns:
- the locale
-