public class HazelcastSessionRepository extends Object implements FindByIndexNameSessionRepository<org.springframework.session.hazelcast.HazelcastSessionRepository.HazelcastSession>, com.hazelcast.map.listener.EntryAddedListener<String,MapSession>, com.hazelcast.map.listener.EntryEvictedListener<String,MapSession>, com.hazelcast.map.listener.EntryRemovedListener<String,MapSession>
SessionRepository implementation that stores
sessions in Hazelcast's distributed IMap.
An example of how to create a new instance can be seen below:
Config config = new Config();
// ... configure Hazelcast ...
HazelcastInstance hazelcastInstance = Hazelcast.newHazelcastInstance(config);
IMap<String, MapSession> sessions = hazelcastInstance
.getMap("spring:session:sessions");
HazelcastSessionRepository sessionRepository =
new HazelcastSessionRepository(sessions);
In order to support finding sessions by principal name using
findByIndexNameAndIndexValue(String, String) method, custom configuration of
IMap supplied to this implementation is required.
The following snippet demonstrates how to define required configuration using
programmatic Hazelcast Configuration:
MapAttributeConfig attributeConfig = new MapAttributeConfig()
.setName(HazelcastSessionRepository.PRINCIPAL_NAME_ATTRIBUTE)
.setExtractor(PrincipalNameExtractor.class.getName());
Config config = new Config();
config.getMapConfig("spring:session:sessions")
.addMapAttributeConfig(attributeConfig)
.addMapIndexConfig(new MapIndexConfig(
HazelcastSessionRepository.PRINCIPAL_NAME_ATTRIBUTE, false));
Hazelcast.newHazelcastInstance(config);
This implementation listens for events on the Hazelcast-backed SessionRepository and
translates those events into the corresponding Spring Session events. Publish the
Spring Session events with the given ApplicationEventPublisher.
SessionCreatedEventSessionExpiredEventSessionDeletedEvent| Modifier and Type | Field and Description |
|---|---|
static String |
PRINCIPAL_NAME_ATTRIBUTE
The principal name custom attribute name.
|
PRINCIPAL_NAME_INDEX_NAME| Constructor and Description |
|---|
HazelcastSessionRepository(com.hazelcast.core.IMap<String,MapSession> sessions) |
| Modifier and Type | Method and Description |
|---|---|
org.springframework.session.hazelcast.HazelcastSessionRepository.HazelcastSession |
createSession()
Creates a new
Session that is capable of being persisted by this
SessionRepository. |
void |
delete(String id)
|
void |
entryAdded(com.hazelcast.core.EntryEvent<String,MapSession> event) |
void |
entryEvicted(com.hazelcast.core.EntryEvent<String,MapSession> event) |
void |
entryRemoved(com.hazelcast.core.EntryEvent<String,MapSession> event) |
Map<String,org.springframework.session.hazelcast.HazelcastSessionRepository.HazelcastSession> |
findByIndexNameAndIndexValue(String indexName,
String indexValue)
Find a Map of the session id to the
Session of all sessions that contain
the session attribute with the name
FindByIndexNameSessionRepository.PRINCIPAL_NAME_INDEX_NAME and the value of
the specified principal name. |
org.springframework.session.hazelcast.HazelcastSessionRepository.HazelcastSession |
getSession(String id)
|
void |
save(org.springframework.session.hazelcast.HazelcastSessionRepository.HazelcastSession session)
Ensures the
Session created by
SessionRepository.createSession() is saved. |
void |
setApplicationEventPublisher(org.springframework.context.ApplicationEventPublisher applicationEventPublisher)
Sets the
ApplicationEventPublisher that is used to publish
session events. |
void |
setDefaultMaxInactiveInterval(Integer defaultMaxInactiveInterval)
Set the maximum inactive interval in seconds between requests before newly created
sessions will be invalidated.
|
void |
setHazelcastFlushMode(HazelcastFlushMode hazelcastFlushMode)
Sets the Hazelcast flush mode.
|
public static final String PRINCIPAL_NAME_ATTRIBUTE
public HazelcastSessionRepository(com.hazelcast.core.IMap<String,MapSession> sessions)
public void setApplicationEventPublisher(org.springframework.context.ApplicationEventPublisher applicationEventPublisher)
ApplicationEventPublisher that is used to publish
session events. The default is to not publish session
events.applicationEventPublisher - the ApplicationEventPublisher that is used
to publish session events. Cannot be null.public void setDefaultMaxInactiveInterval(Integer defaultMaxInactiveInterval)
defaultMaxInactiveInterval - the maximum inactive interval in secondspublic void setHazelcastFlushMode(HazelcastFlushMode hazelcastFlushMode)
HazelcastFlushMode.ON_SAVE.hazelcastFlushMode - the new Hazelcast flush modepublic org.springframework.session.hazelcast.HazelcastSessionRepository.HazelcastSession createSession()
SessionRepositorySession that is capable of being persisted by this
SessionRepository.
This allows optimizations and customizations in how the Session is
persisted. For example, the implementation returned might keep track of the changes
ensuring that only the delta needs to be persisted on a save.
createSession in interface SessionRepository<org.springframework.session.hazelcast.HazelcastSessionRepository.HazelcastSession>Session that is capable of being persisted by this
SessionRepositorypublic void save(org.springframework.session.hazelcast.HazelcastSessionRepository.HazelcastSession session)
SessionRepositorySession created by
SessionRepository.createSession() is saved.
Some implementations may choose to save as the Session is updated by
returning a Session that immediately persists any changes. In this case,
this method may not actually do anything.
save in interface SessionRepository<org.springframework.session.hazelcast.HazelcastSessionRepository.HazelcastSession>session - the Session to savepublic org.springframework.session.hazelcast.HazelcastSessionRepository.HazelcastSession getSession(String id)
SessionRepositorygetSession in interface SessionRepository<org.springframework.session.hazelcast.HazelcastSessionRepository.HazelcastSession>id - the Session.getId() to lookupSession by the Session.getId() or null if no
Session is found.public void delete(String id)
SessionRepositorydelete in interface SessionRepository<org.springframework.session.hazelcast.HazelcastSessionRepository.HazelcastSession>id - the Session.getId() to deletepublic Map<String,org.springframework.session.hazelcast.HazelcastSessionRepository.HazelcastSession> findByIndexNameAndIndexValue(String indexName, String indexValue)
FindByIndexNameSessionRepositorySession of all sessions that contain
the session attribute with the name
FindByIndexNameSessionRepository.PRINCIPAL_NAME_INDEX_NAME and the value of
the specified principal name.findByIndexNameAndIndexValue in interface FindByIndexNameSessionRepository<org.springframework.session.hazelcast.HazelcastSessionRepository.HazelcastSession>indexName - the name if the index (i.e.
FindByIndexNameSessionRepository.PRINCIPAL_NAME_INDEX_NAME)indexValue - the value of the index to search for.Session of all sessions
that contain the session specified index name and the value of the specified index
name. If no results are found, an empty Map is returned.public void entryAdded(com.hazelcast.core.EntryEvent<String,MapSession> event)
entryAdded in interface com.hazelcast.map.listener.EntryAddedListener<String,MapSession>public void entryEvicted(com.hazelcast.core.EntryEvent<String,MapSession> event)
entryEvicted in interface com.hazelcast.map.listener.EntryEvictedListener<String,MapSession>public void entryRemoved(com.hazelcast.core.EntryEvent<String,MapSession> event)
entryRemoved in interface com.hazelcast.map.listener.EntryRemovedListener<String,MapSession>