Package org.codelibs.fess.chat
Class ChatSessionManager
java.lang.Object
org.codelibs.fess.chat.ChatSessionManager
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 Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionbooleanaddMessage(String sessionId, ChatMessage message) Adds a message to a session.protected voidCleans up expired sessions.booleanclearSession(String sessionId) Clears the messages in a session without ownership check.booleanclearSession(String sessionId, String userId) Clears the messages in a session with userId ownership check.createSession(String userId) Creates a new chat session.voiddestroy()Destroys the session manager and shuts down the cleanup scheduler.protected voidEnforces the maximum session cache size.intGets the current number of active sessions.protected intGets the maximum number of history messages to retain.protected intGets the maximum session cache size.getOrCreateSession(String sessionId, String userId) Gets or creates a session.getSession(String sessionId) Gets a session by ID.protected intGets the session timeout in minutes.voidinit()Initializes the session manager and starts the cleanup scheduler.protected booleanisExpired(ChatSession session) Checks if a session is expired.removeSession(String sessionId) Removes a session.
-
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
Creates a new chat session.- Parameters:
userId- the user ID (can be null for anonymous users)- Returns:
- the created session
-
getSession
Gets a session by ID.- Parameters:
sessionId- the session ID- Returns:
- the session, or null if not found or expired
-
getOrCreateSession
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
Adds a message to a session.- Parameters:
sessionId- the session IDmessage- the message to add- Returns:
- true if the message was added, false if the session was not found
-
clearSession
Clears the messages in a session with userId ownership check.- Parameters:
sessionId- the session IDuserId- 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
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
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
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
-