public abstract class ScheduledTask {
private static final Logger log = LoggerFactory.getLogger(ScheduledTask.class);
public void schedule(TaskScheduler scheduler) {
TimeSpan initialDelay = getInitialDelay();
TimeSpan interval = getInterval();
scheduler.scheduleWithFixedDelay(new Runnable() {
@Override
public void run() {
try {
log.info("Running scheduled task: {}", getKey());
ScheduledTask.this.run();
} catch (Exception e) {
log.error("Error running scheduled task: " + getKey(), e);
}
}
}, initialDelay.getTotalMilliseconds(), interval.getTotalMilliseconds(), TimeUnit.MILLISECONDS);
}