This example shows how to use CronTrigger. CronTrigger is an implementation of Trigger which is used to schedule tasks at the specified time. It is typically used with TaskScheduler.
package com.logicbig.example;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.scheduling.support.CronTrigger;
import java.time.LocalTime;
public class CronTriggerExample {
public static void main (String[] args) throws InterruptedException {
System.out.println("Program starts at : " + LocalTime.now());
ThreadPoolTaskScheduler s = new ThreadPoolTaskScheduler();
s.setPoolSize(5);
s.initialize();
for (int i = 0; i < 2; i++) {
s.schedule(getTask(), new CronTrigger("0 43 19 * * ?"));
}
s.getScheduledThreadPoolExecutor().shutdown();
}
public static Runnable getTask () {
return () -> System.out.printf("Task: %s, Time: %s:%n",
Thread.currentThread().getName(),
LocalTime.now());
}
}
Output
Program starts at : 19:42:20.843
Task: ThreadPoolTaskScheduler-1, Time: 19:43:00.001:
Task: ThreadPoolTaskScheduler-2, Time: 19:43:00.001:
INFO: Initializing ExecutorService
Original Post