spring - 每次 Spring Retry 尝试更改参数值
问题描述
我有以下方法是@Retryable:
@Retryable(value = HttpClientErrorException.class)
public Integer callToExternalService(DateTime start, DateTime end) throws MyException
我的问题是我是否可以在每次重试方法时修改输入参数,因为我需要使用不同的值进行休息调用。有没有类似于@Recover
这种情况的选项?
解决方案
不是开箱即用;您需要添加比重试拦截器更接近 bean 的另一个建议来修改MethodInvocation.arguments
.
或者您可以子类化重试拦截器并覆盖该invoke
方法。
两者都不是微不足道的,除非您对 Spring 代理有一些了解。
这是一种更简单的方法;如果您需要恢复而不是向调用者抛出最后一个异常,则需要做更多的工作。
@SpringBootApplication
@EnableRetry
public class So61486701Application {
public static void main(String[] args) {
SpringApplication.run(So61486701Application.class, args);
}
@Bean
MethodInterceptor argumentChanger() {
RetryTemplate retryTemplate = new RetryTemplate();
SimpleRetryPolicy retryPolicy = new SimpleRetryPolicy(3);
FixedBackOffPolicy backOff = new FixedBackOffPolicy();
backOff.setBackOffPeriod(1000L);
retryTemplate.setRetryPolicy(retryPolicy);
retryTemplate.setBackOffPolicy(backOff);
return invocation -> {
return retryTemplate.execute(context -> {
if (context.getRetryCount() > 0) {
Object[] args = invocation.getArguments();
args[0] = ((Integer) args[0]) + 1;
args[1] = ((String) args[1]) + ((String) args[1]);
}
return invocation.proceed();
});
};
}
@Bean
public ApplicationRunner runner(Foo foo) {
return args -> foo.test(1, "foo.");
}
}
@Component
class Foo {
@Retryable(interceptor = "argumentChanger")
public void test(int val, String str) {
System.out.println(val + ":" + str);
throw new RuntimeException();
}
}
推荐阅读
- php - 将 ACF Post Object 字段添加到 Wordpress 管理列
- r - processR包的modSummary函数返回NULL
- floris - 从 floris v1.1.4 开发分支运行优化示例时出现 ModuleNotFoundError
- mysql - 无法在 aws amazon linux v2 内的 django 视图函数中检索或创建数据库对象
- python - 如何解决“TypeError: max() received an invalid combination of arguments - got (Linear, int), but expected”?
- javascript - 调查流畅快速的 UI,使用 Svelte 3 的疑问
- gatsby - gatsby-plugin-mdx 上标和下标支持
- shell - 如何在 Shell 脚本中将日期格式 MM-DD-YY HH:MI AM/PM 转换为 YYYY-MM-DD
- idyll - 在 Idyll 中使 js 函数可访问的方法?
- python - 如果值介于其他两个值之间,则做某事并继续,直到不在。如果不是,再试一次,直到它是,然后在它不是时停止