实际上,我通过以下几个步骤和两个tasklet来支持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();
}
这是我的自定义tasklet
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删除的数据就会消失,我无法恢复它。 有没有办法在整个Job上执行回滚或在特定步骤/ tasklet之前回滚?
答案 0 :(得分:0)
Spring Batch中没有回滚整个作业或步骤的概念。但是,您可以使用侦听器(JobExecutionListener
或StepExecutionListener
)来执行补偿逻辑以手动执行回滚。