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

前端之家收集整理的这篇文章主要介绍了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.
}
原文链接:https://www.f2er.com/spring/431350.html

猜你在找的Spring相关文章