我们可以在Spring Batch中配置两个retryConfigurations类以在Tasklet中使用吗?我的资料很复杂,因此需要两种不同的重试机制。只是想了解应该调用哪个重试模板?
有人请给我快速指点吗?
@EnableRetry
@Configuration
@PropertySource("classpath:config.properties")
public class ABCRetryConfigurations {
@Value("${retryPeriod}")
private int retryIntervalInSeconds;
@Bean
public RetryTemplate retryTemplate() {
SimpleRetryPolicy retryPolicy = new SimpleRetryPolicy();
retryPolicy.setMaxAttempts(3);
FixedBackOffPolicy backOffPolicy = new FixedBackOffPolicy();
backOffPolicy.setBackOffPeriod(retryIntervalInSeconds * 1000);
RetryTemplate template = new RetryTemplate();
template.setRetryPolicy(retryPolicy);
template.setBackOffPolicy(backOffPolicy);
return template;
}
}
和
@EnableRetry
@Configuration
@PropertySource("classpath:config.properties")
public class XYZRetryConfigurations {
@Value("${retryPeriod}")
private int retryIntervalInSeconds;
@Bean
public RetryTemplate XXXXXRetryTemplate() {
SimpleRetryPolicy retryPolicy = new SimpleRetryPolicy();
retryPolicy.setMaxAttempts(3);
FixedBackOffPolicy backOffPolicy = new FixedBackOffPolicy();
backOffPolicy.setBackOffPeriod(retryIntervalInSeconds * 1000);
RetryTemplate template = new RetryTemplate();
template.setRetryPolicy(retryPolicy);
template.setBackOffPolicy(backOffPolicy);
return template;
}
}
这是我的代码吗?
public class XXXXXXXX {
private static Logger logger = Logger.getLogger(XXXXXXXXX.class);
@Autowired
private RetryTemplate retryTemplate;
private Set<String> abcFileNames;
private ChunkContext paramChunkContext;
public XXXXXXXX(Set<String> abcFileNames, SendXXXTasklet tasklet, ChunkContext paramChunkContext) {
this.abcFileNames = abcFileNames;
this.tasklet = tasklet;
this.paramChunkContext = paramChunkContext;
}
public RepeatStatus executeXXXXRetry() {
// Retry configuration
retryTemplate.setBackOffPolicy( fixedBackOffPolicy );
return retryTemplate.execute(context -> {
return tasklet.XXX(abcFileNames, paramChunkContext);
});
}
}
答案 0 :(得分:0)
只是想了解应该调用哪个重试模板?
应该告诉您哪个决定是通过告诉Spring容器哪个重试模板自动装配来决定的。在您的示例中,您有两个RetryTemplate
bean(retryTemplate
和billingRetryTemplate
),并且您想在RetryTemplate retryTemplate
类中自动装配XXXXXXXX
。在您的情况下,retryTemplate
bean将按名称自动装配(默认情况下,方法名称为bean名称)。您可以使用@Primary
或@Qualifier
来更明确地说明要使用哪个。
您可以在Spring Framework文档中找到关于此的更多详细信息:https://docs.spring.io/spring/docs/current/spring-framework-reference/core.html#beans-java