StepBuilderFactory is deprecated and scheduled for removal in v5.2 in favor of using the StepBuilder.
I use StepBuilder to build my steps. Now I want to implement asynchronous processors using documentation
Everything works fine, I just want to increase performance by using AsyncItemProcessor and AsyncItemWriter
But, the StepBuilder does not take the AsyncItemProcessor and AsyncItemWriter as parameter in process(asyncItemProcessor)
and writer(asyncItemWriter)
Here is snippet of configuration code and what I get as errors in comment:
@Bean
public MyItemWriter myItemWriter() {
return new MyItemWriter();
}
@Bean
public AsyncItemWriter<MyItem> asyncItemWriter(MyItemWriter myItemWriter) {
AsyncItemWriter asyncItemWriter = new AsyncItemWriter();
asyncItemWriter.setDelegate(myItemWriter);
return asyncItemWriter;
}
@Bean
public MyItemProcessor myItemProcessor() {
return new MyItemProcessor();
}
@Bean
public AsyncItemProcessor<MyInputItem, MyItem> asyncMyItemProcessor(MyItemProcessor myItemProcessor) throws Exception {
AsyncItemProcessor<MyInputItem, MyItem> processor = new AsyncItemProcessor<>();
processor.setDelegate(myItemProcessor);
processor.setTaskExecutor(new SimpleAsyncTaskExecutor());
return processor;
}
@Bean
public Step archiveMiniDevisStep(JobRepository jobRepository,
PlatformTransactionManager transactionManager,
AsyncItemProcessor<MyInputItem, MyItem> asyncMyItemProcessor,
AsyncItemWriter<MyItem> asyncItemWriter
) {
return new StepBuilder("MY_AWESOME_STEP", jobRepository)
.<ArchiveDevisDTO, NodeDTO>chunk(chunkSize, transactionManager)
.reader(myReader())
.processor((ItemProcessor<? super MyInputItem, ? extends MyItem>) asyncMyItemProcessor) // Force me to cast here
.writer(asyncItemWriter) // error here, reaqured type : ItemWriter<? super MyItem>
.build();
}
How can I make it work ?
You should be able to write your method as such.
@Bean
public Step archiveMiniDevisStep(JobRepository jobRepository,
PlatformTransactionManager transactionManager,
AsyncItemProcessor asyncMyItemProcessor,
AsyncItemWriter asyncItemWriter
) {
return new StepBuilder("MY_AWESOME_STEP", jobRepository)
.<ArchiveDevisDTO, NodeDTO>chunk(chunkSize, transactionManager)
.reader(myReader())
.processor(asyncMyItemProcessor)
.writer(asyncItemWriter)
.build();
}
It might give you a warning in the IDE about an unchecked call
but I suspect it will just work.
The difference with the old StepBuilderFactory
approach is that the StepBuilderFactory
used an untyped StepBuilder
so didn't care about the generics. The new way with the StepBuilder
prefers generics but can lead to some oddities.