戦争で単純なスケジューラーが必要な場合 (Spring フレームワークを使用)、次のようなこともできます。
(Spring では、"@PostConstruct" がスケジューラを初期化するため、main メソッドは必要ありません)
@Component
public class Scheduler {
private static final Logger LOG = LoggerFactory.getLogger(Scheduler.class);
@PostConstruct
private void initializeTenSecSchedule() {
final List<Runnable> jobs = new ArrayList<Runnable>();
jobs.add(doSomeTestLogs());
jobs.add(doSomeTestLogs2());
final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(jobs.size());
for(Runnable job : jobs){
scheduler.scheduleWithFixedDelay(job, 10, 10, SECONDS);
}
}
/**
* ---------------------some schedule tasks--------------------------
*/
private Runnable doSomeTestLogs(){
final Runnable job = new Runnable() {
public void run() {
LOG.debug("== foo SCHEDULE a", 1);
System.out.println("Method executed at every 10 seconds. Current time is :: "+ new Date());
}
};
return job;
}
private Runnable doSomeTestLogs2(){
final Runnable job = new Runnable() {
public void run() {
LOG.debug("== foo SCHEDULE b", 1);
System.out.println("Method executed at every 10 seconds. Current time is :: "+ new Date());
}
};
return job;
}
}