Class ChatSessionManager

java.lang.Object
org.codelibs.fess.chat.ChatSessionManager

public class ChatSessionManager extends Object
Manager class for chat sessions. Sessions are stored in memory with automatic expiration.

Note: Sessions are stored in a local in-memory ConcurrentHashMap. In multi-instance deployments (e.g., behind a load balancer), sessions are not shared between instances. Use sticky sessions or an external session store if session affinity across instances is required.

Author:
FessProject
  • Constructor Details

    • ChatSessionManager

      public ChatSessionManager()
      Default constructor.
  • Method Details

    • init

      @PostConstruct public void init()
      Initializes the session manager and starts the cleanup scheduler.
    • destroy

      @PreDestroy public void destroy()
      Destroys the session manager and shuts down the cleanup scheduler.
    • createSession

      public ChatSession createSession(String userId)
      Creates a new chat session.
      Parameters:
      userId - the user ID (can be null for anonymous users)
      Returns:
      the created session
    • getSession

      public ChatSession getSession(String sessionId)
      Gets a session by ID.
      Parameters:
      sessionId - the session ID
      Returns:
      the session, or null if not found or expired
    • getOrCreateSession

      public ChatSession getOrCreateSession(String sessionId, String userId)
      Gets or creates a session.
      Parameters:
      sessionId - the session ID (can be null to create a new session)
      userId - the user ID
      Returns:
      the existing or new session
    • addMessage

      public boolean addMessage(String sessionId, ChatMessage message)
      Adds a message to a session.
      Parameters:
      sessionId - the session ID
      message - the message to add
      Returns:
      true if the message was added, false if the session was not found
    • clearSession

      public boolean clearSession(String sessionId, String userId)
      Clears the messages in a session with userId ownership check.
      Parameters:
      sessionId - the session ID
      userId - the user ID for ownership verification (can be null)
      Returns:
      true if the session was found, owned by the user, and cleared; false otherwise
    • clearSession

      public boolean clearSession(String sessionId)
      Clears the messages in a session without ownership check. Used for internal/admin operations where ownership verification is not needed.
      Parameters:
      sessionId - the session ID
      Returns:
      true if the session was found and cleared, false otherwise
    • removeSession

      public ChatSession removeSession(String sessionId)
      Removes a session.
      Parameters:
      sessionId - the session ID
      Returns:
      the removed session, or null if not found
    • cleanupExpiredSessions

      protected void cleanupExpiredSessions()
      Cleans up expired sessions.
    • enforceMaxSize

      protected void enforceMaxSize()
      Enforces the maximum session cache size.
    • isExpired

      protected boolean isExpired(ChatSession session)
      Checks if a session is expired.
      Parameters:
      session - the session to check
      Returns:
      true if the session is expired
    • getSessionTimeoutMinutes

      protected int getSessionTimeoutMinutes()
      Gets the session timeout in minutes.
      Returns:
      the session timeout in minutes
    • getMaxSessionSize

      protected int getMaxSessionSize()
      Gets the maximum session cache size.
      Returns:
      the maximum session cache size
    • getMaxHistoryMessages

      protected int getMaxHistoryMessages()
      Gets the maximum number of history messages to retain.
      Returns:
      the maximum number of history messages
    • getActiveSessionCount

      public int getActiveSessionCount()
      Gets the current number of active sessions.
      Returns:
      the number of active sessions