Java API:Schedule a Job
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
- <syntaxhighlight lang="java" enclose="div">
Schedulable job = new DailyTask(); </syntaxhighlight>
- 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.
- <syntaxhighlight lang="java" enclose="div">
// "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 </syntaxhighlight>
- 3. Schedule it:
- <syntaxhighlight lang="java" enclose="div">
String jobId = Functions.schedule( "My Job", job, cronExp [ , (String) userId ] ); </syntaxhighlight>
- 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:
- <syntaxhighlight lang="java" enclose="div">
Result result = Functions.deleteJob(String jobId); </syntaxhighlight>
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:
- <syntaxhighlight lang="java" enclose="div">
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;
} </syntaxhighlight>
While the inner class (DailyTask) does the required tasks when the job runs:
- <syntaxhighlight lang="java" enclose="div">
public class DailyTask implements Schedulable {
@Override public void execute() { // Do important stuff here }
} </syntaxhighlight>
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:
- URL: https://{yourDomain}/networking/rest/class/operation/exec
- Request type: application/xml
- Request body:
- <syntaxhighlight lang="xml" enclose="div">
<platform>
<execClass> <clazz>com.platform.demo.cron.CronJobDemo</clazz> <method>run</method> </execClass>
</platform> </syntaxhighlight>
- 3. See the job's status either by using the REST scheduledJob Resource or by using the GUI: