问题
I have a spring-batch xml-based configuration that should be migrated to annotation-based configuration.
but I can't find any solution to define a chunk into the tasklet definition.
There are my xml and code base decleration:
<step id="files2Memory">
<tasklet>
<chunk reader="pointFileReader" processor="pointFileProcessor"
writer="pointFileWriter" commit-interval="50000"/>
</tasklet>
</step>
public Step files2Memory() {
return stepBuilders.get("files2Memory")
.tasklet(new Tasklet() {
@Override
public RepeatStatus execute(StepContribution stepContribution, ChunkContext chunkContext) throws Exception {
return null;
}
})
.chunk(50000)
.reader(new PointFileReader())
.writer(new PointFileWriter())
.processor(new PointFileProcessor())
.build();
}
Best Regards
回答1:
Mark PointFileReader and PointFileWriter PointFileProcessor with @Component, use @Scope("step") if required. Make Sure component scan cover them.
In you batch config file autowried the above.
@Autowired
private PointFileWriter pointFileWriter ;
@Autowired
private PointFileReader pointFileReader ;
@Autowired
private ItemProcessor<TypeFromReader, TypeForWriter> pointFileProcessor ;
@Autowired
private PlatformTransactionManager transactionManager;
...
@Bean
protected Step files2Memory(){
return steps
.get("files2Memory")
.transactionManager(transactionManager)
.<TypeFromReader, TypeForWriter> chunk(5000)
.reader(pointFileReader )
.processor(pointFileProcessor)
.writer(pointFileWriter).build();
}
If one of the PointFileReader and PointFileWriter PointFileProcessor can not be specif as PointFileReader and PointFileWriter PointFileProcessorPointFileReader and PointFileWriter @Component you can defined them as @Beans and replace fields with method call
e.g.
@Bean
protected ItemReader<TypeFromReader> pointFileReader() {
PointFileReader <TypeFromReader> reader = new PointFileReader <TypeFromReader>();
reader.set(...)
return reader;
}
回答2:
just remove the .tasklet part and if you want to be typesafe, use .<TypeFromReader, TypeForWriter>chunk
.tasklet is for custom Tasklets, .chunk already provides you the (hidden) chunkbased implementation
来源:https://stackoverflow.com/questions/26907419/how-define-a-spring-batch-chunk-in-the-tasklet-with-code