开发者社区> 问答> 正文

Spring batch 中分区partitioner,使用异步线程怎么使用Jd?400报错

如下是报错信息:

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();
    }

 

展开
收起
爱吃鱼的程序员 2020-06-07 18:35:29 988 0
1 条回答
写回答
取消 提交回答
  • https://developer.aliyun.com/profile/5yerqm5bn5yqg?spm=a2c6h.12873639.0.0.6eae304abcjaIB
                        <p>自己的问题自己答吧。报错的原因应该是 reader的作用域不同,需要在配置reader的地方加上step作用域。如果要获取作业上下文的参数也要加上这个作用域注解。在将xml配置转换成java配置的时候不够仔细,没有看到作用域的配置。</p> 
    

     

    @Bean(name = "partitionerStepSlaveReader")
    @StepScope
    public PartitionerStepSlaveReader partitionerStepSlaveReader(){
        return new PartitionerStepSlaveReader();
    }
    2020-06-07 18:35:51
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
云栖社区特邀专家徐雷Java Spring Boot开发实战系列课程(第20讲):经典面试题与阿里等名企内部招聘求职面试技巧 立即下载
微服务架构模式与原理Spring Cloud开发实战 立即下载
阿里特邀专家徐雷Java Spring Boot开发实战系列课程(第18讲):制作Java Docker镜像与推送到DockerHub和阿里云Docker仓库 立即下载