I have many scheduled tasks in my Spring Boot application (ver 1.4.2) and you want to catch all exceptions from them using a single handler, as is possible for regular controllers with @ExceptionHandler annotation. This approach does not work for tasks identified using @Scheduled annotations due to streaming:
@Component public class UpdateJob { @Transactional @Scheduled(cron = "0 1 0 * * *") public void runUpdateUsers() { userService.updateUsers(); } @ExceptionHandler public void handle(Exception e) {
@ExceptionHandler does not work for the @Scheduled method (and it turns out that it is not intended). Instead, Spring Boot uses its LoggingErrorHandler :
2016-12-08 15:49:20.016 ERROR 23119 --- [pool-7-thread-1] osssTaskUtils$LoggingErrorHandler : Unexpected error occurred in scheduled task.
Is there any way to replace or provide a default exception handler for scheduled tasks? Or would it be advisable (and possible) to switch to the PropagatingErrorHandler , which, as I understand it, propagates the error further? Is there any other way to achieve the goal using only Java configuration (without XML)?
This is not a duplicate of this question , as it explicitly requests a solution based on the Java configuration, not XML (so this is to include Spring in the project without any XML configuration).
There are also some answers that demonstrate how to configure TaskScheduler from scratch. For example, this answer requires that you also determine the pool size, maximum pool size, and queue capacity. Here is a solution that also needs a very extensive configuration. The documentation shows how to configure other aspects, but not how to specify error handling. But , which is the minimum required effort with the Java configuration , so I can maximize Spring's default load values (thread pools, artist configurations, etc.).
MF.OX source share