Package com.tnsai.agents.lifecycle
Class IdleMonitor
java.lang.Object
com.tnsai.agents.lifecycle.IdleMonitor
Monitors agent idle time and triggers callbacks when timeout is reached.
IdleMonitor implements the idleTimeoutMs parameter from
AgentSpec, allowing agents to be
notified (and optionally suspended) when they've been idle for too long.
Key Features
- Per-agent idle timer management
- Timer reset on agent activity
- Configurable timeout callbacks
- Thread-safe operation
Usage Example
IdleMonitor monitor = IdleMonitor.getInstance();
// Start monitoring with callback
monitor.startMonitoring(agent, 30000, () -> {
agent.suspend(); // or any other idle action
});
// Reset timer on activity
monitor.resetTimer(agent);
// Stop monitoring when done
monitor.stopMonitoring(agent);
Integration with Agent Lifecycle
The Agent class automatically integrates with IdleMonitor:
- Monitoring starts during agent initialization if
idleTimeoutMs > 0 - Timer is reset on each
chat(),executeAction(), or message receipt - Monitoring stops when agent is stopped
- Author:
- TnsAI Team
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionvoidclear()Clears all monitoring (for testing).longgetIdleTime(Agent agent) Gets the time since last activity for an agent.static IdleMonitorReturns the singleton instance.intGets the number of agents being monitored.intgetTimeout(Agent agent) Gets the configured timeout for an agent.booleanisMonitoring(Agent agent) Checks if an agent is being monitored.voidresetTimer(Agent agent) Resets the idle timer for an agent.voidshutdown()Shuts down the scheduler.voidstartMonitoring(Agent agent, int timeoutMs, Runnable onIdle) Starts monitoring an agent for idle timeout.voidstopMonitoring(Agent agent) Stops monitoring an agent.
-
Method Details
-
getInstance
Returns the singleton instance.- Returns:
- the IdleMonitor instance
-
startMonitoring
Starts monitoring an agent for idle timeout.If the agent is already being monitored, the existing timer is cancelled and a new one is started with the new settings.
- Parameters:
agent- the agent to monitortimeoutMs- the idle timeout in millisecondsonIdle- callback to execute when idle timeout is reached- Throws:
IllegalArgumentException- if agent is null or timeoutMs invalid input: '<'= 0
-
resetTimer
Resets the idle timer for an agent.This should be called whenever the agent performs any activity (chat, action execution, message handling, etc.).
- Parameters:
agent- the agent
-
stopMonitoring
Stops monitoring an agent.- Parameters:
agent- the agent
-
isMonitoring
Checks if an agent is being monitored.- Parameters:
agent- the agent- Returns:
- true if monitoring is active
-
getIdleTime
Gets the time since last activity for an agent.- Parameters:
agent- the agent- Returns:
- milliseconds since last activity, or -1 if not monitored
-
getTimeout
Gets the configured timeout for an agent.- Parameters:
agent- the agent- Returns:
- timeout in milliseconds, or -1 if not monitored
-
shutdown
public void shutdown()Shuts down the scheduler.Called during application shutdown. All monitoring stops.
-
getMonitoredCount
public int getMonitoredCount()Gets the number of agents being monitored.- Returns:
- count of monitored agents
-
clear
public void clear()Clears all monitoring (for testing).
-