Lightweight Java task scheduler with fluent API, inspired from Spring framework scheduler.
Clockwise is initially developed for Audit4j - An open source auditing framework.
Please visit the FAQ page for more information.
Quick Guide:
String id = Schedulers.newDefault().schedule(new PrintTask(), new PeriodicTrigger(1000)); // (1)
System.out.println(id); // (2)
String id2 = Schedulers.newDefault().schedule(new PrintTask(), new CronTrigger("0/2 * * * * *")); // (3)
System.out.println(id2); // (4)
System.out.println(Schedulers.getRunningSchedulerCount()); // (5)
TimeUnit.SECONDS.sleep(5); // (6)
Schedulers.stop(id2); // (7)
System.out.println(Schedulers.getRunningSchedulerCount()); // (8)
TimeUnit.SECONDS.sleep(5); // (9)
Schedulers.stopAll(); // (10)
System.out.println("Executing threadpool scheduler");
String id3 = Schedulers.newThreadPoolScheduler(4).schedule(new PrintTask(), new PeriodicTrigger(4000)); // (11)
System.out.println(id3); // (12)
TimeUnit.SECONDS.sleep(40); // (13)
Schedulers.stopAll(); // (14)
//Registor tasks.
Schedulers.taskRegistry().registor(new TriggerTask(new PrintTask(), new PeriodicTrigger(1000))).registor(
new TriggerTask(new PrintTask(), new PeriodicTrigger(3000))); // (15)
List<String> jobIds = Schedulers.taskRegistry().scheduleAll(); // (16)
public class PrintTask implements Runnable {
@Override
public void run() {
System.out.println("Executed");
}
}
- Scheduler 1: Schedules a task for every one second.
- Prints the scheduler id.
- Scheduler 2 Schedules an another task for every two second. This uses Cron expression.
- Prints the scheduler 2 id.
- Prints the running scheduler counts. This will prints '2'
- Sleep for five seconds
- Stops the scheduler 2
- Prints the running scheduler counts. This will prints '1'
- Sleep for five seconds
- Stop all running schedulers.
- Scheduler 3: Schedules an another task for every four seconds. The executor is created using thread pool and 4 threads used.
- Prints the scheduler 3 id.
- Sleep for fourty seconds.
- Stop all running schedulers.
- Register trigger tasks.
- Schedule all registered tasks.