For my design I need a controllable schedular. Spring boot offers an @Scheduled
annotation but that is more simplified and I do not have granular control.
So I wanted to implement my own scheduler manually.
This is the class I created:
@Slf4j
@Service
public class JobExecutor {
private static ScheduledExecutorService jobExecutor;
private static Environment env;
@Autowired
private JobExecutor(Environment env) {
JobExecutor.env = env;
}
public static ScheduledExecutorService INSTANCE() {
if (null == jobExecutor) {
synchronized (JobExecutor.class) {
if (null == jobExecutor) {
jobExecutor = Executors.newScheduledThreadPool(
Integer.parseInt(Objects.requireNonNull(env.getProperty("scheduler.jobs"))));
}
}
}
return jobExecutor;
}
}
With this approach I could simply call the static method to get a single instance.
Is this correct approach for a schedular?
I need to start and stop and shutdown the jobs. I tried guava AbstractScheduledService
but that does not seem to be working.