当前位置: 首页 > 知识库问答 >
问题:

在带有@Async not working注释的方法中调用@Retryable方法

葛宏爽
2023-03-14

下面的@Retryable代码适用于直接调用方法的情况,但通过@Async annotated method调用Retryable方法会引发异常。有什么建议吗?

这是我的服务课

@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]

共有1个答案

崔宜修
2023-03-14

spring版本4.3也解决了类似的问题。请看https://jira.spring.io/browse/SPR-16196

 类似资料:
  • 我正在测试一个Spring重试,但似乎没有调用恢复。试图让它工作,但似乎详尽无遗。我传递给@recover no argument,Throwable,exception。改变了重试依赖的版本,似乎它包含在spring boot的aop中,并删除了它。Creading Geting Recovery没有被调用,出现以下异常Messege。 请求处理失败;嵌套异常是org.springframewo

  • 问题内容: 我很好奇弹簧注入如何处理带有注释的调用方法。如果我在方法上添加注释并返回实例,则我理解这告诉spring通过调用方法并获取返回的实例来创建bean。但是,有时必须使用该bean来连接其他bean或设置其他代码。完成此操作的通常方法是调用带注释的方法以获取实例。我的问题是,为什么这不会导致有多个bean实例漂浮? 例如,请参见下面的代码(取自另一个问题)。该方法带有注释,因此我可以想象s

  • 问题内容: 我需要 在类中使用@X注释的方法或使用@X注释的方法的切入点 。我还 需要注释对象 。如果同时注释了类和方法,则 我更喜欢将方法注释作为参数 。 我尝试了以下操作,这将创建“不一致的绑定”警告。(为什么不将它们设置为null?) 下面创建“跨’||’的参数x的模糊绑定 在切入点”警告。(我认为这并不一定有意义:为什么不绑定第一个短路评估?) 如果存在类和方法注释,则将先前的尝试自然地分

  • 我尝试在DAO中使用带有Spring注释的,但遇到了这样的问题: 工作正确: 例外: 如何使用parallelStream()注释的事务方法? 更新为什么会发生这种情况Spring事务管理器和多线程,但我希望支持java 8的Spring 4能够提供一些解决方案。有什么想法吗?

  • 我只想使用@scheduler注释定期运行spring boot main方法。我已经指定了一些附加代码,这些代码将在启用REST服务之前执行一些预操作。 我想每10秒运行一次上面的主方法。并在主方法中添加了@时间表注释。但它抛出了一个例外: 根据doc@Scheduler的预期行为应调用一个没有args[]的方法 我想在main方法中使用注释,如下所示: 错误: 组织。springframewo