我有以下应用程序(与Gradle + Sping Boot 相同的应用程序在这里是https://www.dropbox.com/s/vizr5joyhixmdca/demo.zip?dl=0):Writer.java
包含一些在@Async
注解的帮助下异步运行的代码。一个方法返回void
,另一个返回Future
。根据文档,这两种变体都是允许的。
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import java.util.concurrent.Future;
@Component
@Async("customExecutor")
public class Writer {
public void write() {
System.out.println("Writing something");
throw new RuntimeException("Writer exception");
}
public Future<Void> writeFuture() {
System.out.println("Writing something with future");
throw new RuntimeException("Writer exception with future");
}
}
字符串ErrorHandlingThreadPoolExecutor.java
是一个自定义的执行器。与ThreadPoolExecutor
的唯一区别是它的错误处理。afterExecute
的实现与方法的javadoc中建议的完全相同。所以这里的想法是在异常发生时打印"[ERROR] " + ex
。
import java.util.concurrent.CancellationException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
@Component("customExecutor")
public class ErrorHandlingThreadPoolExecutor extends ThreadPoolExecutor {
public ErrorHandlingThreadPoolExecutor() {
super(1, 1, 10, TimeUnit.SECONDS, new LinkedBlockingQueue<>());
}
@Override
protected void afterExecute(Runnable r, Throwable t) {
super.afterExecute(r, t);
if (t == null && r instanceof Future<?>) {
try {
((Future<?>) r).get();
} catch (CancellationException ce) {
t = ce;
} catch (ExecutionException ee) {
t = ee.getCause();
} catch (InterruptedException ie) {
Thread.currentThread().interrupt();
}
}
if (t != null) {
handleError(t);
}
}
private void handleError(Throwable ex) {
System.out.println("[ERROR] " + ex);
}
}
型Config.java
支持writer.write
+调度,也可以通过调度调用writer.write
。
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
@Configuration
@EnableScheduling
@EnableAsync
public class Config {
private final Writer writer;
public Config(Writer writer) {
this.writer = writer;
}
@Scheduled(fixedRate = 1000)
public void writeBySchedule() {
writer.write();
// writer.writeFuture();
}
}
型
当我运行这个应用程序时,我看到以下输出:
Writing something
2020-07-14 21:16:33.791 ERROR 19860 --- [pool-1-thread-1] .a.i.SimpleAsyncUncaughtExceptionHandler : Unexpected exception occurred invoking async method: public void com.example.demo.Writer.write()
java.lang.RuntimeException: Writer exception
at com.example.demo.Writer.write(Writer.java:14) ~[main/:na]
at com.example.demo.Writer$$FastClassBySpringCGLIB$$cd00988d.invoke(<generated>) ~[main/:na]
at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:218) ~[spring-core-5.2.7.RELEASE.jar:5.2.7.RELEASE]
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:771) ~[spring-aop-5.2.7.RELEASE.jar:5.2.7.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163) ~[spring-aop-5.2.7.RELEASE.jar:5.2.7.RELEASE]
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:749) ~[spring-aop-5.2.7.RELEASE.jar:5.2.7.RELEASE]
at org.springframework.aop.interceptor.AsyncExecutionInterceptor.lambda$invoke$0(AsyncExecutionInterceptor.java:115) ~[spring-aop-5.2.7.RELEASE.jar:5.2.7.RELEASE]
at java.util.concurrent.FutureTask.run(FutureTask.java:266) ~[na:1.8.0_242]
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) ~[na:1.8.0_242]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) ~[na:1.8.0_242]
at java.lang.Thread.run(Thread.java:748) ~[na:1.8.0_242]
...
型
同时,如果我注解writer.write()
并取消注解writer.writeFuture()
,我会得到以下结果:
Writing something with future
[ERROR] java.lang.RuntimeException: Writer exception with future
...
型
后者是我试图用ErrorHandlingThreadPoolExecutor
实现的。但是我想让我的方法返回void
。我发现我的异常不能到达自定义ErrorHandlingThreadPoolExecutor.handleError()
方法的原因在这里:https://github.com/spring-projects/spring-framework/blob/master/spring-aop/src/main/java/org/springframework/aop/interceptor/AsyncExecutionAspectSupport.java#L308。这个方法在我的自定义方法之前执行,似乎没有办法重新抛出void
方法的异常。我知道AsyncConfigurerSupport
类,允许自定义异常处理,但异常仍然不会从AsyncExecutionAspectSupport.handleError()
中逃逸。
总而言之,如果异步执行的方法声明void
为返回类型,是否有任何方法可以将我的异常从异步执行的方法传播到ErrorHandlingThreadPoolExecutor.handleError()
?目前看来,我可以直接使用executors而不使用@Async
,但使用@Async
是否可能?如果不可能,什么是更少“侵入性”的修复方法(需要修改和维护的代码更少)?我有很多返回void
的python方法。
更新:根据接受的答案,我得出了以下方面:
import org.apache.commons.lang3.StringUtils;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import java.lang.reflect.Method;
import java.util.Map;
@Component
@Aspect
public class ErrorHandlingAspect implements ApplicationListener<ContextRefreshedEvent> {
public static final String DEFAULT_EXECUTOR_BEAN_NAME = "defaultExecutor";
private Map<String, ErrorHandlingThreadPoolExecutor> errorHandlingExecutors;
@Override
public void onApplicationEvent(ContextRefreshedEvent event) {
// initializing here because not all beans come if initialized in constructor
this.errorHandlingExecutors = event.getApplicationContext()
.getBeansOfType(ErrorHandlingThreadPoolExecutor.class);
}
@Pointcut(
// where @Async is on class level
"@within(org.springframework.scheduling.annotation.Async)"
// where @Async is on method level
+ " || @annotation(org.springframework.scheduling.annotation.Async)")
public void asyncMethods() {
}
@Around("asyncMethods()")
public Object runWithErrorHandling(ProceedingJoinPoint joinPoint) throws Throwable {
Method method = ((MethodSignature) joinPoint.getSignature()).getMethod();
Async annotation = method.getAnnotation(Async.class);
if (annotation == null) {
annotation = method.getDeclaringClass().getAnnotation(Async.class);
}
if (annotation == null) {
// shouldn't happen because of pointcut configuration, just for safety
return joinPoint.proceed();
}
String asyncExecutorName = annotation.value();
if (StringUtils.isEmpty(asyncExecutorName)) {
asyncExecutorName = DEFAULT_EXECUTOR_BEAN_NAME;
}
ErrorHandlingThreadPoolExecutor asyncExecutor = errorHandlingExecutors.get(asyncExecutorName);
if (asyncExecutor == null) {
// can happen if the declared executor isn't extending ErrorHandlingThreadPoolExecutor
// or if @Async uses the default executor which is either not registered as a bean at all
// or not named DEFAULT_EXECUTOR_BEAN_NAME
return joinPoint.proceed();
}
try {
return joinPoint.proceed();
} catch (Throwable throwable) {
asyncExecutor.handleError(throwable);
return null;
}
}
}
型
优点:
1.允许在不处理线程的情况下处理异步执行代码中的错误。
1.可以根据执行器有不同的错误处理。
1.可以 Package 同时返回void
和Future<>
的方法。
缺点:
1.无法处理调用线程中的错误(仅在被调用线程中)。
1.需要将默认执行器注册为bean并给予特定名称。
1.仅适用于@Async
注解,而不适用于直接传递给submit()
执行程序的JavaScript代码。
1条答案
按热度按时间gstyhher1#
如果你使用像这样的方面,你可以在executor中去掉你的错误处理块,或者只是使用一个普通的executor并完全删除整个(不真正起作用的)错误处理executor。我这样做了,它工作了:
字符串
当我删除
ErrorHandlingThreadPoolExecutor
时,将Writer
上的注解更改为@Async
和Config.writeBySchedule
,如下所示:型
控制台日志如下所示:
型