AgileApps Support Wiki Pre Release

Difference between revisions of "Java API:Schedule a Job"

From AgileApps Support Wiki
imported>Aeric
m (Text replace - '] > Setup > Monitor >' to '] > Administration > Monitoring >')
imported>Aeric
 
Line 37: Line 37:
public String run(Map params)  
public String run(Map params)  
{
{
     Functions.debug("Scheduling...");
     Logger.info("Scheduling...", "CronJob");
     String msg = "";
     String msg = "";
     try {
     try {
Line 51: Line 51:
       msg = "Failed: " + e.getMessage();
       msg = "Failed: " + e.getMessage();
     }
     }
     Functions.debug(msg);
     Logger.info(msg, "CronJob");
     return msg;
     return msg;
}     
}     

Latest revision as of 01:38, 10 September 2013

The platform's scheduling APIs let you specify jobs to be carried out at regular intervals.

Schedule a Job

To schedule a job using the Java APIs:

1. Create an instance of a class that implements com.platform.api.Schedulable
Schedulable job = new DailyTask();
The execute() method specified by that interface is invoked when the job runs.
2. Create a string with a Cron Expression that specifies the repetition interval.
// "Secs Mins Hrs DayOfMon Mon DayOfWk Yr" 
// ?: don't care  a,b: list  a/b:interval  a-b: range
String cronExp = "0 0 3 ? ? * ?"; // Run every day at 3 am
3. Schedule it:
String jobId = Functions.schedule( "My Job", job, cronExp [ , (String) userId ] );
  • The method returns a jobId that can be used to monitor the job or delete it.
  • The optional userId specifies the user who is nominally carrying out the job's tasks when it runs.

Delete a Job

To delete a job:

Result result = Functions.deleteJob(String jobId);

Sample Code

The CronJobDemo sample class schedules a job that runs every day at 3 am.

How it Works

The run method does the scheduling:

public String run(Map params) 
{
    Logger.info("Scheduling...", "CronJob");
    String msg = "";
    try {
      // Format: "Secs  Mins  Hrs  DayOfMon  Mon  DayOfWk  Yr" 
      // Values: ?="don't care"  a,b=list  a/b=interval  a-b=range
      String cronExp = "0 0 3 ? * * *";   // Run every day at 3 am

      Schedulable task = new DailyTask();
      String jobId = Functions.schedule("Daily Task", task, cronExp);
      msg = "Job scheduled";
    }
    catch (Exception e) {
      msg = "Failed: " + e.getMessage();
    }
    Logger.info(msg, "CronJob");
    return msg;
}

While the inner class (DailyTask) does the required tasks when the job runs:

public class DailyTask implements Schedulable
{
    @Override
    public void execute() {
       // Do important stuff here      
    }   
}

Notes:

  • The run method's params argument is required to make the method callable using the REST API. Otherwise, it isn't used.
  • A cron expression can use a "?" for DayOfMon or for DayOfWk, but not both.
  • Similarly, it can use "*" for DayOfMon or for DayOfWk, but not both.

How to Run It

1. Put the CronJobDemo class on the platform, using either the GUI or the REST class Resource.
2. Schedule the job by using the REST class/operation/exec resource to invoke the run method:
<platform>
  <execClass>
    <clazz>com.platform.demo.cron.CronJobDemo</clazz>
    <method>run</method>
  </execClass>
</platform>
3. See the job's status either by using the REST scheduledJob Resource or by using the GUI:
GearIcon.png > Administration > Monitoring > Scheduled Job Log