春季 RetryTemplate 返回使用

huangapple go评论80阅读模式
英文:

Spring RetryTemplate return usage

问题

例如,我有一个Spring RetryTemplate配置:

@Configuration
@EnableRetry
public class RetryTemplateConfig {

    @Bean
    public RetryTemplate retryTemplate() {
        SimpleRetryPolicy retryPolicy = new SimpleRetryPolicy();
        retryPolicy.setMaxAttempts(5);
        FixedBackOffPolicy backOffPolicy = new FixedBackOffPolicy();
        backOffPolicy.setBackOffPeriod(300000);
        RetryTemplate template = new RetryTemplate();
        template.setRetryPolicy(retryPolicy);
        template.setBackOffPolicy(backOffPolicy);
        return template;
    }
}

我希望在捕获异常时重新调用这个方法:

@Scheduled(cron = "${schedule.cron.update}")
public void calculate() throws Exception {
    log.info("Scheduled started");
    try {
        retryTemplate.execute(retryContext -> {
            myService.work();
            return true;
        });
    } catch (IOException | TemplateException e) {
        log.error(e.toString());
    }
    log.info("Scheduled finished");
}

因此,我的服务类中的work()方法可能会抛出异常:

public void send() throws IOException, TemplateException {
    // ...
}

看起来好像可以正常工作,但我真的不理解下面的代码是什么意思:

retryTemplate.execute(retryContext -> {
    myService.work();
    return true;
});

为什么我可以返回truenullnew Object()和其他东西?它会影响什么,将在何处使用?我应该返回什么?

英文:

For example I've a Spring RetryTemplate configuration:

@Configuration
@EnableRetry
public class RetryTemplateConfig {

    @Bean
    public RetryTemplate retryTemplate() {
        SimpleRetryPolicy retryPolicy = new SimpleRetryPolicy();
        retryPolicy.setMaxAttempts(5);
        FixedBackOffPolicy backOffPolicy = new FixedBackOffPolicy();
        backOffPolicy.setBackOffPeriod(300000);
        RetryTemplate template = new RetryTemplate();
        template.setRetryPolicy(retryPolicy);
        template.setBackOffPolicy(backOffPolicy);
        return template;
    }
}

And I want to re-invoke this method if exception was caught:

@Scheduled(cron = "${schedule.cron.update}")
    public void calculate() throws Exception {
        log.info("Scheduled started");
        try {
            retryTemplate.execute(retryContext -> {
                myService.work();
                return true;
            });
        } catch (IOException | TemplateException e) {
            log.error(e.toString());
        }
        log.info("Scheduled finished");
    }

So, my method work() in service class can throw exceptions:

 public void send() throws IOException, TemplateException {
        ...
    }

Seems like it works okay, but I really don't understand what does next code mean:

retryTemplate.execute(retryContext -> {
                myService.work();
                return true;
            });

Why I can return true, null, new Object() and other stuff? What it affects and where will it be used? What should i return?

答案1

得分: 2

RetryTemplate执行RetryCallback,它是通用的,并且可以返回您定义的任何返回类型。

如果您需要从成功的执行中获取数据,您可以在回调中返回它,并在流程中稍后获取它。

返回:
成功操作的结果。

使用示例:读取带有重试的文件:

return template.execute(context -> {
FileUtils.copyURLToFile(new URL(path), copy);
return FileUtils.readFileToString(copy, Charset.defaultCharset());

英文:

RetryTemplate executes RetryCallback which is generic and can return any return type you define.

If you need to get data from successful execution you can return it in callback and get it later in flow

>Returns:
the result of the successful operation.

Example of reading file with retry:

> return template.execute(context -> {
> FileUtils.copyURLToFile(new URL(path), copy);
> return FileUtils.readFileToString(copy, Charset.defaultCharset());

huangapple
  • 本文由 发表于 2020年10月12日 16:13:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/64314011.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定