forked from Archives/Athou_commafeed
31 lines
843 B
Java
31 lines
843 B
Java
package com.commafeed.backend.task;
|
|
|
|
import java.util.concurrent.ScheduledExecutorService;
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
@Slf4j
|
|
public abstract class ScheduledTask {
|
|
protected abstract void run();
|
|
|
|
protected abstract long getInitialDelay();
|
|
|
|
protected abstract long getPeriod();
|
|
|
|
protected abstract TimeUnit getTimeUnit();
|
|
|
|
public void register(ScheduledExecutorService executor) {
|
|
Runnable runnable = () -> {
|
|
try {
|
|
ScheduledTask.this.run();
|
|
} catch (Exception e) {
|
|
log.error(e.getMessage(), e);
|
|
}
|
|
};
|
|
log.debug("registering task {} for execution every {} {}, starting in {} {}", getClass().getSimpleName(), getPeriod(),
|
|
getTimeUnit(), getInitialDelay(), getTimeUnit());
|
|
executor.scheduleWithFixedDelay(runnable, getInitialDelay(), getPeriod(), getTimeUnit());
|
|
}
|
|
}
|