如下是报错信息:
org.springframework.batch.item.ItemStreamException: Failed to initialize the reader
at org.springframework.batch.item.support.AbstractItemCountingItemStreamItemReader.open(AbstractItemCountingItemStreamItemReader.java:147)
at org.springframework.batch.item.database.JdbcPagingItemReader.open(JdbcPagingItemReader.java:260)
at org.springframework.batch.item.support.CompositeItemStream.open(CompositeItemStream.java:96)
at org.springframework.batch.core.step.tasklet.TaskletStep.open(TaskletStep.java:310)
at org.springframework.batch.core.step.AbstractStep.execute(AbstractStep.java:197)
at org.springframework.batch.core.partition.support.TaskExecutorPartitionHandler$1.call(TaskExecutorPartitionHandler.java:139)
at org.springframework.batch.core.partition.support.TaskExecutorPartitionHandler$1.call(TaskExecutorPartitionHandler.java:136)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.IllegalStateException: Cannot open an already opened ItemReader, call close first
at org.springframework.util.Assert.state(Assert.java:70)
at org.springframework.batch.item.database.AbstractPagingItemReader.doOpen(AbstractPagingItemReader.java:133)
at org.springframework.batch.item.support.AbstractItemCountingItemStreamItemReader.open(AbstractItemCountingItemStreamItemReader.java:144)
代码配置:参考网上的示例是可以使用异步线程的。
示例网址:http://blog.csdn.net/weiyanghuadi/article/details/9618389
@Bean(name = "partitionerJob")
public Job partitionerJob() {
return jobBuilderFactory.get("partitionerJob")
.incrementer(new RunIdIncrementer())
.start(partitionerStepMaster())
.build();
}
@Bean(name = "partitionerStepMaster")
public Step partitionerStepMaster(){
return stepBuilderFactory.get("partitionerStepMaster")
.partitioner("rangePartitioner",rangePartitioner)
// .partitionHandler(partitionHandler())
.step(partitionerStepSlave())
.gridSize(5)
.taskExecutor(new SimpleAsyncTaskExecutor()) //异步线程 就会报错,
.build();
}
@Bean
public PartitionHandler partitionHandler() {
TaskExecutorPartitionHandler retVal = new TaskExecutorPartitionHandler();
retVal.setTaskExecutor(new SimpleAsyncTaskExecutor());
retVal.setStep(partitionerStepSlave());
retVal.setGridSize(5);
return retVal;
}
@Bean(name = "partitionerStepSlave")
public Step partitionerStepSlave(){
return stepBuilderFactory.get("partitionerStepSlave")
//.tasklet(partitionerStepSlaveTasklet())
.<RepayOrderInfo, RepayOrderInfo> chunk(100)
.reader(partitionerStepSlaveReader()) //继承JdbcPagingItemReader
.processor(partitionerStepSlaveProcessor())
.build();
}
<p>自己的问题自己答吧。报错的原因应该是 reader的作用域不同,需要在配置reader的地方加上step作用域。如果要获取作业上下文的参数也要加上这个作用域注解。在将xml配置转换成java配置的时候不够仔细,没有看到作用域的配置。</p>
@Bean(name = "partitionerStepSlaveReader") @StepScope public PartitionerStepSlaveReader partitionerStepSlaveReader(){ return new PartitionerStepSlaveReader(); }
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。