Spring Batch中如何使用Decisioner?

时间:2018-09-04 14:50:58

标签: spring-batch

我是Spring批处理的新手。我创建了一个决策器,该决策器将FlowExecutionStatus返回为“是” /“否”。基于FlowExecutionStatus,我需要调用step2()或step3()。

在我下面的代码中,step2()在决策程序之前被调用。如何修改代码以便调用决策程序,并根据决策程序返回的“ FlowExecutionStatus”,应调用step2()或step3()。请帮忙。

@Autowired
private NumberDecider decider;

@Bean
public Job NumberLoaderJob() throws NumberFormatException, IOException {
    return jobBuilderFactory.get("numberLoaderJob").start(step1()).listener(new MyNumberJobListener())
            .next(decider).on("YES").to(step2())
            .from(decider).on("NO").to(step3()).end().build();
}

    @Bean
public Step step1() {

    return stepBuilderFactory.get("step1").tasklet(new Tasklet() {

        @Override
        public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {
            System.out.println("Exiting step1() execute()");
            return RepeatStatus.FINISHED;
        }
    }).build();
}

/**
 * Step 2
 * 
 * @return
 * @throws NumberFormatException
 * @throws IOException
 */
@Bean
public Step step2() throws NumberFormatException, IOException {
    return stepBuilderFactory.get("step2").listener(new MyStepListener())
            .<OrderNumber, OrderNumber>chunk(Integer.valueOf(chunkSize)).faultTolerant()
            .listener(new MyChunkListener()).reader(new MyItemReader())
            .listener(new MyItemReaderListener()).writer(customItemWriter())
            .listener(new MyWriteListener()).build();
}

1 个答案:

答案 0 :(得分:2)

您需要在step1中设置退出状态,以便决策者选择并做出决定:

@Bean
public Step step1() {

   return stepBuilderFactory.get("step1").tasklet(new Tasklet() {

       @Override
       public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {
          System.out.println("Exiting step1() execute()");
          chunkContext.getStepContext().getStepExecution().setExitStatus(new ExitStatus("YES")); // or NO
          return RepeatStatus.FINISHED;
       }

    }).build();
}

编辑:我认为决策者应该根据步骤1的退出状态(即上一个示例)做出决定。因此,添加一个示例以展示如何使用决策程序(在澄清之后):

import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.job.flow.FlowExecutionStatus;
import org.springframework.batch.core.job.flow.JobExecutionDecider;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.batch.repeat.RepeatStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@EnableBatchProcessing
public class MyJob {

    @Autowired
    private JobBuilderFactory jobs;

    @Autowired
    private StepBuilderFactory steps;

    @Bean
    public Step step1() {
        return steps.get("step1")
                .tasklet((contribution, chunkContext) -> {
                    System.out.println("hello");
                    return RepeatStatus.FINISHED;
                })
                .build();
    }

    @Bean
    public JobExecutionDecider decider() {
        return (jobExecution, stepExecution) -> new FlowExecutionStatus("YES"); // or NO
    }

    @Bean
    public Step step2() {
        return steps.get("step2")
                .tasklet((contribution, chunkContext) -> {
                    System.out.println("world");
                    return RepeatStatus.FINISHED;
                })
                .build();
    }

    @Bean
    public Step step3() {
        return steps.get("step3")
                .tasklet((contribution, chunkContext) -> {
                    System.out.println("!!");
                    return RepeatStatus.FINISHED;
                })
                .build();
    }

    @Bean
    public Job job() {
        return jobs.get("job")
                .start(step1())
                .next(decider())
                    .on("YES").to(step2())
                    .from(decider()).on("NO").to(step3())
                    .end()
                .build();
    }

    public static void main(String[] args) throws Exception {
        ApplicationContext context = new AnnotationConfigApplicationContext(MyJob.class);
        JobLauncher jobLauncher = context.getBean(JobLauncher.class);
        Job job = context.getBean(Job.class);
        jobLauncher.run(job, new JobParameters());
    }

}

在此示例中,首先执行步骤1,然后执行决策程序。如果决策者返回YES,则执行步骤2;如果返回NO,则执行步骤3。

希望这会有所帮助。