Interface AgentScheduler
This interface defines the fundamental operations for scheduling agents, including registering tasks with various scheduling configurations (cron expressions, fixed rates, fixed delays), managing task lifecycle (pause, resume, cancel), and querying scheduled agents.
Implementations can be based on different scheduling frameworks such as:
- Spring TaskScheduler - Lightweight implementation for simple scenarios
- Quartz Scheduler - Enterprise-grade implementation with persistence and clustering
- External schedulers - Integration with distributed scheduling systems (XXL-Job, etc.)
Design Principles:
- Task-centric - Returns
ScheduleAgentTaskfor task lifecycle management - Framework agnostic - Core interface can be implemented with any scheduling backend
- Extensible - Support for custom scheduling strategies through configuration
- Stateful - Track task execution history, metrics, and current status
Usage Example:
AgentScheduler scheduler = new XxlJobAgentScheduler(executor);
// Create agent configuration
AgentConfig agentConfig = AgentConfig.builder()
.name("MyAgent")
.model(model)
.toolkit(toolkit)
.sysPrompt("You are a helpful assistant")
.build();
// Create schedule configuration
ScheduleConfig scheduleConfig = ScheduleConfig.builder()
.cron("0 0 8 * * ?")
.build();
// Schedule the agent and get a task handle
ScheduleAgentTask task = scheduler.schedule(agentConfig, scheduleConfig);
// Control the task
task.run(); // Manual execution
task.cancel(); // Cancel scheduling
// Or cancel through scheduler
scheduler.cancel(task.getName());
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionbooleanCancel and remove a scheduled task permanently.Retrieve all scheduled tasks managed by this scheduler.getScheduledAgent(String name) Retrieve a scheduled task by its name.Get the type identifier of this scheduler implementation.schedule(AgentConfig agentConfig, ScheduleConfig scheduleConfig) Schedule an agent to run according to the specified configuration.default ScheduleAgentTaskschedule(AgentConfig agentConfig, ScheduleConfig scheduleConfig, io.agentscope.core.message.Msg input) Schedule an agent to run with an initial input message.voidshutdown()Gracefully shutdown the scheduler.
-
Method Details
-
schedule
Schedule an agent to run according to the specified configuration.This method accepts an
AgentConfigthat defines how to create fresh Agent instances. If you need to configure runtime objects like Toolkit and Hooks, pass aRuntimeAgentConfiginstance instead. Each time the scheduled task executes, a new Agent instance will be created, providing better state isolation and resource management.The returned
ScheduleAgentTaskprovides task control methods (run, cancel) and serves as a handle to manage the scheduled task. The agent name fromAgentConfig.getName()will be used as the identifier for retrieving the task later viagetScheduledAgent(String).Benefits:
- Each execution gets a fresh Agent with clean state
- No long-lived Agent instances consuming resources
- Configuration can be serialized and persisted (when using AgentConfig)
- Task lifecycle is independent of Agent lifecycle
- Parameters:
agentConfig- The agent configuration defining how to create agents (can be AgentConfig or RuntimeAgentConfig)scheduleConfig- The scheduling configuration including timing and execution policies- Returns:
- The ScheduleAgentTask handle for controlling the scheduled task
- Throws:
IllegalArgumentException- if agentConfig or scheduleConfig is null or invalid
-
schedule
default ScheduleAgentTask schedule(AgentConfig agentConfig, ScheduleConfig scheduleConfig, io.agentscope.core.message.Msg input) Schedule an agent to run with an initial input message.This method extends
schedule(AgentConfig, ScheduleConfig)by allowing developers to pass an initialMsgobject. When the scheduled task is triggered, the newly created Agent instance will be executed with this input message.This is particularly useful for tasks that require specific context, parameters, or initial instructions to begin their execution.
- Parameters:
agentConfig- The agent configuration defining how to create agents (can be AgentConfig or RuntimeAgentConfig)scheduleConfig- The scheduling configuration including timing and execution policiesinput- The initial message to pass to the agent when triggered (can be null)- Returns:
- The ScheduleAgentTask handle for controlling the scheduled task
- Throws:
UnsupportedOperationException- if the specific scheduler implementation does not support initial input
-
cancel
Cancel and remove a scheduled task permanently.This operation permanently removes the task from the scheduler. If the task is currently executing, the behavior depends on the scheduler implementation - it may wait for completion or attempt to interrupt the execution.
This method provides centralized control through the scheduler. Alternatively, you can call
ScheduleAgentTask.cancel()directly on the task.- Parameters:
name- The name of the scheduled task to cancel- Returns:
- true if the task was successfully cancelled, false if not found or already removed
-
getScheduledAgent
Retrieve a scheduled task by its name.The name is derived from
AgentConfig.getName()when the task was scheduled. This allows you to retrieve a task without keeping a reference to theScheduleAgentTaskreturned fromschedule(AgentConfig, ScheduleConfig).If multiple tasks with the same name are scheduled, the behavior depends on the implementation - it may return the first one, the most recently scheduled, or throw an exception.
- Parameters:
name- The name of the scheduled task- Returns:
- The ScheduleAgentTask if found, or null if no task with the given name is scheduled
-
getAllScheduleAgentTasks
List<ScheduleAgentTask> getAllScheduleAgentTasks()Retrieve all scheduled tasks managed by this scheduler.This operation returns all scheduled tasks regardless of their status (scheduled, running, paused, etc.). For large numbers of tasks, consider implementing pagination in scheduler implementations.
- Returns:
- A list of all ScheduleAgentTask instances
-
shutdown
void shutdown()Gracefully shutdown the scheduler.This operation stops accepting new scheduling requests and waits for currently executing agent tasks to complete. The maximum wait time depends on the implementation. After shutdown, no methods on this scheduler should be called.
For implementations that support persistence (e.g., Quartz with database store), task configurations may be preserved and can be recovered when the scheduler restarts.
-
getSchedulerType
String getSchedulerType()Get the type identifier of this scheduler implementation.This can be used to distinguish between different scheduler implementations (e.g., "spring", "quartz", "xxl-job") and for logging/monitoring purposes.
- Returns:
- The scheduler type identifier
-