We configured closures and jobs, and executed them on remote nodes using the MapReduce/distributed closure API. Apache Ignite's job scheduler API can execute remote jobs periodically. The IgniteScheduler.scheduleLocal() method can be configured to execute periodically on a local node using the UNIX cron syntax. The IgniteScheduler APIs operate on Runnable and Callable. The following are the IgniteScheduler methods:
In this section, we will examine local job scheduling. If you want to execute a code snippet after N secs/minutes/hours and so on, the steps are as follows:
- Add a class, RunLocalSchedulerTest, start a local ignite instance, and call the runLocal on an IgniteScheduler instance. In runLocal pass a runnable closure, 10, and TimeUnit.MICROSECONDS. It will schedule the runnable to execute after 10 microseconds:
IgniteConfiguration cfg = new IgniteConfiguration();
cfg.setPeerClassLoadingEnabled(true);
try (Ignite ignite = Ignition.start(cfg)) {
ignite.scheduler().runLocal(
new Runnable() {
@Override
public void run() {
System.out.println("now executed @"+new Date());
}
},
10, TimeUnit.MICROSECONDS);
Thread.sleep(1000);
}
- When we run the program, it prints the following output:
UNIX cron supports minimum scheduling time unit 1 minute. We'll schedule a local job to execute every 1 minute. The following are the steps:
- We need to add an ignite-schedule dependency to our build.gradle:
compile group: 'org.apache.ignite', name: 'ignite-schedule',
version: '1.2.0-incubating'
- Create a class, LocalPeriodicSchedulerTest, and add the following lines to schedule a local job every 1 minute with the cron expression * * * * *:
IgniteConfiguration cfg = new IgniteConfiguration();
cfg.setPeerClassLoadingEnabled(true);
try (final Ignite ignite = Ignition.start(cfg)) {
ignite.scheduler().scheduleLocal(new Runnable() {
@Override public void run() {
System.out.println(String.format("Executing at %s",
new Date()));
}
}, "* * * * *");
System.in.read();
}
- The System.in.read() waits for the user input and lets the scheduler run in background. When we run the job, it keeps printing the local time until we hit Enter:
We can broadcast our local job to all cluster nodes and schedule it using cron, but the remote nodes must be configured with the ignite-schedule dependency.
The following is the code to broadcast our job to remote nodes and schedule it to execute periodically, every 1 minute:
- Add a new class, BroadcastScheduleTest and add the following lines:
ignite.compute().broadcast(new IgniteCallable<Object>() {
private static final long serialVersionUID = 1L;
@IgniteInstanceResource
Ignite ignite;
@Override
public Object call() throws Exception {
ignite.scheduler().scheduleLocal(new Runnable() {
@Override public void run() {
System.out.println(String.format("Executing at %s",
new Date()));
}
}, "* * * * *");
return null;
}
});
- Put the ignite-schedule-1.2.0-incubating.jar and its dependency, cron4j-2.2.5.jar
,
in the IGNITE_HOME/libs directory:
- Launch an Ignite server instance from the IGNITE_HOME/bin directory. It will keep printing the local time, until the server instance is stopped/killed:
We can configure remote nightly jobs to perform heavy calculations, such as a batch job to print year end bank statements or claim processing and so on. The complex event processing will explore the on the fly job execution.