java – Spring Retry不适用于第二级方法

@Retryable似乎没有像下面的sphRemoteCall那样在第二级方法上工作.我看到创建了一个代理,但它永远不会在失败时重试.

一旦我将@Retryable移动到第一级方法(如getSubscriberAccount),它就开始工作了.

示例如下:

@Service
public class SphIptvClient extends WebServiceGatewaySupport {
    //Works over here
    @Retryable(maxAttempts=3,backoff=@Backoff(delay=100))
    public GetSubscriberAccountResponse getSubscriberAccount(String loginTocken,String billingServId) {

        GetSubscriberAccountResponse response = (GetSubscriberAccountResponse) sphRemoteCall(sphIptvendPoint,getSubAcc,"xxxxx");
        return response;
    }

    /*
     * Retryable is not working on the 2nd level methods in the bean. 
     * It works only with methods which are called directly from outside
     * if there is 2nd level method,like this,Retryable is not working.
     */
    //@Retryable
    private Object sphRemoteCall(String uri,Object requestPayload,String soapAction) {
        log.debug("Calling the sph for uri:{} and soapAction:{}",uri,soapAction);
        return getwebservicetemplate().marshalSendAndReceive(uri,requestPayload,new SoapActionCallback(soapAction));
    }
}

@Configuration
@EnableRetry
public class SphClientConfig {
    @Bean
    public SphIptvClient sphIptvClient() {
        SphIptvClient client = new SphIptvClient();
        return client;
    }
}
最佳答案
所以这是一个超级迟到的答案,但由于我刚刚来到这里遇到同样的问题(再次,多年前与交易摔跤),我将提供一个更加充实的解决方案,希望有人会发现它有用.可以说@M. Deinum的诊断是正确的.

enter image description here

enter image description here

在上面的例子中,并且为了解释Understanding AOP proxies,SphIptvClient获得自动装配的任何地方都将被赋予对Spring Retry在处理@EnableRetry时将创建的代理的引用:

“The @EnableRetry annotation creates proxies for @Retryable beans” – 07004

一旦调用了getSubscriberAccount并且执行已经通过代理并进入对象的@Service实例,就不会知道对代理的引用.因此,调用sphRemoteCall就好像根本没有@Retryable一样.

您可以通过以允许getSubscriberAccount调用代理编辑的sphRemoteCall的方式对代码进行移植来处理框架,这需要新的接口和类实现.

例如:

public interface SphWebService {
   Object sphRemoteCall(String uri,String soapAction);
}

@Component
public class SphWebServiceImpl implements SphWebService {
   @Retryable
   public Object sphRemoteCall(String uri,String soapAction) {
       log.debug("Calling the sph for uri:{} and soapAction:{}",soapAction);
       return getwebservicetemplate().marshalSendAndReceive(uri,new SoapActionCallback(soapAction));
   }
}

@Service
public class SphIptvClient extends WebServiceGatewaySupport {

   @Autowired
   SphWebService sphWebService;

   @Retryable(maxAttempts=3,backoff=@Backoff(delay=100))
   public GetSubscriberAccountResponse getSubscriberAccount(String loginTocken,String billingServId) {
       GetSubscriberAccountResponse response = (GetSubscriberAccountResponse) this.sphWebService.sphRemoteCall(sphIptvendPoint,"xxxxx");
       return response;
   }
}

@Configuration
@EnableRetry
public class SphClientConfig {
   // the @Bean method was unnecessary and may cause confusion. 
   // @Service was already instantiating SphIptvClient behind the scenes.
}

相关文章

这篇文章主要介绍了spring的事务传播属性REQUIRED_NESTED的原...
今天小编给大家分享的是一文解析spring中事务的传播机制,相...
这篇文章主要介绍了SpringCloudAlibaba和SpringCloud有什么区...
本篇文章和大家了解一下SpringCloud整合XXL-Job的几个步骤。...
本篇文章和大家了解一下Spring延迟初始化会遇到什么问题。有...
这篇文章主要介绍了怎么使用Spring提供的不同缓存注解实现缓...