Class QuartzAgentScheduler
- All Implemented Interfaces:
io.agentscope.extensions.scheduler.AgentScheduler
This scheduler integrates with Quartz to provide robust task scheduling capabilities for agents within the application. It supports various scheduling modes and lifecycle management.
Key Features:
- In-memory or persistent scheduling (configurable via Quartz properties)
- Support for cron expressions, fixed rate, and fixed delay execution
- Full lifecycle management: schedule, pause, resume, cancel
- Thread-safe task management
Prerequisites:
- Quartz dependencies must be on the classpath
- Proper configuration of Quartz properties if using persistence
Usage Example:
// 1. Create scheduler
AgentScheduler scheduler = QuartzAgentScheduler.builder()
.autoStart(true)
.build();
// 2. Create agent configuration
AgentConfig agentConfig = AgentConfig.builder()
.name("MyAgent")
.description("My scheduled agent")
.agentFactory(() -> ReActAgent.builder()
.name("MyAgent")
.model(model)
.toolkit(toolkit)
.build())
.build();
// 3. Configure schedule
ScheduleConfig scheduleConfig = ScheduleConfig.builder()
.scheduleMode(ScheduleMode.CRON)
.cronExpression("0 0 8 * * ?") // Run at 8:00 AM every day
.build();
// 4. Schedule the agent
ScheduleAgentTask task = scheduler.schedule(agentConfig, scheduleConfig);
// 5. Manage task
scheduler.pause("MyAgent");
scheduler.resume("MyAgent");
scheduler.cancel("MyAgent");
Important Notes:
- Uses a
QuartzAgentSchedulerRegistryto bridge Quartz Jobs with Scheduler instances - Supports
FIXED_DELAYmode by manually rescheduling triggers after job completion - Each scheduler instance has a unique ID to support multiple schedulers in the same JVM
- See Also:
-
AgentSchedulerScheduleAgentTask
-
Nested Class Summary
Nested Classes -
Method Summary
Modifier and TypeMethodDescriptionstatic QuartzAgentScheduler.Builderbuilder()Create a new Builder instance for QuartzAgentScheduler.booleanCancel a scheduled agent task.List<io.agentscope.extensions.scheduler.ScheduleAgentTask>Retrieve all scheduled agent tasks.io.agentscope.extensions.scheduler.quartz.QuartzScheduleAgentTaskgetScheduledAgent(String name) Retrieve a scheduled agent task by name.org.quartz.Trigger.TriggerStateGet the current status of a scheduled task.booleanInterrupt a running task.booleanPause a scheduled task.booleanResume a paused task.io.agentscope.extensions.scheduler.ScheduleAgentTaskschedule(io.agentscope.extensions.scheduler.config.AgentConfig agentConfig) Schedule an agent execution based on the provided configuration with default schedule.io.agentscope.extensions.scheduler.ScheduleAgentTaskschedule(io.agentscope.extensions.scheduler.config.AgentConfig agentConfig, io.agentscope.extensions.scheduler.config.ScheduleConfig scheduleConfig) Schedule an agent execution based on the provided configuration with no initial input.io.agentscope.extensions.scheduler.ScheduleAgentTaskschedule(io.agentscope.extensions.scheduler.config.AgentConfig agentConfig, io.agentscope.extensions.scheduler.config.ScheduleConfig scheduleConfig, io.agentscope.core.message.Msg input) Schedule an agent execution based on the provided configuration and initial input message.voidshutdown()Shutdown the scheduler.
-
Method Details
-
builder
Create a new Builder instance for QuartzAgentScheduler.- Returns:
- A new Builder
-
schedule
public io.agentscope.extensions.scheduler.ScheduleAgentTask schedule(io.agentscope.extensions.scheduler.config.AgentConfig agentConfig) Schedule an agent execution based on the provided configuration with default schedule.- Parameters:
agentConfig- The configuration for the agent to be scheduled- Returns:
- A task handle for the scheduled agent
-
schedule
public io.agentscope.extensions.scheduler.ScheduleAgentTask schedule(io.agentscope.extensions.scheduler.config.AgentConfig agentConfig, io.agentscope.extensions.scheduler.config.ScheduleConfig scheduleConfig) Schedule an agent execution based on the provided configuration with no initial input.This is a convenience method that delegates to
schedule(AgentConfig, ScheduleConfig, Msg), passingnullas the initial input message.- Specified by:
schedulein interfaceio.agentscope.extensions.scheduler.AgentScheduler- Parameters:
agentConfig- The configuration for the agent to be scheduledscheduleConfig- The scheduling configuration (time, mode, etc.)- Returns:
- A task handle for the scheduled agent
- Throws:
RuntimeException- if scheduling fails
-
schedule
public io.agentscope.extensions.scheduler.ScheduleAgentTask schedule(io.agentscope.extensions.scheduler.config.AgentConfig agentConfig, io.agentscope.extensions.scheduler.config.ScheduleConfig scheduleConfig, io.agentscope.core.message.Msg input) Schedule an agent execution based on the provided configuration and initial input message.This method creates a Quartz Job and Trigger based on the schedule mode:
CRON: Uses CronTriggerFIXED_RATE: Uses SimpleTrigger with repeat foreverFIXED_DELAY: Uses a one-time trigger and reschedules upon completion
If an initial
inputmessage is provided, it will be safely serialized into JSON and stored within the Quartz JobDataMap. This ensures compatibility with persistent and clustered Quartz environments (e.g., JDBCJobStore).- Specified by:
schedulein interfaceio.agentscope.extensions.scheduler.AgentScheduler- Parameters:
agentConfig- The configuration for the agent to be scheduledscheduleConfig- The scheduling configuration (time, mode, etc.)input- The initial message to pass to the agent when triggered (can be null)- Returns:
- A task handle for the scheduled agent
- Throws:
RuntimeException- if scheduling fails or if the input message serialization fails
-
cancel
Cancel a scheduled agent task.This removes the Job and Trigger from the Quartz scheduler.
- Specified by:
cancelin interfaceio.agentscope.extensions.scheduler.AgentScheduler- Parameters:
name- The name of the agent/task to cancel- Returns:
trueif the task was found and cancelled,falseotherwise
-
getScheduledAgent
public io.agentscope.extensions.scheduler.quartz.QuartzScheduleAgentTask getScheduledAgent(String name) Retrieve a scheduled agent task by name.- Specified by:
getScheduledAgentin interfaceio.agentscope.extensions.scheduler.AgentScheduler- Parameters:
name- The name of the agent- Returns:
- The task handle, or
nullif not found
-
getAllScheduleAgentTasks
Retrieve all scheduled agent tasks.This method retrieves all tasks from both the local cache and the Quartz scheduler. Even if a task is not in the local memory (e.g., created by another node in a cluster), it will be retrieved from Quartz.
- Specified by:
getAllScheduleAgentTasksin interfaceio.agentscope.extensions.scheduler.AgentScheduler- Returns:
- A list of all tasks managed by this scheduler
-
shutdown
public void shutdown()Shutdown the scheduler.This stops the Quartz scheduler and releases resources.
- Specified by:
shutdownin interfaceio.agentscope.extensions.scheduler.AgentScheduler- See Also:
-
Scheduler.shutdown(boolean)
-
getSchedulerType
- Specified by:
getSchedulerTypein interfaceio.agentscope.extensions.scheduler.AgentScheduler
-
pause
Pause a scheduled task.- Parameters:
name- The name of the agent/task to pause- Returns:
trueif successfully paused,falseotherwise
-
resume
Resume a paused task.- Parameters:
name- The name of the agent/task to resume- Returns:
trueif successfully resumed,falseotherwise
-
interrupt
Interrupt a running task.- Parameters:
name- The name of the agent/task to interrupt- Returns:
trueif the interrupt signal was sent,falseotherwise
-
getStatus
Get the current status of a scheduled task.- Parameters:
name- The name of the agent/task- Returns:
- The Quartz TriggerState, or
nullif not found
-