Spring Boot scheduler thread stops randomly

Joe :

I have an Scheduler in spring boot that fulfils a specific business task every X minutes. It works fine until it suddenly stops and does not engage anymore. There is no exception in the logs or any other logs. I need to restart the program for the scheduler to work again.

Sometimes the task of the scheduler goes wrong, and I throw an exception. To be able to handle those exceptions specifically, I wrote a custom ErrorHandler in Spring for the scheduler that resolves a seperate task for logging purposes. It is linked correctly to the scheduler and processes the task.

Joe :

This issue can come up when an unhandled exception gets thrown inside of an ErrorHandler. I am not sure about the specifics, however a Runtime Exception thrown by an ErrorHandler (or a method inside of it) that gets propagated outside of it basically kills the scheduled thread for that task. Furthermore NOTHING gets written to the logs (no Exception message, nada).

The "easiest" way to resolve this is by wrapping the entirety of the method in a try/catch block catching Exception - although depending on why you have that Error Handler that might be a bad idea. This does not solve the underlying issue at hand, but it keeps the thread alive and allows you to log the issue.

Example:

public class MyErrorHandler implements ErrorHandler {

    @Override
    public void handleError(Throwable t) {
        try {
            //handle intended exception (ex. write to database or logs)
        } catch (Exception e) {
            //handle exception that was thrown while trying to handle the intended exception. 
        }
    }

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=216780&siteId=1