title: Universal exception handler for @Scheduled tasks in Spring (Boot) with Java configuration
body: <p>I have lots of scheduled tasks in my Spring Boot (ver 1.4.2) application and would like to catch all exceptions from them using one handler like is possible for normal controllers with <a href="http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/web/bind/annotation/ExceptionHandler.html" rel="noreferrer">@ExceptionHandler</a> annotation. This approach does not work for tasks that are defined with @Scheduled annotation because of threading:</p>
  
  <pre><code>@Component
  public class UpdateJob {
      @Transactional
      @Scheduled(cron = "0 1 0 * * *")
      public void runUpdateUsers() {
          userService.updateUsers();
      }
  
      @ExceptionHandler
      public void handle(Exception e) {
         // some more logic here
         logger.error(e.getMessage());
      }
  
   }
  </code></pre>
  
  <p>@ExceptionHandler does not work for @Scheduled method (and it turns out it is not meant to). Instead, Spring Boot uses it's own <a href="https://docs.spring.io/spring/docs/4.2.2.RELEASE_to_4.2.3.RELEASE/Spring%20Framework%204.2.2.RELEASE/org/springframework/scheduling/support/TaskUtils.LoggingErrorHandler.html" rel="noreferrer">LoggingErrorHandler</a>:</p>
  
  <pre><code>2016-12-08 15:49:20.016 ERROR 23119 --- [pool-7-thread-1] o.s.s.s.TaskUtils$LoggingErrorHandler    : Unexpected error occurred in scheduled task.
  </code></pre>
  
  <p>Can I somehow replace or supply default exception handler for scheduled tasks? Or would it make sense (and is it possible) to switch to <a href="https://docs.spring.io/spring/docs/4.2.2.RELEASE_to_4.2.3.RELEASE/Spring%20Framework%204.2.2.RELEASE/org/springframework/scheduling/support/TaskUtils.PropagatingErrorHandler.html" rel="noreferrer">PropagatingErrorHandler</a> which as I understand propagates the error further? Is there any other way to achieve the goal using only Java configuration (no XML)?</p>
  
  <p>This is not a duplicate of <a href="https://stackoverflow.com/questions/24031613/exception-handling-for-spring-3-2-scheduled-annotation">this question</a> as it explicitly asks for <strong>a solution based on Java configuration, not XML</strong> (so it is decent to incorporate into Spring Boot project without any XML configuration).</p>
  
  <p>There are also some answers that demonstrate how to configure TaskScheduler from the scratch. Eg this <a href="https://stackoverflow.com/a/27970752/1169324">answer</a> requires that you also define pool size, max pool size, queue capacity. <a href="https://dzone.com/articles/spring-async-and-exception" rel="noreferrer">Here</a> is a solution which also need very extensive configuration. <a href="http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/scheduling/annotation/EnableScheduling.html" rel="noreferrer">Documentation</a> shows how to configure other aspects, but not how to specify error handling. But <strong>what is the minimal required effort with Java config</strong> so that I can maximally keep Spring Boot default values (thread pools, executor configurations etc). </p>