簡體   English   中英

回滾春季批作業

[英]Rollback Spring Batch Job

實際上,我通過以下幾個步驟和兩個小任務完成了一份工作:

@Bean
public Step stepItem() {
    return stepBuilderFactory.get("stepItem")
            .<Item, Item> chunk(10)
            .reader(itemReader())
            .processor(itemProcessor())
            .faultTolerant()
            .writer(itemWriter())
            .build();
}

@Bean
public Step deleteAllItemStep() {
    return stepBuilderFactory.get("deleteAllItemStep")
            .tasklet(itemStepDeleteAllTasklet())
            .build();
}

這是我的自定義小任務

public class ItemStepDeleteAllTasklet implements Tasklet{

private ItemRepository itemRepository;

public ItemStepDeleteAllTasklet(ItemRepository itemRepository) {
    this.itemRepository = itemRepository;
}

@Override
public RepeatStatus execute(StepContribution stepContribution, ChunkContext chunkContext) throws Exception {
    itemRepository.deleteAllInBatch();
    return null;
}
}

這是我的主要工作配置。

@Configuration
@Import({BatchItemConfiguration.class, BatchCriticalComponentConfiguration.class})
public class BatchConfiguration {

@Autowired
private JobBuilderFactory jobBuilderFactory;

@Autowired
private Step stepCriticalComponent;

@Autowired
private Step stepItem;

@Autowired
private Step deleteAllItemStep;

@Autowired
private Step deleteAllCriticalComponentStep;

@Bean
public Job importJob(JobCompletionNotificationListener listener) {
    return jobBuilderFactory.get("importJob")
            .incrementer(new RunIdIncrementer())
            .listener(listener)
            .start(deleteAllCriticalComponentStep)
            .next(deleteAllItemStep)
            .next(stepItem).next(stepCriticalComponent)
            .build();
}

}

實際上,如果某個步驟(stepItem或stepCriticalComponent)失敗,則由tasklet刪除的數據將消失,而我將無法恢復它。 有沒有辦法對整個作業進行回滾,或者在特定步驟/任務集之前進行回滾?

Spring Batch中沒有回滾整個作業或步驟的概念。 但是,您可以使用偵聽器( JobExecutionListenerStepExecutionListener )執行補償邏輯做手工回滾。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM