版权声明:“假装、是个有灵魂的程序员” —— Go Big Or Go Home https://blog.csdn.net/u011663149/article/details/88788259
前言:
在使用Spring项目使用@Async注释时捕获异常。 当多个线程进行任务异步调用出现异常时,解决方案非常简单,需要注入自己的Exception处理程序,以便在执行@Async方法时捕获到异常。
1. AsyncConfigurer : AsyncConfigurer是Spring提供的接口,它提供了两种方法,一种是覆盖TaskExecutor(Threadpool),另一种是异常处理程序,你可以在其中注入异常处理程序,以便捕获未捕获的异常。您可以创建自己的类并直接实现该类。但我不会这样做,作为替代方案,我将使用Spring AsyncConfigurerSupport类,它由@Configuration和@EnableAsync注释,并提供默认实现。
import java.lang.reflect.Method;
import java.util.concurrent.Executor;
import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.task.SimpleAsyncTaskExecutor;
import org.springframework.lang.Nullable;
import org.springframework.scheduling.annotation.AsyncConfigurerSupport;
import org.springframework.scheduling.annotation.EnableAsync;
/**
*自定义ExceptionHandler配置
*/
@Configuration
@EnableAsync
public class CustomConfiguration extends AsyncConfigurerSupport {
@Override
public Executor getAsyncExecutor() {
return new SimpleAsyncTaskExecutor();
}
@Override
@Nullable
public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {
//lambda表达式用作异常处理程序
return (throwable, method, obj)->{
System.out.println("Exception Caught in Thread - " + Thread.currentThread().getName());
System.out.println("Exception message - " + throwable.getMessage());
System.out.println("Method name - " + method.getName());
for (Object param : obj) {
System.out.println("Parameter value - " + param);
}
};
}
}
2.创建一个抛出异常的异步方法。
import java.util.Map;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
/*定义异常捕获触发*/
@Component
public class AsyncMailTrigger {
@Async
public void sendMailwithException() throws Exception{
throw new Exception("SMTP Server not found :: orginated from Thread :: " + Thread.currentThread().getName());
}
}
3.创建一个调用Caller。
import java.util.HashMap;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
@Component
public class AsyncCaller {
@Autowired
AsyncMailTrigger asyncMailTriggerObject;
public void rightWayToCall() throws Exception {
System.out.println("Calling From rightWayToCall Thread " + Thread.currentThread().getName());
asyncMailTriggerObject.senMailwithException();
}
}
4.创建运行主类:
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.scheduling.annotation.EnableAsync;
import com.example.ask2shamik.springAsync.demo.AsyncCaller;
@SpringBootApplication
public class DemoApplication {
@Autowired
AsyncCaller caller;
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
@Bean
public CommandLineRunner commandLineRunner(ApplicationContext ctx) {
return args -> {
caller.rightWayToCall();
};
}
}
//输出结果:
//Calling From rightWayToCall Thread main
//Exception Caught in Thread - SimpleAsyncTaskExecutor-1
//Exception message - SMTP Server not found:: originated from Thread:: //SimpleAsyncTaskExecutor-1
//Method name - senMailwithException
Spring-@Async 使用参考:https://blog.csdn.net/u011663149/article/details/88561868