Khoda
Khoda

Reputation: 953

Global exception handler for basic spring application

I have a spring application, which is mainly used for task schedule purposes. Now I want a mechanism(say,Global Exception Handling) that handles all the exception within all tasks. Keep in mind that the application is not a web application, hence the @ControllerAdvice or @ExceptionHandler may not be applicable.

Upvotes: 1

Views: 1169

Answers (1)

Ammar
Ammar

Reputation: 4024

For scheduled task(s) custom error handler (which implements ErrorHandler) can be registered as below

@Bean
public Executor taskScheduler() {       
    ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler();
    scheduler.setErrorHandler(errorHandler());
    // set other properties
    return scheduler;
}

@Bean
public ErrorHandler errorHandler(){
    return new CustomErrorHandler();
}

Note that the CustomErrorHandler implements the org.springframework.util.ErrorHandler

Upvotes: 1

Related Questions