微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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.
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐