I have standard Step with reader, one processor and writer. Reader is reading A class objects from database table (say table1) using JpaPagingItemReaderBuilder:
@Bean
public ItemReader<A> aItemReader(EntityManagerFactory entityManagerFactory) {
return new JpaPagingItemReaderBuilder<A>().name("Areader")
...
.build();
}
then I have processor that maps A class objects to B class objects
@Bean
public ItemProcessor<A, B> itemProcessor() {
return item -> {
...
// returns B object
};
}
and writer that writes B class objects to another db table (say table2)
@Component
@RequiredArgsConstructor
@StepScope
public class BEntityWriter implements ItemWriter<B> {
private final Brepository brepository;
@Override
public void write(List<? extends B> items) {
brepository.persistAll(items);
}
}
I have defined a step:
stepFactory.get("name")
.<A, B>chunk(SIZE)
.reader(aItemReader)
.processor(itemProcessor)
.writer(bEntityWriter)
.faultTolerant()
.skip(Throwable.class)
.skipLimit(LIMIT)
.build();
In case of any exception during processing/writing of a record, I want to skip that record and mark it in A table (table1) as failing - lets say A class has status field with corresponding table1 column and I want to set it to FAILED.
I think it can be achieved by using SkipListener, I am able to access A class object in
@Override
@Transactional(propagation = Propagation.REQUIRES_NEW)
public void onSkipInWrite(A item, Throwable t) {
A a = aRepository.findById(item.getId())
.orElseThrow(RuntimeException::new);
a.setStatus("FAILED");
}
but I am not able to access when there is a failure on write level, I only can access B object:
public void onSkipInWrite(B item, Throwable t)
The solution I have found is to pass A object in some kind of wrapper class to Writer along with B object, then I can operate on wrapper class both on Writer and SkipListener, but this does not look nice. I was thinking about using StepContext, but I only want to put there failing records.
Is there any other way I can access reader objects (in my case A class objects) in SkipListener?