如何在Spring批处理中使用decider?

问题描述 投票:0回答:1

我是Spring批次的新手。我创建了一个decider,它将FlowExecutionStatus返回为“YES”/“NO”。基于FlowExecutionStatus,我需要调用step2()或step3()。

在我的下面的代码中,step2()在决策之前被调用。如何修改代码以便调用decider,并根据决策器返回的“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();
}
spring-batch
1个回答
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();
}

编辑:我认为决策者应该根据step1的退出状态做出决定,因此是之前的样本。因此,添加一个示例以显示如何使用决策程序(在澄清之后):

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

}

在此示例中,首先执行step1,然后执行decider。如果决策程序返回YES,则执行step2,如果返回NO,则执行step3。

希望这可以帮助。

© www.soinside.com 2019 - 2024. All rights reserved.