In order to schedule a task, a component needs a pointer to the Scheduler, which is usually set as a component property. The component schedules a new task by calling addScheduledJob
on the Scheduler. The Scheduler executes the job as scheduled.
When the Scheduler executes a job, it calls performScheduledTask
on the object that performs the task, which must implement atg.service.scheduler.Schedulable
. Typically, the component that schedules the task is also the Schedulable
component that executes it, but this is not strictly required.
When a component schedules a task, it must provide the Scheduler with the following information:
A name for the scheduled job; used only for display to the administrator.
The name of the component scheduling the job; used only for display to the administrator.
The
Schedulable
object that handles the job; typically, the same as the component that schedules the job.A flag that indicates how to run the job:
In a separate thread.
In the same thread as other scheduled services.
In a dedicated, reusable thread.
If a job runs in the same thread as other services, no other scheduled services can run until the job finishes. If the job is long and expensive, it should run in a separate thread. If the job is short, it should run in the same thread. For more information, see ScheduledJob Thread Methods.
The
Schedule
that indicates when the job should run. This is specified as an object that implementsatg.service.scheduler.Schedule
. Thescheduler
package provides a set of usefulSchedule
types, including schedules that represent an event at a specific time, schedules that represent periodic events, and schedules that represent events based on the calendar—for example, on the 1st and 15th of every month. Usually theSchedule
is passed in as a property. For more information, see Schedule Settings.
All of this information is encapsulated in a ScheduledJob
object, which is passed to the Scheduler’s addScheduledJob()
method.
When a job is added to the Scheduler, the Scheduler returns an integer job ID, which you can later use to reference that job. For example, to stop a scheduled job, you can call removeScheduledJob
on the Scheduler, passing in the ID of the job to stop.
When the Schedulable
object is called to perform a task, it is passed the ScheduledJob
object that was used to schedule that task. This is useful in the case where a single service is supposed to perform several kinds of scheduled tasks, and needs the properties of the ScheduledJob
to determine which task it is supposed to perform.