Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/362.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 基本spring应用程序的全局异常处理程序_Java_Spring_Exception Handling - Fatal编程技术网

Java 基本spring应用程序的全局异常处理程序

Java 基本spring应用程序的全局异常处理程序,java,spring,exception-handling,Java,Spring,Exception Handling,我有一个spring应用程序,主要用于任务调度目的。现在我需要一种机制(比如说,全局异常处理)来处理所有任务中的所有异常。请记住,该应用程序不是web应用程序,因此@ControllerAdvice或@ExceptionHandler可能不适用。对于计划任务,可以按如下方式注册自定义错误处理程序(实现) @Bean public Executor taskScheduler() { ThreadPoolTaskScheduler scheduler = new Thread

我有一个spring应用程序,主要用于任务调度目的。现在我需要一种机制(比如说,全局异常处理)来处理所有任务中的所有异常。请记住,该应用程序不是web应用程序,因此
@ControllerAdvice
@ExceptionHandler
可能不适用。

对于计划任务,可以按如下方式注册自定义错误处理程序(实现)

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

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

请注意,
CustomErrorHandler
实现了
org.springframework.util.ErrorHandler

AFAIK没有这样的机制,但是您可以用一些
try-catch
来包装您的所有任务,因此我更愿意将其命名为伟大的spring框架的一个缺点。