从方法调用时@Retryable不起作用

时间:2019-02-26 05:37:12

标签: spring-boot aop spring-retry

下面是我的应用程序类。从这里开始,从formatted_desc_split = ['Akshay Godase is from pune', 'Amar:Satara', 'Sandesh:Solapur', 'Mahesh:Nagpur', 'Prashant:Indapur'] my_dict = {} for each_split_data in formatted_desc_split: split_by_colon = each_split_data.split(":") if len(split_by_colon) == 2: my_dict[split_by_colon[0]] = split_by_colon[1] print(my_dict) DEToken类的流程,我调用DEToken,这里有RestConnection方法。

@retryable

DEToken类:来自@SpringBootApplication @EnableRetry public class SpringBootTrfficApplication implements CommandLineRunner { Enter code here @Autowired DEToken deToken; @Autowired SyncService syncService; public static void main(String[] args) { SpringApplication.run(SpringBootTrfficApplication.class, args); } @Override public void run(String... args) throws Exception { deToken.getToken(); } } 的我正在使用getToken方法的地方调用RestConnect

@Retrable

我的REST连接类具有@Service public class DEToken { private Logger logger = LogManager.getLogger(getClass()); @Autowired RestConnection restConnection; @Autowired private Environment env; public String accessToken; public void getToken() { System.out.println("hello from get token"); //String getJsonPayload = "{\"Query\":{\"RegisterExtensionWithDE\":{\"pid\": \"\",\"providerInsName\":" + //env.getProperty("provider.ins") + "}}}"; //String str = restConnection.restPost( // env.getProperty("rest.de.url"), getJsonPayload); try { String getJsonPayload = "{\"Query\":{\"RegisterExtensionWithDE\":{\"pid\": \"\",\"providerInsName\":" + env.getProperty("provider.ins") + "}}}"; StringBuffer tokenResult = restConnection.restPost(env.getProperty("rest.de.url"), getJsonPayload); System.out.println(tokenResult); JSONObject xmlJSONObj = XML.toJSONObject(tokenResult.toString()); JSONObject registration = new JSONObject(); if (xmlJSONObj.has("Registration")) { registration = xmlJSONObj.getJSONObject("Registration"); if (registration.has("accessToken")) { accessToken = registration.get("accessToken").toString(); } else logger.info("no accessToken from DE"); } else logger.info("no Registration object from DE"); } catch (Exception e) { logger.error("Exception while fetching accesstoken from DE "); logger.error(e.getMessage()); } } } 方法:

retryable

1 个答案:

答案 0 :(得分:1)

考虑到您看到函数restPost()的实现,

@Retryable(value = {IOException.class, ConnectException.class},
           maxAttempts = 4,
           backoff = @Backoff(5000))
public StringBuffer restPost(String restUrl, String payload) {
    try {
        // Your code
    }
    catch(IOException ex){ // These catch block handles the exception
                           // and nothing to throw to retryable.
    }
    catch(MalformedURLException ex){ // More catch blocks that you
                                     // define to handle exception.
    }
}

在这里,您可以处理所有可能导致撤销retryrecover方法的异常。

  

注意:可恢复方法仅在引发异常时执行,而不由任何try-catch块处理。

方法restPost()引发的异常均由方法try-catch自身处理,没有任何被catch块抛出的异常。

现在,Spring-Retry无法获得任何异常(因为它是由方法try-catch块处理的)。因此,将不会执行 recovery 方法。

  

解决方案:您应该从要执行重试恢复的方法定义中删除这些捕获块。

请做些有需要的事情,它将像魅力一样发挥作用...:)

相关问题