REF: https://www.baeldung.com/spring-task-scheduler
@Configuration
@ComponentScan(
basePackages="com.baeldung.taskscheduler",
basePackageClasses={ThreadPoolTaskSchedulerExamples.class})
public class ThreadPoolTaskSchedulerConfig {
@Bean
public ThreadPoolTaskScheduler threadPoolTaskScheduler(){
ThreadPoolTaskScheduler threadPoolTaskScheduler
= new ThreadPoolTaskScheduler();
threadPoolTaskScheduler.setPoolSize(5);
threadPoolTaskScheduler.setThreadNamePrefix(
"ThreadPoolTaskScheduler");
return threadPoolTaskScheduler;
}
}
SimpleTask 구현
class RunnableTask implements Runnable{
private String message;
public RunnableTask(String message){
this.message = message;
}
@Override
public void run() {
System.out.println(new Date()+" Runnable Task with "+message
+" on thread "+Thread.currentThread().getName());
}
}
taskScheduler에 의한 SimpleTask 스케줄링
class RunnableTask implements Runnable{
private String message;
public RunnableTask(String message){
this.message = message;
}
@Override
public void run() {
System.out.println(new Date()+" Runnable Task with "+message
+" on thread "+Thread.currentThread().getName());
}
}
Scheduling at a Fixed Rate
taskScheduler.scheduleAtFixedRate(
new RunnableTask("Fixed Rate of 2 seconds") , 2000);
Scheduling with CronTrigger
CronTrigger cronTrigger
= new CronTrigger("10 * * * * ?");
taskScheduler.schedule(new RunnableTask("Cron Trigger"), cronTrigger);
PeriodicTrigger
PeriodicTrigger periodicTrigger
= new PeriodicTrigger(2000, TimeUnit.MICROSECONDS);
taskScheduler.schedule(
new RunnableTask("Periodic Trigger"), periodicTrigger);
periodicTrigger.setFixedRate(true);
periodicTrigger.setInitialDelay(1000);