代码之家  ›  专栏  ›  技术社区  ›  John Joe Mayank Nema

使用SpringBoot编写SpringBatch

  •  0
  • John Joe Mayank Nema  · 技术社区  · 6 年前

    我正在学习如何写作 SpringBatch 使用 SpringBoot .

    示例my spring batch如下所示

      <batch:job id="job" >
                <batch:step id="deleteFile" next="generateFile">
                    <batch:tasklet ref="fileDeleting" />
                </batch:step>
                <batch:step id="generateFile" next="emailFile" >
                    <batch:tasklet transaction-manager="transactionManager">
                        <batch:chunk reader="reader" writer="writer" processor="processor" commit-interval="10" />
                    </batch:tasklet>
                </batch:step>
                <batch:step id="emailFile">
                    <batch:tasklet ref="emailTasklet" />
                </batch:step> 
                <batch:listeners>
                    <batch:listener ref="jobListener" />
                </batch:listeners>
            </batch:job>
    

    如你所见,在读者被叫之前, deleteFile 将首先调用。一旦 generateFile 完成, emailFile 将被呼叫。

    这就是 生成文件 看起来像是当它转向SpringBoot的时候。

    @Configuration
    public class BatchConfig {
    
        @Autowired
        public JobBuilderFactory jobBuilderFactory;
    
        @Autowired
        public StepBuilderFactory stepBuilderFactory;
    
        @Bean
        public Job job() {
    
            return jobBuilderFactory.get("job")
                    .incrementer(new RunIdIncrementer())
                    .flow(step1())
                    .end()
                    .build();
        }
    
        @Bean
        public Step step1() {
            System.out.println("In step1");
            return stepBuilderFactory.get("step1")
                    .<String, String>chunk(1)
                    .reader(new Reader())
                    .processor(new Processor())
                    .writer(new Writer())
                    .build();
        }
    }
    

    如何让 删除文件 电子邮件文件 在里面 弹簧靴 ?

    1 回复  |  直到 6 年前
        1
  •  1
  •   Mahmoud Ben Hassine    6 年前

    可以像面向项的步骤一样声明tasklet步骤,例如:

    @Configuration
    public class BatchConfig {
    
        @Autowired
        private JobBuilderFactory jobBuilderFactory;
    
        @Autowired
        private StepBuilderFactory stepBuilderFactory;
    
        @Bean
        public Step step1() {
            return stepBuilderFactory.get("step1")
                    .tasklet(deleteFileTasklet())
                    .build();
        }
    
        @Bean
        public Step step2() {
            return stepBuilderFactory.get("step2")
                    .<String, String>chunk(1)
                    .reader(new Reader())
                    .processor(new Processor())
                    .writer(new Writer())
                    .build();
        }
    
        @Bean
        public Step step3() {
            return stepBuilderFactory.get("step3")
                    .tasklet(emailTasklet())
                    .build();
        }
    
        @Bean
        @StepScope
        public Tasklet deleteFileTasklet() {
            return null; // TODO implement delete file tasklet
        }
    
        @Bean
        @StepScope
        public Tasklet emailTasklet() {
            return null; // TODO implement email tasklet
        }
    
        @Bean
        public Job job() {
            return jobBuilderFactory.get("job")
                    .incrementer(new RunIdIncrementer())
                    .start(step1())
                    .next(step2())
                    .next(step3())
                    .build();
        }
    
    }
    

    我希望这有帮助。