This section describes how to write a component that schedules itself to perform a task according to different schedules. In this case, the task to be performed is to write Hello
to the console.
Such a component might look like this:
import atg.nucleus.*; import atg.service.scheduler.*; public class HelloJob extends GenericService implements Schedulable { public HelloJob () {} // Scheduler property Scheduler scheduler; public Scheduler getScheduler () { return scheduler; } public void setScheduler (Scheduler scheduler) { this.scheduler = scheduler; } // Schedule property Schedule schedule; public Schedule getSchedule () { return schedule; } public void setSchedule (Schedule schedule) { this.schedule = schedule; } // Schedulable method public void performScheduledTask (Scheduler scheduler, ScheduledJob job) { System.out.println ("Hello"); } // Start method int jobId; public void doStartService () throws ServiceException { ScheduledJob job = new ScheduledJob ("hello", "Prints Hello", getAbsoluteName (), getSchedule (), this, ScheduledJob.SCHEDULER_THREAD); jobId = getScheduler ().addScheduledJob (job); } // Stop method public void doStopService () throws ServiceException { getScheduler ().removeScheduledJob (jobId); } }
Notice that this component extends GenericService
, which allows it to be notified of start and stop conditions through doStartService
and doStopService
. The component also implements Schedulable
by defining the performScheduledTask
method to print Hello
. The component also requires the scheduler
and schedule
to be set as properties.
When the component is started, it constructs a ScheduledJob
from the schedule
property, and also specifies that the job should run in the Scheduler’s thread. The component then adds the job to the Scheduler, and stores the job ID returned by the Scheduler. When the component is stopped, it removes the scheduled job by passing the ID of the job.