Saturday, August 1, 2015

ATG Scheduler 101

Writing a Scheduler
This is a simple scheduler that prints "Hello World" to the console. See the steps to write and debug a scheduler in ATG.

package com.wilcats.schedulers;

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;
/**
 *
 *
 * @version  : 1.0
 */
public class ExampleScheduler extends GenericService implements Schedulable{
    // 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;
    }
     
    int jobId;
    // Schedulable method
    public void performScheduledTask(Scheduler scheduler,
            ScheduledJob job){
        logDebug("Hello World!!!");
        System.out.println("Hello World");
    }
    // Start method
     
    public void doStartService () throws ServiceException{
        ScheduledJob job = new ScheduledJob("hello",
                "Prints Hello World",
                getAbsoluteName(),
                getSchedule(),
                this,
                ScheduledJob.SCHEDULER_THREAD);
        jobId = getScheduler().addScheduledJob(job);
    }
    // Stop method
    public void doStopService() throws ServiceException{
        getScheduler().removeScheduledJob(jobId);
    }
}

ExampleScheduler.properties

$class=com.wildcats.schedulers.ExampleScheduler
$scope=global
scheduler=/atg/dynamo/service/Scheduler
schedule=every 30 seconds
threadMethod=2
loggingDebug=true

Initialize this scheduler in initial.properties only if you want this scheduler to initialize/run on start up of the server. For more details on initial.properties read http://docs.oracle.com/cd/E23095_01/Platform.93/ATGProgGuide/html/s0204startinganucleuscomponent01.html
Intialize the ExampleScheduler in initial.properties like below:
/com/wilcats/schedulers/ExampleScheduler

Steps to execute a Scheduler
  1. Write the above classes to your ATG code base
  2. Build your code.
  3. Start your server. For eg: Jboss server using startJboss.sh
  4. Go to ATG Dynamo Server Admin. See details in the next section. 
  5. It will show you the component browser. Search your scheduler - ExampleScheduler
  6. Once you reach the ExampleScheduler page - go to the bottom of the page and you will see a set of methods listed. Click on doStartService. It will ask you to confirm to invoke this call. 
  7. Confirm you want to invoke this method call.
  8. Check your server-log/console and you will see "Hello World" printed there.

ATG Dynamo Server Admin

The ATG Dynamo Server Admin gives you quick access to a number of useful features. For example, you can modify the configuration for ATG server instances, browse the Nucleus component hierarchy, change administrator passwords, and so on.
To start the ATG Dynamo Server Admin:

Debugging Schedulers

  1. In eclipse, go to Debug configurations -> Remote Java Applications -> right click create new  -> change the port to 8787. 
  2. Once you have started your server, you can add breakpoints in your code and start debugging
  3. Hint: Change the schedule of your scheduler to run every 4 hours. Put breakpoint on doStartService(),  performScheduledTask(), doScheduledTask()

References


1 comment: