-1

在我的应用程序中,我正在调用 Web 服务。所以我想实现 Spring Retry 机制,让处理更健壮,更不容易失败。这是我第一次使用 Spring Retry

我创建了一个应用程序服务类,我在其中声明了 RetryTemplate 并设置了 RetryPolicy。

但它抛出了语法错误

"Multiple markers at this line
- Syntax error, insert "Identifier (" to complete MethodHeaderName
- Syntax error on token ".", @ expected after this token
- Syntax error, insert ")" to complete MethodDeclaration" 

即使我使用 ctrl+space 它也没有显示 setRetryPolicy() 方法。

下面是我的课:

import java.util.Collections;

import org.springframework.retry.policy.SimpleRetryPolicy;

import org.springframework.retry.support.RetryTemplate;

public class ApplicationServiceRetry {

    SimpleRetryPolicy policy = new SimpleRetryPolicy(5,Collections
            .<Class<? extends Throwable>, Boolean> singletonMap(Exception.class, true));
    RetryTemplate template = new RetryTemplate();
    template.setRetryPolicy(policy);  //it's throwing error here

}

我指的是http://docs.spring.io/spring-batch/reference/html/retry.html。这里我使用的是 Spring-retry 1.1.5.RELEASE。

4

1 回答 1

0

??

template.setRetryPolicy(policy);

您不能只是将任意代码放在一个类中 - 它必须在一个方法或一个初始化块中......

public class ApplicationServiceRetry {

    SimpleRetryPolicy policy = new SimpleRetryPolicy(5,
            Collections.<Class<? extends Throwable>, Boolean> singletonMap(Exception.class, true));

    RetryTemplate template = new RetryTemplate();

    {
        template.setRetryPolicy(policy);
    }

}
于 2017-06-23T20:38:11.553 回答