Spring可重试注释在mvc应用程序中不起作用

时间:2018-02-13 10:25:03

标签: java spring spring-retry

我是spring框架的新手。我已经开始在我的mvc应用程序中使用Retryable注释了。我在我的配置类中添加了@EnableRetry。

@Configuration
@EnableScheduling
@EnableRetry
class ApplicationConfig {

我有我的MachinesContainer类,我在其中调用其他一些REST API。在该方法中,我使用@Retryable注释并提供了配置。

@Retryable(value = {NullPointerException.class},maxAttempts = 3,backoff = @Backoff(2000))
public static void getMachineContainer(ResponseEntity<MachinesContainer> machinesContainer,String ipsByGeoUrl,HttpEntity<?> requestEntity) throws Exception {

    if(machinesContainer==null) {
        throw new NullPointerException();
    }

    machinesContainer = restTemplate
            .exchange(ipsByGeoUrl, HttpMethod.POST,
                    requestEntity, MachinesContainer.class);

}

它直接调用异常而不是调用“getMachineContainer”3次。

@Component
public class Query{
@Override
public MachinesContainer getIpsByGeo(String city, String state, String country) {


    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.TEXT_PLAIN);
    String query = bundle.getString("ipsByGeo.sqlQuery");

    // Build Map with params to IPsByGeo query
    HashMap<String, String> args = new HashMap<String, String>();
    args.put("cityKey", city.trim().toUpperCase());
    args.put("countryKey", country.trim());
    if (state != null && state.length() >= 1) {
        args.put("stateClause", " and r.state='" + state.trim() + "'");
    } else {
        args.put("stateClause", "");
    }

    //Generate query with values
    StrSubstitutor sub = new StrSubstitutor(args,"$","$");
    query = sub.replace(query);

    HttpEntity<?> requestEntity = new HttpEntity<String>(query, headers);

    String environment = System.getenv(SERVICE_ENVIRONMENT);
    logger.debug("getIpsByGeo service environment is:{}",environment);
    String ipsByGeoUrl = <Some API URL>;

    try {
        ResponseEntity<MachinesContainer> machinesContainer = null;

        getMachineContainer(machinesContainer,ipsByGeoUrl,requestEntity);

        return machinesContainer.getBody();
    }
    catch (Exception e) {
        throw e;
    }
}
}

请提出一些解决方案。

我使用的是spring版本 - 4.2.3.RELEASE Java版本 - 1.8 弹簧重试 - 1.2.1.RELEASE spring-aop - 4.2.5.RELEASE aspectjweaver - 1.8.8

1 个答案:

答案 0 :(得分:-1)

一些事情 1. @Retryable ia基于使用代理的Spring AOP,因此它不能使用私有方法调用(我的意思是同一类中的方法)
2.同样适用于静态方法。

相关问题