Scheduling
Scheduled tasks are instances of GridTasks with a predefined task type. These tasks are created by Platform for both time-based workflows and programmatically created schedules.
Time-Baser Workflows
Time-based workflows allow you to define a workflow that will be executed on a schedule. For more information on creating time-based workflows, refer to the chapter titled Data and Process Modeling in Studio in this user guide.
Programmatic Scheduling
Similar to time-based workflows, you can create schedules programmatically. These schedules must have their GroupName and a ScheduleName defined.
The schedule name is used to uniquely identify a schedule within a group. With both the group name and schedule name, you can enable, disable, add, remove, or run a single schedule.
You can specify the class that will run when the scheduled task executes by using the ScheduleEntry.setRunnableClassName method. The class must implement the java.lang.Runnable interface and must either have a no-arg constructor or a constructor accepting JSONObject as its only argument.
This sets up the Example schedule in the Test group. When this schedule runs, it will execute the NoOpRunnable.run() method.
package
com.example;
import
org.json.JSONObject;
public
class
NoOpRunnable
implements
Runnable {
public
NoOpRunnable(JSONObject args) {
//parse and manipulate args
}
public
void
run() {
//no-op
}
}
Next, we'll create the scheduler service.
SchedulerService schedulerService = Services.get(SchedulerService.
class
);
ScheduleEntry schedule = schedulerService.newScheduleEntry();
schedule.setGroupName(
"Test"
);
schedule.setScheduleName(
"Example"
);
schedule.setRunnableClassName(
"com.example.NoOpRunnable"
);
You have a number of options in the way you run the Example schedule:
OnceOnlySchedule - This schedule type will only run once at the specified time. The following example will set Example to run at 11:00 AM on January 1st.
Calendar c = Calendar.getInstance();
c.set(
2011
, Calendar.JANUARY,
1
,
11
,
0
);
OnceOnlySchedule once = schedulerService.newOnceOnlySchedule(c);
schedule.setSchedule(once);
schedulerService.addSchedules(schedule);
RepeatingSchedule - This schedule type will repeat on a given interval from whenever it starts. The following example will set an example to run every thirty minutes.
RepeatingSchedule repeat = schedulerService.newRepeatingSchedule(
30
*
60
);
schedule.setSchedule(repeat);
schedulerService.addSchedules(schedule)
ComplexSchedule - This schedule allows you to configure a recurring schedule that will repeat at specific times of the year, month, or day. A ComplexSchedule is comprised of ComplexScheduleElements.
ComplexSchedule complex = schedulerService.newComplexSchedule();
ComplexScheduleElement complexElement = complex.newComplexScheduleElement();
complexElement.getWeekDay().add(
1
);
//Sunday
complexElement.getHourOfDay().add(
12
);
//12 PM
complexElement.getHourOfDay().add(
15
);
//3 PM
complexElement.getMinuteOfHour().add(
30
);
//30 min into the hour
complex.getComplexScheduleElements().add(complexElement);
ComplexScheduleElement complexElement2 = complex.newComplexScheduleElement();
complexElement2.getWeekDay().add(
2
);
//Monday
complexElement2.getHourOfDay().add(
13
);
//1 PM
complex.getComplexScheduleElements().add(complexElement2);
schedule.setSchedule(complex);
schedulerService.addSchedules(schedule);
Delayed Execution - In some cases, you may want to delay execution until some time in the future. It is possible to disable an existing schedule. The following example shows how to delay a current schedule until the following day.
RepeatingSchedule repeat = schedulerService.newRepeatingSchedule(
30
*
60
);
schedule.setSchedule(repeat);
Calendar tomorrow = Calendar.getInstance();
tomorrow.add(Calendar.DAY_OF_MONTH,
1
);
schedule.setDisableUntil(tomorrow);
schedulerService.addSchedules(schedule);
In addition to controlling when the schedule runs, you can override the maximum allowed duration and the maximum number of attempts for a schedule.
Refer to the javadoc for ScheduleEntry for more details methods and configuration options.
One caveat you should keep in mind with respect to schedules is that all schedules are created with the same task type, DTBScheduler. If your time-based workflow or schedule Runnable is long-running, it has the potential to starve out other scheduled tasks. In these cases, you can generate a separate GridTask as part of the workflow / Runnable and delegate the work to that task. Alternatively, you can configure the SchedulerConfiguration to suit your needs.