Actually I jave a Job with several steps and two tasklets as follow:
@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();
}
Here my custom 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;
}
}
This is my configuration with the main job.
@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();
}
}
Actually if a step (stepItem or stepCriticalComponent) fails,data that is deleted by the tasklet is gone and I cannot recover it. Is there a way to do a rollback on the entire Job or a rollback before a specific step/tasklet?
There is no concept of a rollback of an entire job or step within Spring Batch. However, you can use a listener ( JobExecutionListener
or StepExecutionListener
) to execute compensating logic to do a rollback by hand.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.