gpt4 book ai didi

spring - 在用@Async 注释的方法中调用@Retryable 方法不起作用

转载 作者:行者123 更新时间:2023-12-01 15:13:54 24 4
gpt4 key购买 nike

下面的@Retryable 代码适用于直接调用方法的地方,但是通过@Async 注释方法调用可重试方法然后抛出异常。
有什么建议 ?

这是我的服务类

@Service
public class RetryAndRecoverService {

int counter = 0;
String str = null;
@Retryable(value = {FooException.class, BarException.class}, maxAttempts = 5, backoff = @Backoff(delay = 1000, multiplier = 1))
public String retryWithException() {
System.out.println("retryWithException - "+(counter++));
String value = getMapperValue();
if(value == null){
throw new FooException();
}
return value;
}

private String getMapperValue() {
return null;
}

@Async
public String testRetry(){
return retryWithException();
}

}

这是 Junit 测试类
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = RetryExampleApplication.class)
public class RetryTest {

@Autowired
private RetryAndRecoverService retryAndRecoverService;

@Test
public void retryWithException() {
Stream.of(IntStream.range(0, 3)).forEach(index -> {
String str = retryAndRecoverService.testRetry();
System.out.println(str);

});
}
}

这是 Spring Boot 应用程序类
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.retry.annotation.EnableRetry;
import org.springframework.scheduling.annotation.EnableAsync;

@EnableRetry
@EnableAsync
@SpringBootApplication
public class RetryExampleApplication {


@Autowired
RetryAndRecoverService retryAndRecoverService;

public static void main(String[] args){
SpringApplication.run(RetryExampleApplication.class, args);
}

private void retryAndRecoverService() {
retryAndRecoverService.retryWithException();

}
}

异常如下。请对此有任何建议。

retryWithException - 0
2018-04-05 12:45:17.644 ERROR 23052 --- [cTaskExecutor-1] .a.i.SimpleAsyncUncaughtExceptionHandler : Unexpected error occurred invoking async method 'public java.lang.String com.mscharhag.springretrydemo.RetryAndRecoverService.testRetry()'.

com.mscharhag.springretrydemo.RetryAndRecoverService$FooException: null
at com.mscharhag.springretrydemo.RetryAndRecoverService.retryWithException(RetryAndRecoverService.java:19) ~[classes/:na]
at com.mscharhag.springretrydemo.RetryAndRecoverService.testRetry(RetryAndRecoverService.java:47) ~[classes/:na]
at com.mscharhag.springretrydemo.RetryAndRecoverService$$FastClassBySpringCGLIB$$f31442b9.invoke(<generated>) ~[classes/:na]
at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204) ~[spring-core-4.2.5.RELEASE.jar:4.2.5.RELEASE]
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:720) ~[spring-aop-4.2.5.RELEASE.jar:4.2.5.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157) ~[spring-aop-4.2.5.RELEASE.jar:4.2.5.RELEASE]
at org.springframework.retry.annotation.AnnotationAwareRetryOperationsInterceptor.invoke(AnnotationAwareRetryOperationsInterceptor.java:121) ~[spring-retry-1.1.2.RELEASE.jar:na]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) ~[spring-aop-4.2.5.RELEASE.jar:4.2.5.RELEASE]
at org.springframework.aop.interceptor.AsyncExecutionInterceptor$1.call(AsyncExecutionInterceptor.java:108) ~[spring-aop-4.2.5.RELEASE.jar:4.2.5.RELEASE]
at java.util.concurrent.FutureTask.run(FutureTask.java:266) [na:1.8.0_141]
at java.lang.Thread.run(Thread.java:748) [na:1.8.0_141]

最佳答案

一个类似的问题是这种情况,并用 Spring 版本修复
4.3.14 见https://jira.spring.io/browse/SPR-16196

关于spring - 在用@Async 注释的方法中调用@Retryable 方法不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49666494/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com