java - 如何实现SpringRetry

标签 java spring-boot

我有2个类。一个是Msgcontroller.java,另一个类是RetryProcess.java

在Msgcontroller.java中,我使用以下方法

public String getSampleService() throws IOException,
            UnsupportedEncodingException, HttpException {
        // Read from request

        StringBuilder buffer = new StringBuilder();
        BufferedReader reader = request.getReader();
        String line;
        while ((line = reader.readLine()) != null) {
            buffer.append(line);
        }
        String data = buffer.toString();
        StringRequestEntity requestEntity = null;
        StringBuffer eventResponse = new StringBuffer();
        HttpClient httpclient = new HttpClient();
        InputStream inputStream = null;
        int statusCode;
        BufferedReader br = null;
        String jsonReceive = null;
        logger.info("RequestJson" + data);
        baseServiceUrl = "http://localhost:8080/oif";

        apiServiceUrl = "/services/rest/item";

        requestEntity = new StringRequestEntity(data, MEDIA_TYPE, FORMAT);
        PostMethod postMethod = new PostMethod(baseServiceUrl + apiServiceUrl);
        postMethod.setRequestEntity(requestEntity);
        statusCode = httpclient.executeMethod(postMethod);
        logger.info("Status code from item service call" + statusCode);
        inputStream = postMethod.getResponseBodyAsStream();
        if (null != inputStream) {
            br = new BufferedReader(new InputStreamReader(inputStream, FORMAT));
            for (line = br.readLine(); line != null; line = br.readLine()) {
                eventResponse = eventResponse.append(line);
            }
        }
        jsonReceive = eventResponse.toString();
        logger.info("JsonReceive" + jsonReceive);
        return jsonReceive;
    }

并在RetryProcess.java中

public class RetryProcess {
    private static final Logger logger  = LogManager.getLogger(RetryProcess.class);
@Retryable(
        value={Exception.class},
        maxAttempts=2,
        backoff=@Backoff(delay=5000)
        )
public void getSplSevice() throws UnsupportedEncodingException, HttpException, IOException{
    Msgcontroller msg=new Msgcontroller ();
    logger.info("Retryprocess");
    msg.getSampleService();
}
}

但是当 MsgController 中的 getSampleService() 发生异常时,RetryProcess.java 不起作用。任何人都可以帮助我

最佳答案

如果 @Retrayble 抛出异常,您必须使用 @Recover 这是一个后备方法。在这种情况下,您可以使用带有 @Recover 注释和异常详细信息的另一种方法。方法结构如下。

@Recover
    void recover(Exception e, String someParams) {
... other code
}

欲了解更多详情,您可以查看此链接。 https://www.baeldung.com/spring-retry

关于java - 如何实现SpringRetry,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61905355/

相关文章:

java - Spring Jpa save() 从子实体中删除列

spring-boot - 无法发布具有许多(超过 256 个)值的表单

java - 具有多种情况的 Thymeleaf switch 语句

java - Spring Cloud Gateway IPv4地址无效

jersey - Spring-boot 指标对 RESTful 服务的每个 requestURI 进行计数和测量

performance - Spring Boot在Raspberry PI上启动缓慢

java - 在 Java 中向 JComboBox 添加项目的问题

java - elasticsearch中更新文档的效率

java - GL绘图纹理绑定(bind)错误

java - 有没有比 Math.pow 更好(更正确)的方法来计算某些幂的模数?