Sunday, December 9, 2012

Sample Scheduling a Task : ATG

Scheduling a Task


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.


CustomScheduler.java



package atg.custom.schedule;



import atg.nucleus.GenericService;
import atg.nucleus.ServiceException;
import atg.service.scheduler.Schedulable;
import atg.service.scheduler.Schedule;
import atg.service.scheduler.ScheduledJob;
import atg.service.scheduler.Scheduler;

public class CustomScheduler extends GenericService implements Schedulable {

    private Scheduler scheduler;
    private Schedule schedule;
    int jobId;
   
    public Schedule getSchedule() {
        return schedule;
    }


    public void setSchedule(Schedule schedule) {
        this.schedule = schedule;
    }


    public Scheduler getScheduler() {
        return scheduler;
    }


    public void setScheduler(Scheduler scheduler) {
        this.scheduler = scheduler;
    }


    @Override
    public void performScheduledTask(Scheduler arg0, ScheduledJob arg1) {
        // TODO Auto-generated method stub
        logDebug("Muraliraj says Hello");
    }

    @Override
    public void doStartService() throws ServiceException {
        // TODO Auto-generated method stub
        ScheduledJob scheduledJob = new ScheduledJob("Hello","Prints Hello",getAbsoluteName(),getSchedule(),this,ScheduledJob.SCHEDULER_THREAD);
        jobId = getScheduler().addScheduledJob(scheduledJob);
        //super.doStartService();
    }
   
    @Override
    public void doStopService() throws ServiceException {
        // TODO Auto-generated method stub
    getScheduler().removeScheduledJob(jobId);
    }
   
       
   
}



Note : 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

CustomScheduler.properties


$class=atg.custom.schedule.CustomScheduler
$scope=global
scheduler=/atg/dynamo/service/Scheduler
schedule=every 1 minutes


Note: Start the server and check the server log now, every 1 minutes you can see the message "Muraliraj says Hello",

1 comment:

Tech Giant said...

Refer ATG Scheduler for detailed step by step guide.

Popular Posts