Friday, December 30, 2011

Scheduled Task Example


Overview
There are a few simple things to watch out for when writing a Scheduled Task in Dynamo. This example can be used as a boiler plate for your own tasks.

The code shown in gray is specific to this example, so copy the whole file and simply modify the bits you don't need.

Source Code and Configuration Files
Repeater.java - imagescript.atg.schedule.Repeater

package imagescript.atg.schedule;

import atg.nucleus.GenericService;
import atg.service.scheduler.*;
import atg.servlet.*;

public class Repeater
               extends GenericService
               implements Schedulable
{
  Schedule mSchedule;
  Scheduler mScheduler;
  int mJobId;
  String mMessage;

  public String getMessage(){ return mMessage; }
  public void setMessage(String s){ mMessage = s; }

  public void performScheduledTask(Scheduler sch, ScheduledJob job)
  {
    if(isLoggingDebug())
    {
      logDebug("Job Name = " + job.getJobName());
      logDebug("Job Desc = " + job.getJobDescription());
      logDebug("Job Id = " + job.getJobId());
      logDebug("Job Source = " + job.getSourceName());
    }

    if(isLoggingInfo())
    {
      logInfo(getMessage());
    }
  }

 
  //Demonstration of using the value parser to manipulate the
  // schedule programmatically
  public String getScheduleString()
  {
    String str;
    if(getSchedule() != null)
    {
      str = getSchedule().scheduleString();
    }
    else
    {
      str = "No Schedule Specified";
    }

    return str;
  }

  public void setScheduleString(String s)
  {
     SchedulePropertyValueParser parser = new SchedulePropertyValueParser();

     try
     {
       setSchedule((Schedule)parser.parsePropertyValue(s, Schedule.class));

     }
     catch(Exception e)
     {
       if(isLoggingError()){ logError(e); }
     }
  }

  public void setSchedule(Schedule s){ mSchedule = s; }
  public Schedule getSchedule(){ return mSchedule; }

  public void setScheduler(Scheduler s){ mScheduler = s; }
  public Scheduler getScheduler(){ return mScheduler; }

  public int getJobId(){ return mJobId; }


  public Repeater()
  {
    mJobId = 0;
    mMessage = "Default Message";
  }

  public void doStartService()
  {
    startJob();
  }

  public void doStopService()
  {
    stopJob();
  }

  void restartJob()
  {
    stopJob();
    startJob();
  }

  void startJob()
  {
    if(getScheduler() != null)
    {
      if(mJobId == 0)
      {
         ScheduledJob job = new ScheduledJob("repeater",
                                             "Demonstrates schedule",
                                             getAbsoluteName(),
                                             getSchedule(),
                                             (Schedulable)this,
                                             ScheduledJob.SCHEDULER_THREAD);
         mJobId = getScheduler().addScheduledJob(job);

      }
      else
      {
        if(isLoggingError())
        {
          logError("Attempted to start job without stopping it first");
        }
      }
    }
    else
    {
        if(isLoggingError())
        {
          logError("Scheduler not specified");
        }
    }
  }


  void stopJob()
  {
      if(getScheduler() != null)
      {
        if(mJobId != 0)
        {
           getScheduler().removeScheduledJob(mJobId);
           mJobId = 0;
        }
        else
        {
          if(isLoggingError())
          {
            logError("Attempted to stop job without starting it first");
          }
        }
      }
      else
      {
          if(isLoggingError())
          {
            logError("Scheduler not specified");
          }
      }
    }

  public boolean handleRestart(DynamoHttpServletRequest req,
                              DynamoHttpServletResponse res)
  {
    restartJob();
    return true;
  }
}


Repeater.properties

This is an example configuration file for the task

$class=imagescript.atg.schedule.Repeater
$scope=global
schedule=every\ 1\ minute
scheduler=/atg/dynamo/service/Scheduler

How to make sure the job starts automatically with Dynamo
Add your component to one of the inital arrays in an Initial component. Dynamo has an Initial component on the root /Initial. Usually we want to create our own Initial component for our application in a sub directory, /example/Initial and add that component to the /Initial initial list. Then add your component to your /example/Initial initial components list

No comments:

Popular Posts