Class HazelcastIndexedSessionRepository

java.lang.Object
org.springframework.session.hazelcast.HazelcastIndexedSessionRepository
All Implemented Interfaces:
com.hazelcast.map.listener.EntryAddedListener<String,MapSession>, com.hazelcast.map.listener.EntryEvictedListener<String,MapSession>, com.hazelcast.map.listener.EntryExpiredListener<String,MapSession>, com.hazelcast.map.listener.EntryRemovedListener<String,MapSession>, com.hazelcast.map.listener.MapListener, EventListener, org.springframework.beans.factory.DisposableBean, org.springframework.beans.factory.InitializingBean, FindByIndexNameSessionRepository<org.springframework.session.hazelcast.HazelcastIndexedSessionRepository.HazelcastSession>, SessionRepository<org.springframework.session.hazelcast.HazelcastIndexedSessionRepository.HazelcastSession>

public class HazelcastIndexedSessionRepository extends Object implements FindByIndexNameSessionRepository<org.springframework.session.hazelcast.HazelcastIndexedSessionRepository.HazelcastSession>, com.hazelcast.map.listener.EntryAddedListener<String,MapSession>, com.hazelcast.map.listener.EntryEvictedListener<String,MapSession>, com.hazelcast.map.listener.EntryRemovedListener<String,MapSession>, com.hazelcast.map.listener.EntryExpiredListener<String,MapSession>, org.springframework.beans.factory.InitializingBean, org.springframework.beans.factory.DisposableBean
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:
 AttributeConfig attributeConfig = new AttributeConfig()
         .setName(HazelcastIndexedSessionRepository.PRINCIPAL_NAME_ATTRIBUTE)
         .setExtractorClassName(rincipalNameExtractor.class.getName());

 Config config = new Config();

 config.getMapConfig(HazelcastIndexedSessionRepository.DEFAULT_SESSION_MAP_NAME)
         .addAttributeConfig(attributeConfig)
         .addIndexConfig(new IndexConfig(
                 IndexType.HASH,
                 HazelcastIndexedSessionRepository.PRINCIPAL_NAME_ATTRIBUTE));

 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.
Since:
2.2.0
  • 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

    • afterPropertiesSet

      public void afterPropertiesSet()
      Specified by:
      afterPropertiesSet in interface org.springframework.beans.factory.InitializingBean
    • destroy

      public void destroy()
      Specified by:
      destroy in interface org.springframework.beans.factory.DisposableBean
    • 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(Duration 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 time out. The default is 30 minutes.
      Parameters:
      defaultMaxInactiveInterval - the default maxInactiveInterval
    • setDefaultMaxInactiveInterval

      @Deprecated(since="3.0.0") public void setDefaultMaxInactiveInterval(Integer defaultMaxInactiveInterval)
      Deprecated.
      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 time out. The default is 1800 (30 minutes).
      Parameters:
      defaultMaxInactiveInterval - the default maxInactiveInterval in seconds
    • setIndexResolver

      public void setIndexResolver(IndexResolver<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(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(SaveMode saveMode)
      Set the save mode.
      Parameters:
      saveMode - the save mode
    • createSession

      public org.springframework.session.hazelcast.HazelcastIndexedSessionRepository.HazelcastSession createSession()
      Description copied from interface: SessionRepository
      Creates a new Session 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.

      Specified by:
      createSession in interface SessionRepository<org.springframework.session.hazelcast.HazelcastIndexedSessionRepository.HazelcastSession>
      Returns:
      a new Session that is capable of being persisted by this SessionRepository
    • save

      public void save(org.springframework.session.hazelcast.HazelcastIndexedSessionRepository.HazelcastSession session)
      Description copied from interface: SessionRepository
      Ensures the Session 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.

      Specified by:
      save in interface SessionRepository<org.springframework.session.hazelcast.HazelcastIndexedSessionRepository.HazelcastSession>
      Parameters:
      session - the Session to save
    • findById

      public org.springframework.session.hazelcast.HazelcastIndexedSessionRepository.HazelcastSession findById(String id)
      Description copied from interface: SessionRepository
      Gets the Session by the Session.getId() or null if no Session is found.
      Specified by:
      findById in interface SessionRepository<org.springframework.session.hazelcast.HazelcastIndexedSessionRepository.HazelcastSession>
      Parameters:
      id - the Session.getId() to lookup
      Returns:
      the Session by the Session.getId() or null if no Session is found.
    • deleteById

      public void deleteById(String id)
      Description copied from interface: SessionRepository
      Deletes the Session with the given Session.getId() or does nothing if the Session is not found.
      Specified by:
      deleteById in interface SessionRepository<org.springframework.session.hazelcast.HazelcastIndexedSessionRepository.HazelcastSession>
      Parameters:
      id - the Session.getId() to delete
    • findByIndexNameAndIndexValue

      public Map<String,org.springframework.session.hazelcast.HazelcastIndexedSessionRepository.HazelcastSession> findByIndexNameAndIndexValue(String indexName, String indexValue)
      Description copied from interface: FindByIndexNameSessionRepository
      Find a Map of the session id to the Session of all sessions that contain the specified index name index value.
      Specified by:
      findByIndexNameAndIndexValue in interface FindByIndexNameSessionRepository<org.springframework.session.hazelcast.HazelcastIndexedSessionRepository.HazelcastSession>
      Parameters:
      indexName - the name of the index (i.e. FindByIndexNameSessionRepository.PRINCIPAL_NAME_INDEX_NAME)
      indexValue - the value of the index to search for.
      Returns:
      a Map (never null) of the session id to the Session of all sessions that contain the specified index name and index value. If no results are found, an empty Map is returned.
    • entryAdded

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

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

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

      public void entryExpired(com.hazelcast.core.EntryEvent<String,MapSession> event)
      Specified by:
      entryExpired in interface com.hazelcast.map.listener.EntryExpiredListener<String,MapSession>
    • setSessionIdGenerator

      public void setSessionIdGenerator(SessionIdGenerator sessionIdGenerator)
      Set the SessionIdGenerator to use to generate session ids.
      Parameters:
      sessionIdGenerator - the SessionIdGenerator to use
      Since:
      3.2