Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/13.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
Spring 在带有@Async not working注释的方法中调用@Retryable方法_Spring_Spring Boot_Async Await_Spring Retry_Dynamic Proxy - Fatal编程技术网

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

Spring 在带有@Async not working注释的方法中调用@Retryable方法,spring,spring-boot,async-await,spring-retry,dynamic-proxy,Spring,Spring Boot,Async Await,Spring Retry,Dynamic Proxy,下面的@Retryable代码适用于直接调用方法的情况,但通过@Async annotated method调用Retryable方法会引发异常。 有什么建议吗 这是我的服务课 @Service public class RetryAndRecoverService { int counter = 0; String str = null; @Retryable(value = {FooException.class, BarException.class}, maxA

下面的@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引导应用程序类

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]
2018-04-05 12:45:17.644错误23052-[cTaskExecutor-1].a.i.SimpleAsyncUncaughtExceptionHandler:调用异步方法“public java.lang.String com.mscharhag.springretrydemo.RetryAndRecoverService.testRetry()”时发生意外错误。
com.mscharhag.springretrydemo.RetryAndRecoverService$FooException:null
在com.mscharhag.springretrydemo.RetryAndRecoverService.retryWithException(RetryAndRecoverService.java:19)~[classes/:na]
在com.mscharhag.springretrydemo.RetryAndRecoverService.testRetry(RetryAndRecoverService.java:47)~[classes/:na]
在com.mscharhag.springretrydemo.RetryAndRecoverService$$FastClassBySpringCGLIB$$f31442b9.invoke()~[classes/:na]
在org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)~[spring-core-4.2.5.RELEASE.jar:4.2.5.RELEASE]
在org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:720)~[spring-aop-4.2.5.RELEASE.jar:4.2.5.RELEASE]
在org.springframework.aop.framework.ReflectiveMethodInvocation.procedue(ReflectiveMethodInvocation.java:157)~[spring-aop-4.2.5.RELEASE.jar:4.2.5.RELEASE]
在org.springframework.retry.annotation.annotationawaretryoperationsinterceptor.invoke(annotationawaretryoperationsinterceptor.java:121)~[spring-retry-1.1.2.RELEASE.jar:na]
在org.springframework.aop.framework.ReflectiveMethodInvocation.procedue(ReflectiveMethodInvocation.java:179)~[spring-aop-4.2.5.RELEASE.jar:4.2.5.RELEASE]
在org.springframework.aop.interceptor.AsyncExecutionInterceptor$1.call(AsyncExecutionInterceptor.java:108)~[spring-aop-4.2.5.RELEASE.jar:4.2.5.RELEASE]
在java.util.concurrent.FutureTask.run(FutureTask.java:266)[na:1.8.0141]
在java.lang.Thread.run(Thread.java:748)[na:1.8.0141]

类似的问题也出现在这种情况下,并在spring版本中修复
4.3.14请参见

Spring使用代理应用AOP,而
@Async
@Retryable
均通过AOP应用。只有通过代理传递到对象的方法调用内部方法调用不会通过代理传递。因此,基本上,只有当
@Retryable
方法在不同的bean中或者在
@Async
方法上时,这才有效。绝对正确!!在两个不同的类中移动这两个方法之后,它工作得很好。非常感谢。