[英]How to configure fault tolerance programmatically for a spring tasklet (not a chunk)
以編程方式為塊配置容錯的工作方式如下:
stepBuilders.get("step")
.<Partner,Partner>chunk(1)
.reader(reader())
.processor(processor())
.writer(writer())
.listener(logProcessListener())
.faultTolerant()
.skipLimit(10)
.skip(UnknownGenderException.class)
.listener(logSkipListener())
.build();
訣竅是,通過添加“塊”,鏈切換到提供“容錯”方法的 SimpleStepBuilder。
我的問題是,如果您只有一個 tasklet(沒有讀取器、處理器、寫入器),該怎么做?
定義 tasklet 的工作方式如下:
stepBuilders.get("step")
.tasklet(tasklet())
.build();
“tasklet”的使用切換到不提供“faultTolerant”方法的 TaskletStepBuilder。 因此,我看不出如何定義諸如 skipLimit 之類的屬性。
有任何想法嗎?
Tasklet
沒有要跳過的“項目”的概念,因此容錯僅對面向塊的步驟有意義。 我建議您在原始版本中使用 Spring Retry(1.1.0.RELEASE 現在已經發布,並且您有一些流暢的構建器選項和@Retryable
注釋)。
根據@DaveSyer 給出的指導並使用org.springframework.retry:spring-retry:1.1.0.RELEASE這就是我最終得到的結果:
Tasklet tasklet = // whatever
ProxyFactory proxyFactory = new ProxyFactory(Tasklet, new SingletonTargetSource(tasklet));
proxyFactory.addAdvice(RetryInterceptorBuilder.stateless()
.maxAttempts(3)
.build());
Step step = stepBuilderFactory.get("taskName")
.tasklet((Tasklet)proxyFactory.proxy)
.build();
我唯一仍在苦苦掙扎的是,如果我想吞下導致超過maxAttempts
后重試的異常。 如果我將ExceptionHandler
添加到步驟中,則永遠不會重試該步驟。 我想這意味着異常處理程序位於切入點內,我覺得這有點令人驚訝。
//you need to have a bean in order to define the retry policies
@Configuration
@EnableAutoConfiguration
public class MyRetryBean{
@Autowired
private RetryProperties properties;
private RetryTemplate retryTemplate;
@Bean
public RetryTemplate initializeRetryTemplate() {
Map<Class<? extends Throwable>, Boolean> retryExceptions = Collections.singletonMap(IOException.class,
Boolean.TRUE);
SimpleRetryPolicy policy = new SimpleRetryPolicy(properties.getMaxAttempt(), retryExceptions);
retryTemplate = new RetryTemplate();
retryTemplate.setRetryPolicy(policy);
FixedBackOffPolicy backOffPolicy = new FixedBackOffPolicy();
backOffPolicy.setBackOffPeriod(properties.getBackoffPeriod());
retryTemplate.setBackOffPolicy(backOffPolicy);
return retryTemplate;
}
public RetryTemplate getRetryTemplate() {
return retryTemplate;
}
}
// in spring batch steps config
@Autowire
private MyRetryBean retryBean;
stepBuilders.get("step")
.tasklet(new MyTasklet(retryBean))
.build();
// in your tasklet
public class MyTasklet implements Tasklet{
private MyRetryBean retry;
public MyTasklet (MyRetryBean aRetry) {
this.retry= aRetry;
}
@Override
public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws IOException {
retry.getRetryTemplate().execute(new RetryCallback<RepeatStatus, IOException>() {
@Override
public RepeatStatus doWithRetry(RetryContext context) throws IOException {
throw new IOException();
}
});
return null;
}
}
要回答你的問題,不,沒有支持:-(
我也非常喜歡 Tasklet 的 Spring Batch 重試支持。 老實說,我不明白他們為什么要實施它。 從 Tasklet 調用不穩定的系統是一個有效的用例。 我不想使用 @Retryable 因為 Spring Batch 對重試的支持更清晰。 所以我想出了這個解決方案:
@Bean
public Step setInboxMessageStatusToReceivedStep(OneItemReader oneItemReader, SetInboxMessageStatusToReceivedItemWriter setInboxMessageStatusToReceivedItemWriter) {
return this.stepBuilders.get("setInboxMessageStatusToReceivedStep")
.<String, String>chunk(1)
.reader(oneItemReader)
.writer(setInboxMessageStatusToReceivedItemWriter)
.faultTolerant()
.backOffPolicy(milliSecondsBetweenRetiesPolicy(this.milliSecondsBetweenReties))
.retryLimit(this.retryLimit)
.retry(Throwable.class)
.build();
}
/**
* Spring Batch has some cool things like retry or backOffPolicy. These things are only available for ItemReader + ItemWriter and not for Tasklets.
* This is a nice Hack to use these Spring Batch features for Tasklets like thinks wrapped into a ItemReader + ItemWriter.
*/
public class OneItemReader implements ItemReader<String> {
public static final String ONE_ITEM_READER_EXECUTION_CONTEXT_KEY = "ONE_ITEM_READER_EXECUTION_CONTEXT_KEY";
public static final String JOB_SUCCESSFUL_FINISHED = "ONE_ITEM_READER_EXECUTION_CONTEXT_VALUE_JOB_SUCCESSFUL_FINISHED";
private StepExecution stepExecution;
@BeforeStep
public void beforeStep(StepExecution stepExecution) {
this.stepExecution = stepExecution;
}
@Override
public String read() {
String isFirstTimeRead = (String) this.stepExecution.getExecutionContext().get(ONE_ITEM_READER_EXECUTION_CONTEXT_KEY);
if (isFirstTimeRead == null) {
return "dummy value just to ensure the writer is invoked";
} else {
// null will stop the reading
return null;
}
}
/**
* As the last action in a Step, this method must be called. It tells the OneItemReader to stop returning Items.
*/
public static void markDataAsRead(StepExecution stepExecution) {
stepExecution.getExecutionContext().put(ONE_ITEM_READER_EXECUTION_CONTEXT_KEY, JOB_SUCCESSFUL_FINISHED);
}
}
您的 ItemWriter 現在可以像 Tasklet 一樣工作。 它只被調用一次。
請注意,您必須調用 markDataAsRead(this.stepExecution); 在您的 ItemWriter 中。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.