springbatch自學之路-07(決策期decider的使用)

1.配置類

package com.springbatch._05new_decider;

import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.StepContribution;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.job.flow.JobExecutionDecider;
import org.springframework.batch.core.scope.context.ChunkContext;
import org.springframework.batch.core.step.tasklet.Tasklet;
import org.springframework.batch.repeat.RepeatStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 任務配置器
 *
 * @Package: com.springbatch._01helloworld
 * @ClassName: jobConfig
 * @author: zq
 * @since: 2020/5/22 21:00
 * @version: 1.0
 * @Copyright: 2020 zq. All rights reserved.
 */
@Configuration
public class JobConfig {

    //創建 創建job的對象
    @Autowired
    private JobBuilderFactory jobBuilderFactory;

    //創建 創建step的對象
    @Autowired
    private StepBuilderFactory stepBuilderFactory;

   
    @Bean
    public Job newJob() {

        return jobBuilderFactory.get("newJob")
                .start(step1())
                .next(myDecider())
                .from(myDecider()).on("odd").to(step2())
                .from(myDecider()).on("even").to(step3())
//                .from(step3()).on("*").to(myDecider())
                .end()
                .build();

    }

    @Bean
    public Step step3() {
        return stepBuilderFactory.get("step--3")
                .tasklet(new Tasklet() {
                    @Override
                    public RepeatStatus execute(StepContribution stepContribution, ChunkContext chunkContext) throws Exception {
                        System.out.println(Thread.currentThread() + " step--3");

                        return RepeatStatus.FINISHED;
                    }
                }).build();
    }


    @Bean
    public Step step2() {
        return stepBuilderFactory.get("step--2")
                .tasklet(new Tasklet() {
                    @Override
                    public RepeatStatus execute(StepContribution stepContribution, ChunkContext chunkContext) throws Exception {
                        System.out.println(Thread.currentThread() + " step--2");
                        return RepeatStatus.FINISHED;
                    }
                }).build();
    }

    @Bean
    public Step step1() {

        return stepBuilderFactory.get("step--1")
                .tasklet(new Tasklet() {
                    @Override
                    public RepeatStatus execute(StepContribution stepContribution, ChunkContext chunkContext) throws Exception {
                        System.out.println(" step--1");
                        return RepeatStatus.FINISHED;
                    }
                }).build();

    }

    @Bean
    public JobExecutionDecider myDecider() {

        return new MyDecider();

    }

}

2.決策期

package com.springbatch._05new_decider;

import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.StepExecution;
import org.springframework.batch.core.job.flow.FlowExecutionStatus;
import org.springframework.batch.core.job.flow.JobExecutionDecider;

/**
 * @Package: com.springbatch._05new_decider
 * @ClassName: MyDecider
 * @author: zq
 * @since: 2020/6/1 21:25
 * @version: 1.0
 * @Copyright: 2020 zq. All rights reserved.
 */
public class MyDecider implements JobExecutionDecider {

    int i = 0;

    @Override
    public FlowExecutionStatus decide(JobExecution jobExecution, StepExecution stepExecution) {

        i++;
        if (i % 2 == 0) {

            return new FlowExecutionStatus("odd");

        } else {

            return new FlowExecutionStatus("even");

        }

    }
}

3.啓動類

package com.springbatch._05new_decider;

import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * SpringBatchDemo項目啓動類
 *
 * @Package: PACKAGE_NAME
 * @ClassName: com.springbatch._01helloworld.SpringBatchConfig
 * @author: zq
 * @since: 2020/5/22 20:31
 * @version: 1.0
 * @Copyright: 2020 zq. All rights reserved.
 */
@SpringBootApplication
@EnableBatchProcessing
public class SpringBatchConfig {

    public static void main(String[] args) {

        SpringApplication.run(SpringBatchConfig.class, args);

    }

}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章