Class QuartzAgentScheduler

java.lang.Object
io.agentscope.extensions.scheduler.quartz.QuartzAgentScheduler
All Implemented Interfaces:
io.agentscope.extensions.scheduler.AgentScheduler

public class QuartzAgentScheduler extends Object implements io.agentscope.extensions.scheduler.AgentScheduler
AgentScheduler implementation based on Quartz Scheduler.

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 QuartzAgentSchedulerRegistry to bridge Quartz Jobs with Scheduler instances
  • Supports FIXED_DELAY mode by manually rescheduling triggers after job completion
  • Each scheduler instance has a unique ID to support multiple schedulers in the same JVM
See Also:
  • AgentScheduler
  • ScheduleAgentTask
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    static class 
  • Method Summary

    Modifier and Type
    Method
    Description
    Create a new Builder instance for QuartzAgentScheduler.
    boolean
    cancel(String name)
    Cancel a scheduled agent task.
    List<io.agentscope.extensions.scheduler.ScheduleAgentTask>
    Retrieve all scheduled agent tasks.
    io.agentscope.extensions.scheduler.quartz.QuartzScheduleAgentTask
    Retrieve a scheduled agent task by name.
     
    org.quartz.Trigger.TriggerState
    Get the current status of a scheduled task.
    boolean
    Interrupt a running task.
    boolean
    pause(String name)
    Pause a scheduled task.
    boolean
    resume(String name)
    Resume a paused task.
    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.
    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.
    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.
    void
    Shutdown the scheduler.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • builder

      public static QuartzAgentScheduler.Builder 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), passing null as the initial input message.

      Specified by:
      schedule in interface io.agentscope.extensions.scheduler.AgentScheduler
      Parameters:
      agentConfig - The configuration for the agent to be scheduled
      scheduleConfig - 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 CronTrigger
      • FIXED_RATE: Uses SimpleTrigger with repeat forever
      • FIXED_DELAY: Uses a one-time trigger and reschedules upon completion

      If an initial input message 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:
      schedule in interface io.agentscope.extensions.scheduler.AgentScheduler
      Parameters:
      agentConfig - The configuration for the agent to be scheduled
      scheduleConfig - 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

      public boolean cancel(String name)
      Cancel a scheduled agent task.

      This removes the Job and Trigger from the Quartz scheduler.

      Specified by:
      cancel in interface io.agentscope.extensions.scheduler.AgentScheduler
      Parameters:
      name - The name of the agent/task to cancel
      Returns:
      true if the task was found and cancelled, false otherwise
    • getScheduledAgent

      public io.agentscope.extensions.scheduler.quartz.QuartzScheduleAgentTask getScheduledAgent(String name)
      Retrieve a scheduled agent task by name.
      Specified by:
      getScheduledAgent in interface io.agentscope.extensions.scheduler.AgentScheduler
      Parameters:
      name - The name of the agent
      Returns:
      The task handle, or null if not found
    • getAllScheduleAgentTasks

      public List<io.agentscope.extensions.scheduler.ScheduleAgentTask> 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:
      getAllScheduleAgentTasks in interface io.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:
      shutdown in interface io.agentscope.extensions.scheduler.AgentScheduler
      See Also:
      • Scheduler.shutdown(boolean)
    • getSchedulerType

      public String getSchedulerType()
      Specified by:
      getSchedulerType in interface io.agentscope.extensions.scheduler.AgentScheduler
    • pause

      public boolean pause(String name)
      Pause a scheduled task.
      Parameters:
      name - The name of the agent/task to pause
      Returns:
      true if successfully paused, false otherwise
    • resume

      public boolean resume(String name)
      Resume a paused task.
      Parameters:
      name - The name of the agent/task to resume
      Returns:
      true if successfully resumed, false otherwise
    • interrupt

      public boolean interrupt(String name)
      Interrupt a running task.
      Parameters:
      name - The name of the agent/task to interrupt
      Returns:
      true if the interrupt signal was sent, false otherwise
    • getStatus

      public org.quartz.Trigger.TriggerState getStatus(String name)
      Get the current status of a scheduled task.
      Parameters:
      name - The name of the agent/task
      Returns:
      The Quartz TriggerState, or null if not found