Spring Batch Step流程函数中是否存在错误?

时间:2015-05-15 17:07:22

标签: spring spring-batch

在下面的代码中,当StepA失败时,只执行StepB和StepC,但实际发生的是所有3个步骤都在执行!我想根据步骤是否通过来拆分弹簧批处理作业。我知道还有其他方法可以通过使用JobDecider,设置一些工作参数等来实现这一点,但我想知道我在这里做错了吗?

@Configuration
@EnableBatchProcessing
public class JobConfig {

    @Autowired
    private JobBuilderFactory jobBuilderFactory;

    @Autowired
    private StepBuilderFactory stepBuilderFactory;

    @Bean
    public PlatformTransactionManager transactionManager() {
        return new ResourcelessTransactionManager();
    }

    @Bean
    public JobRepository jobRepository() {
        try {
            return new MapJobRepositoryFactoryBean(transactionManager())
                    .getJobRepository();
        } catch (Exception e) {
            return null;
        }
    }

    @Bean
    public JobLauncher jobLauncher() {
        final SimpleJobLauncher launcher = new SimpleJobLauncher();
        launcher.setJobRepository(jobRepository());
        return launcher;
    }

    @Bean
    public Job job() {
        return jobBuilderFactory.get("job").
                flow(stepA()).on("FAILED").to(stepC()).next(stepD()).
                from(stepA()).on("*").to(stepB()).next(stepC()).end().build();
    }

    @Bean
    public Step stepA() {
        return stepBuilderFactory.get("stepA")
                .tasklet(new RandomFailTasket("stepA")).build();
    }

    @Bean
    public Step stepB() {
        return stepBuilderFactory.get("stepB")
                .tasklet(new PrintTextTasklet("stepB")).build();
    }

    @Bean
    public Step stepC() {
        return stepBuilderFactory.get("stepC")
                .tasklet(new PrintTextTasklet("stepC")).build();
    }

    @Bean
    public Step stepD() {
        return stepBuilderFactory.get("stepD")
                .tasklet(new PrintTextTasklet("stepD")).build();
    }

    @SuppressWarnings("resource")
    public static void main(String[] args) {
        // create spring application context
        final ApplicationContext appContext = new AnnotationConfigApplicationContext(
                JobConfig.class);
        // get the job config bean (i.e this bean)
        final JobConfig jobConfig = appContext.getBean(JobConfig.class);
        // get the job launcher
        JobLauncher launcher = jobConfig.jobLauncher();
        try {
            // launch the job
            JobExecution execution = launcher.run(jobConfig.job(), new JobParameters());
            System.out.println(execution.getJobInstance().toString());
        } catch (JobExecutionAlreadyRunningException e) {
            e.printStackTrace();
        } catch (JobRestartException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (JobInstanceAlreadyCompleteException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (JobParametersInvalidException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

}

StepA:是一个失败的虚拟作业,即抛出一些异常 公共类RandomFailTask​​et扩展PrintTextTasklet {

    public RandomFailTasket(String text) {
        super(text);
    }

    public RepeatStatus execute(StepContribution arg0, ChunkContext arg1)
            throws Exception {

        if (Math.random() < 0.5){
            throw new Exception("fail");
        }
        return RepeatStatus.FINISHED;
    }

}

StepB,StepC,StepD也是虚拟tasklet: public class PrintTextTasklet实现Tasklet {

    private final String text;

    public PrintTextTasklet(String text){
        this.text = text;
    }


    public RepeatStatus execute(StepContribution arg0, ChunkContext arg1)
            throws Exception {
        System.out.println(text);
        return RepeatStatus.FINISHED;
    }

}

1 个答案:

答案 0 :(得分:0)

需要查看您正在使用的xml结构。

尝试使用Step侦听器 - 然后在后续步骤中检查Step状态,然后您可以实现逻辑以调用下一步

相关问题