Class HazelcastIndexedSessionRepository

java.lang.Object
org.springframework.session.hazelcast.HazelcastIndexedSessionRepository
All Implemented Interfaces:
com.hazelcast.map.listener.EntryAddedListener<String,org.springframework.session.MapSession>, com.hazelcast.map.listener.EntryEvictedListener<String,org.springframework.session.MapSession>, com.hazelcast.map.listener.EntryRemovedListener<String,org.springframework.session.MapSession>, com.hazelcast.map.listener.MapListener, EventListener, org.springframework.session.FindByIndexNameSessionRepository<org.springframework.session.hazelcast.HazelcastIndexedSessionRepository.HazelcastSession>, org.springframework.session.SessionRepository<org.springframework.session.hazelcast.HazelcastIndexedSessionRepository.HazelcastSession>
Direct Known Subclasses:
HazelcastSessionRepository

public class HazelcastIndexedSessionRepository extends Object implements org.springframework.session.FindByIndexNameSessionRepository<org.springframework.session.hazelcast.HazelcastIndexedSessionRepository.HazelcastSession>, com.hazelcast.map.listener.EntryAddedListener<String,org.springframework.session.MapSession>, com.hazelcast.map.listener.EntryEvictedListener<String,org.springframework.session.MapSession>, com.hazelcast.map.listener.EntryRemovedListener<String,org.springframework.session.MapSession>
A 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.
  • entryAdded - SessionCreatedEvent
  • entryEvicted - SessionExpiredEvent
  • entryRemoved - SessionDeletedEvent
Since:
2.2.0
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final String
    The default name of map used by Spring Session to store sessions.
    static final String
    The principal name custom attribute name.

    Fields inherited from interface org.springframework.session.FindByIndexNameSessionRepository

    PRINCIPAL_NAME_INDEX_NAME
  • Constructor Summary

    Constructors
    Constructor
    Description
    HazelcastIndexedSessionRepository(com.hazelcast.core.HazelcastInstance hazelcastInstance)
    Create a new HazelcastIndexedSessionRepository instance.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
     
    org.springframework.session.hazelcast.HazelcastIndexedSessionRepository.HazelcastSession
     
    void
     
    void
    entryAdded(com.hazelcast.core.EntryEvent<String,org.springframework.session.MapSession> event)
     
    void
    entryEvicted(com.hazelcast.core.EntryEvent<String,org.springframework.session.MapSession> event)
     
    void
    entryRemoved(com.hazelcast.core.EntryEvent<String,org.springframework.session.MapSession> event)
     
    org.springframework.session.hazelcast.HazelcastIndexedSessionRepository.HazelcastSession
     
    Map<String,org.springframework.session.hazelcast.HazelcastIndexedSessionRepository.HazelcastSession>
    findByIndexNameAndIndexValue(String indexName, String indexValue)
     
    void
     
    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(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(String sessionMapName)
    Set the name of map used to store sessions.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

    Methods inherited from interface org.springframework.session.FindByIndexNameSessionRepository

    findByPrincipalName
  • Field Details

    • DEFAULT_SESSION_MAP_NAME

      public static final String DEFAULT_SESSION_MAP_NAME
      The default name of map used by Spring Session to store sessions.
      See Also:
    • PRINCIPAL_NAME_ATTRIBUTE

      public static final String PRINCIPAL_NAME_ATTRIBUTE
      The principal name custom attribute name.
      See Also:
  • Constructor Details

    • HazelcastIndexedSessionRepository

      public HazelcastIndexedSessionRepository(com.hazelcast.core.HazelcastInstance hazelcastInstance)
      Create a new HazelcastIndexedSessionRepository instance.
      Parameters:
      hazelcastInstance - the HazelcastInstance to use for managing sessions
  • Method Details

    • init

      @PostConstruct public void init()
    • close

      @PreDestroy public void close()
    • setApplicationEventPublisher

      public void setApplicationEventPublisher(org.springframework.context.ApplicationEventPublisher applicationEventPublisher)
      Sets the ApplicationEventPublisher that is used to publish session events. The default is to not publish session events.
      Parameters:
      applicationEventPublisher - the ApplicationEventPublisher that is used to publish session events. Cannot be null.
    • setDefaultMaxInactiveInterval

      public void setDefaultMaxInactiveInterval(Integer defaultMaxInactiveInterval)
      Set the maximum inactive interval in seconds between requests before newly created sessions will be invalidated. A negative time indicates that the session will never timeout. The default is 1800 (30 minutes).
      Parameters:
      defaultMaxInactiveInterval - the maximum inactive interval in seconds
    • setIndexResolver

      public void setIndexResolver(org.springframework.session.IndexResolver<org.springframework.session.Session> indexResolver)
      Set the IndexResolver to use.
      Parameters:
      indexResolver - the index resolver
    • setSessionMapName

      public void setSessionMapName(String sessionMapName)
      Set the name of map used to store sessions.
      Parameters:
      sessionMapName - the session map name
    • setFlushMode

      public void setFlushMode(org.springframework.session.FlushMode flushMode)
      Sets the Hazelcast flush mode. Default flush mode is FlushMode.ON_SAVE.
      Parameters:
      flushMode - the new Hazelcast flush mode
    • setSaveMode

      public void setSaveMode(org.springframework.session.SaveMode saveMode)
      Set the save mode.
      Parameters:
      saveMode - the save mode
    • createSession

      public org.springframework.session.hazelcast.HazelcastIndexedSessionRepository.HazelcastSession createSession()
      Specified by:
      createSession in interface org.springframework.session.SessionRepository<org.springframework.session.hazelcast.HazelcastIndexedSessionRepository.HazelcastSession>
    • save

      public void save(org.springframework.session.hazelcast.HazelcastIndexedSessionRepository.HazelcastSession session)
      Specified by:
      save in interface org.springframework.session.SessionRepository<org.springframework.session.hazelcast.HazelcastIndexedSessionRepository.HazelcastSession>
    • findById

      public org.springframework.session.hazelcast.HazelcastIndexedSessionRepository.HazelcastSession findById(String id)
      Specified by:
      findById in interface org.springframework.session.SessionRepository<org.springframework.session.hazelcast.HazelcastIndexedSessionRepository.HazelcastSession>
    • deleteById

      public void deleteById(String id)
      Specified by:
      deleteById in interface org.springframework.session.SessionRepository<org.springframework.session.hazelcast.HazelcastIndexedSessionRepository.HazelcastSession>
    • findByIndexNameAndIndexValue

      public Map<String,org.springframework.session.hazelcast.HazelcastIndexedSessionRepository.HazelcastSession> findByIndexNameAndIndexValue(String indexName, String indexValue)
      Specified by:
      findByIndexNameAndIndexValue in interface org.springframework.session.FindByIndexNameSessionRepository<org.springframework.session.hazelcast.HazelcastIndexedSessionRepository.HazelcastSession>
    • entryAdded

      public void entryAdded(com.hazelcast.core.EntryEvent<String,org.springframework.session.MapSession> event)
      Specified by:
      entryAdded in interface com.hazelcast.map.listener.EntryAddedListener<String,org.springframework.session.MapSession>
    • entryEvicted

      public void entryEvicted(com.hazelcast.core.EntryEvent<String,org.springframework.session.MapSession> event)
      Specified by:
      entryEvicted in interface com.hazelcast.map.listener.EntryEvictedListener<String,org.springframework.session.MapSession>
    • entryRemoved

      public void entryRemoved(com.hazelcast.core.EntryEvent<String,org.springframework.session.MapSession> event)
      Specified by:
      entryRemoved in interface com.hazelcast.map.listener.EntryRemovedListener<String,org.springframework.session.MapSession>