加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

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

发布时间:2020-12-15 01:46:20 所属栏目:大数据 来源:网络整理
导读:@Retryable似乎没有像下面的sphRemoteCall那样在第二级方法上工作.我看到创建了一个代理,但它永远不会在失败时重试. 一旦我将@Retryable移动到第一级方法(如getSubscriberAccount),它就开始工作了. 示例如下: @Servicepublic class SphIptvClient extends W

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读