美文网首页
spring batch 传参数

spring batch 传参数

作者: 刘小刀tina | 来源:发表于2020-04-27 19:24 被阅读0次
    @Configuration
    @EnableBatchProcessing
    public class ParameterDemo implements StepExecutionListener {
    
        @Autowired
        private JobBuilderFactory jobBuilderFactory;
    
        @Autowired
        private StepBuilderFactory stepBuilderFactory;
    
        //创建一个成员变量
        private Map<String,JobParameter> parameters;
    
        /**
         * 创建job
         * @return
         */
        @Bean(value = "parameterDemoJob1")
        public Job  parameterDemoJob1(){
            return jobBuilderFactory.get("parameterDemoJob1")
                    .start(parameterDemoStep1())
                    .build();
        }
    
        /**
         * 创建step
         * @return
         */
        @Bean(value = "parameterDemoStep1")
        public Step parameterDemoStep1() {
            return stepBuilderFactory.get("parameterDemoStep1")
                    .listener(this)
                    .tasklet(new Tasklet() {
                        @Override
                        public RepeatStatus execute(StepContribution stepContribution, ChunkContext chunkContext) throws Exception {
                            System.out.println("info的值为:"+parameters.get("info"));
                            return RepeatStatus.FINISHED;
                        }
                    }).build();
        }
    
        /**
         * 对step做监听,在监听之前给Job传递参数
         * @param stepExecution
         */
    
        @Override
        public void beforeStep(StepExecution stepExecution) {
            this.parameters = stepExecution.getJobParameters().getParameters();
        }
    
        @Override
        public ExitStatus afterStep(StepExecution stepExecution) {
            return null;
        }
    }
    
    
    

    相关文章

      网友评论

          本文标题:spring batch 传参数

          本文链接:https://www.haomeiwen.com/subject/qeiuwhtx.html