In order for a component to schedule a task, the component needs a pointer to the Scheduler, which is usually set as a property. The component then schedules a new task by calling addScheduledJob
on the Scheduler. The Scheduler handles things from there, waiting until the time comes to execute the job.
When the Scheduler executes a job, it calls performScheduledTask
on the object that is supposed to perform the task, which must implement atg.service.scheduler.Schedulable
. Most often the component that scheduled the task will also be the Schedulable
that will handle the task, but this is not strictly required.
When a component schedules a task, it must specify enough information for the Scheduler to handle the scheduling:
A name for the scheduled job, which is not used except when it is displayed to the administrator.
The name of the component scheduling the job. Once again, this is not used except for purposes of displaying to the administrator.
The
Schedulable
object that will handle the job. Most often this is the same as the component scheduling the job, but need not be the case.A flag indicating whether the job should
run in a separate thread when its time comes,
run in the same thread that all other scheduled services run in, or
run in a dedicated, reusable thread.
If it runs in the same thread, then no other scheduled services can run until that job finishes. If the job is long and expensive, it should run in a separate thread. If the job is short, then it should run in the same thread.
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 (e.g., on the 1st and 15th of every month). Usually theSchedule
is passed in as a property.
All of this information is encapsulated in a ScheduledJob
object, which is passed to the addScheduledJob
method of the Scheduler.
When a job is added to the Scheduler, the Scheduler returns a job ID for the job, which is an integer that can later refer to that job. For example, to stop a scheduled job, you can call removeScheduledJob
on the Scheduler, passing in the ID of the job to be stopped.
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.