public class HazelcastIndexedSessionRepository
extends java.lang.Object
implements org.springframework.session.FindByIndexNameSessionRepository<org.springframework.session.hazelcast.HazelcastIndexedSessionRepository.HazelcastSession>, com.hazelcast.map.listener.EntryAddedListener<java.lang.String,org.springframework.session.MapSession>, com.hazelcast.map.listener.EntryEvictedListener<java.lang.String,org.springframework.session.MapSession>, com.hazelcast.map.listener.EntryRemovedListener<java.lang.String,org.springframework.session.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);
HazelcastIndexedSessionRepository sessionRepository =
new HazelcastIndexedSessionRepository(hazelcastInstance);
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(HazelcastIndexedSessionRepository.PRINCIPAL_NAME_ATTRIBUTE)
.setExtractor(PrincipalNameExtractor.class.getName());
Config config = new Config();
config.getMapConfig(HazelcastIndexedSessionRepository.DEFAULT_SESSION_MAP_NAME)
.addMapAttributeConfig(attributeConfig)
.addMapIndexConfig(new MapIndexConfig(
HazelcastIndexedSessionRepository.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 java.lang.String |
DEFAULT_SESSION_MAP_NAME
The default name of map used by Spring Session to store sessions.
|
static java.lang.String |
PRINCIPAL_NAME_ATTRIBUTE
The principal name custom attribute name.
|
| Constructor and Description |
|---|
HazelcastIndexedSessionRepository(com.hazelcast.core.HazelcastInstance hazelcastInstance)
Create a new
HazelcastIndexedSessionRepository instance. |
| Modifier and Type | Method and Description |
|---|---|
void |
close() |
org.springframework.session.hazelcast.HazelcastIndexedSessionRepository.HazelcastSession |
createSession() |
void |
deleteById(java.lang.String id) |
void |
entryAdded(com.hazelcast.core.EntryEvent<java.lang.String,org.springframework.session.MapSession> event) |
void |
entryEvicted(com.hazelcast.core.EntryEvent<java.lang.String,org.springframework.session.MapSession> event) |
void |
entryRemoved(com.hazelcast.core.EntryEvent<java.lang.String,org.springframework.session.MapSession> event) |
org.springframework.session.hazelcast.HazelcastIndexedSessionRepository.HazelcastSession |
findById(java.lang.String id) |
java.util.Map<java.lang.String,org.springframework.session.hazelcast.HazelcastIndexedSessionRepository.HazelcastSession> |
findByIndexNameAndIndexValue(java.lang.String indexName,
java.lang.String indexValue) |
void |
init() |
void |
save(org.springframework.session.hazelcast.HazelcastIndexedSessionRepository.HazelcastSession session) |
void |
setApplicationEventPublisher(org.springframework.context.ApplicationEventPublisher applicationEventPublisher)
Sets the
ApplicationEventPublisher that is used to publish
session events. |
void |
setDefaultMaxInactiveInterval(java.lang.Integer defaultMaxInactiveInterval)
Set the maximum inactive interval in seconds between requests before newly created
sessions will be invalidated.
|
void |
setFlushMode(org.springframework.session.FlushMode flushMode)
Sets the Hazelcast flush mode.
|
void |
setIndexResolver(org.springframework.session.IndexResolver<org.springframework.session.Session> indexResolver)
Set the
IndexResolver to use. |
void |
setSaveMode(org.springframework.session.SaveMode saveMode)
Set the save mode.
|
void |
setSessionMapName(java.lang.String sessionMapName)
Set the name of map used to store sessions.
|
public static final java.lang.String DEFAULT_SESSION_MAP_NAME
public static final java.lang.String PRINCIPAL_NAME_ATTRIBUTE
public HazelcastIndexedSessionRepository(com.hazelcast.core.HazelcastInstance hazelcastInstance)
HazelcastIndexedSessionRepository instance.hazelcastInstance - the HazelcastInstance to use for managing sessions@PostConstruct public void init()
@PreDestroy public void close()
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(java.lang.Integer defaultMaxInactiveInterval)
defaultMaxInactiveInterval - the maximum inactive interval in secondspublic void setIndexResolver(org.springframework.session.IndexResolver<org.springframework.session.Session> indexResolver)
IndexResolver to use.indexResolver - the index resolverpublic void setSessionMapName(java.lang.String sessionMapName)
sessionMapName - the session map namepublic void setFlushMode(org.springframework.session.FlushMode flushMode)
FlushMode.ON_SAVE.flushMode - the new Hazelcast flush modepublic void setSaveMode(org.springframework.session.SaveMode saveMode)
saveMode - the save modepublic org.springframework.session.hazelcast.HazelcastIndexedSessionRepository.HazelcastSession createSession()
createSession in interface org.springframework.session.SessionRepository<org.springframework.session.hazelcast.HazelcastIndexedSessionRepository.HazelcastSession>public void save(org.springframework.session.hazelcast.HazelcastIndexedSessionRepository.HazelcastSession session)
save in interface org.springframework.session.SessionRepository<org.springframework.session.hazelcast.HazelcastIndexedSessionRepository.HazelcastSession>public org.springframework.session.hazelcast.HazelcastIndexedSessionRepository.HazelcastSession findById(java.lang.String id)
findById in interface org.springframework.session.SessionRepository<org.springframework.session.hazelcast.HazelcastIndexedSessionRepository.HazelcastSession>public void deleteById(java.lang.String id)
deleteById in interface org.springframework.session.SessionRepository<org.springframework.session.hazelcast.HazelcastIndexedSessionRepository.HazelcastSession>public java.util.Map<java.lang.String,org.springframework.session.hazelcast.HazelcastIndexedSessionRepository.HazelcastSession> findByIndexNameAndIndexValue(java.lang.String indexName,
java.lang.String indexValue)
findByIndexNameAndIndexValue in interface org.springframework.session.FindByIndexNameSessionRepository<org.springframework.session.hazelcast.HazelcastIndexedSessionRepository.HazelcastSession>public void entryAdded(com.hazelcast.core.EntryEvent<java.lang.String,org.springframework.session.MapSession> event)
entryAdded in interface com.hazelcast.map.listener.EntryAddedListener<java.lang.String,org.springframework.session.MapSession>public void entryEvicted(com.hazelcast.core.EntryEvent<java.lang.String,org.springframework.session.MapSession> event)
entryEvicted in interface com.hazelcast.map.listener.EntryEvictedListener<java.lang.String,org.springframework.session.MapSession>public void entryRemoved(com.hazelcast.core.EntryEvent<java.lang.String,org.springframework.session.MapSession> event)
entryRemoved in interface com.hazelcast.map.listener.EntryRemovedListener<java.lang.String,org.springframework.session.MapSession>