我已经配置了一个块大小为10的批处理作业。是否可以在运行时读取chunksize?
作为一个例子,我已经实现了以下ChunkListener来说明我的问题:
public class MyChunkListener implements ChunkListener {
private final Logger logger = LoggerFactory.getLogger(this.getClass());
private long startTimeInMs;
@Override
public void beforeChunk(ChunkContext context) {
startTimeInMs = System.currentTimeMillis();
}
@Override
public void afterChunk(ChunkContext context) {
long duration = System.currentTimeMillis() - startTimeInMs;
long chunkSize = 10;
logger.info(String.format(
"Chunk of size %s computed in %sms", chunkSize, duration));
是否可以动态访问afterChunk方法中的chunksize?我想做像
这样的事情long chunkSize = context.getChunkSize();
Joe Chiavaroli提出的解决方案适用于我的情况:只需在BatchConfig中注入chunksize并通过构造函数参数将其传递给ChunkListener:
@Configuration
@EnableBatchProcessing
public class MyBatchConfig {
@Value("${batch.mystep.chunksize}")
private int chunkSize;
@Autowired
private JobBuilderFactory jobs;
@Autowired
private StepBuilderFactory steps;
@Bean
protected Step step(MyReader reader, MyProcessor processor, MyWriter writer) {
return steps.get("myStep")
.<InputItem, ProcessedItem> chunk(chunkSize)
.reader(reader)
.processor(processor)
.writer(writer)
.listener(new MyChunkListener(chunkSize))
.build();