I have a Problem with asynchronous methods that run on a customTaskExecutors. In the run method I never reach the last log.info that the thread is finished running (where I would usualy put other cleanup code). I would like to stop that thread in the PreDestroy method with setting the boolean to false, but pre destroy only gets called when spring has ended all his threads, which includes the customTaskExecutor. I already have put the custom Exectur with a short awaitTermination time, didn't change anything. I also made a class, where I wire all taskExecutors in and shut them down externaly. That all stops the ThreadExecutor but never in a way where I can have code after the while loop running.
Does anybody have a best practice for what i want to do (basically change the boolean for the while loop on sigterm signal)? Or am I bound to use PreDestroy for all cleanup stuff when I want to use TaskExecutors of Spring Boot?
@PreDestroy
public void shutDown() {
log.info("Shutting down AsyncRunner...");
printingRunning = false;
}
@Override
@Async("customTaskExecutor") // Using the custom executor from AsyncConfig
public void run(String... args) throws Exception {
log.info("AsyncRunner starting on thread: {}", Thread.currentThread().getName());
try {
while(printingRunning && !Thread.currentThread().isInterrupted()) {
Thread.sleep(1000);
this.printWorkOut();
}
} catch (InterruptedException e) {
Thread.currentThread().interrupt(); // Restore interrupted status
log.info("AsyncRunner was interrupted.");
}
log.info("AsyncRunner finished on thread: {}", Thread.currentThread().getName());
}
Thread.sleepin an async task, as that will block a thread. If you are using that task executor for more things you will run out of threads or even worse have a non responsive system. Looks like you would need a scheduler instead of an async task and trigger the method each 1 second.