首页 > 解决方案 > Spring Batch:需要更好的作业配置

问题描述

我的 Spring 批处理应用程序处理 5 种不同类型的文件中的 1 种。所有 5 种类型的流程完全相同。我的配置相当长而且笨重。有人可以通过重用流程来帮助我压缩它吗?

这是我能想到的最好的:

        return jobBuilderFactory.get(CommonConstants.BATCH_JOB_DISTRIBUTION)
                .listener(jobListener)
                .start(retrieveFileStep())
                .next(createFileJobDetailStep(fileJobDetailTasklet))
                .next(fileTypeDecider)

                .from(fileTypeDecider).on(FileType.YearEnd.name()).to(yearEndStep)
                .from(yearEndStep).on("ERROR").to(moveFileToErrorStep(fileOperationsTasklet))
                .from(yearEndStep).on("SUCCESS").to(moveFileToProcessedStep(fileOperationsTasklet))

                .from(fileTypeDecider).on(FileType.Quarterly.name()).to(quarterlyStep)
                .from(quarterlyStep).on("ERROR").to(moveFileToErrorStep(fileOperationsTasklet))
                .from(quarterlyStep).on("SUCCESS").to(moveFileToProcessedStep(fileOperationsTasklet))

                //[repeat 3 more times...]

我想要一个更好的方式来写这个。谢谢。

标签: spring-batch

解决方案


我最终创建了一个子流程,并在整个作业配置中重用了该子流程。

public Flow flow(String flowName, Step step, Tasklet fileOperationsTasklet) {
    return new FlowBuilder<Flow>(flowName)
            .start(step)
            .from(step).on("ERROR").to(moveFileToErrorStep(fileOperationsTasklet))
            .from(step).on("SUCCESS").to(moveFileToProcessedStep(fileOperationsTasklet))
            .end();
}

所以现在我的配置包括:

.from(fileTypeDecider).on(FileType.YearEnd.name()).to(flow("yearEndFlow", yearEndStep, fileOperationsTasklet))

推荐阅读