在 spring 批启动期间设置 属性 值

Set property value during launch in spring batch

我正在使用 spring 批处理 3.0。我有一个场景,我需要 运行 大约 1000 个具有 1 属性 值的作业和具有不同 属性 值的相同作业。有没有办法在启动或调度时间而不是在作业配置中设置作业的 属性。或任何其他无需复制 1000 个作业即可实现此类功能的方法。

<batch:job id="job_A" parent="simpleJob">
        <batch:step id="A" parent="simpleStep">
            <batch:tasklet>
                <bean id="bA" class="ClassA" scope="step">
                        <property name="downloadFileA" value="false" />
                </bean>     
            </batch:tasklet>
         </batch:step>  
</batch:job>

同样的作业,属性 值为 true。

<batch:job id="job_A" parent="simpleJob">
        <batch:step id="A" parent="simpleStep">
            <batch:tasklet>
                <bean id="bA" class="ClassA" scope="step">
                        <property name="downloadFileA" value="true" />
                </bean>     
            </batch:tasklet>
         </batch:step>  
</batch:job>

非常感谢任何帮助。

我认为你应该看看决策者:

来自 spring 批处理文档:

In some situations, more information than the ExitStatus may be required to decide which step to execute next. In this case, a JobExecutionDecider can be used to assist in the decision.

public class MyDecider implements JobExecutionDecider {
    public FlowExecutionStatus decide(JobExecution jobExecution, StepExecution stepExecution) {
        if (someCondition) {
            return "FAILED";
        }
        else {
            return "COMPLETED";
        }
    }
}

In the job configuration, a "decision" tag will specify the decider to use as well as all of the transitions.

<job id="job">
    <step id="step1" parent="s1" next="decision" />

    <decision id="decision" decider="decider">
        <next on="FAILED" to="step2" />
        <next on="COMPLETED" to="step3" />
    </decision>

    <step id="step2" parent="s2" next="step3"/>
    <step id="step3" parent="s3" />
</job>

<beans:bean id="decider" class="com.MyDecider"/>

所以换句话说,你应该创建一个 class 来实现 JobExecutionDecider 接口,然后 return 一个基于你的 if-else 测试的结果,并在你的 class spring批量xml配置。瞧!

您可以使用传递给作业启动器的作业参数,然后可以从作业中的步骤访问这些参数。

启动工作时,您可以这样做...

JobParameters jobParameters = new JobParametersBuilder()
    .addString("downloadFileA", "true")
    .toJobParameters();

JobExecution jobExecution = jobLauncher.run(job, jobParameters);

然后您可以在您的步骤中注入 jobParameter...

@Autowired
@Value("#{jobParameters[downloadFileA]}")
String downloadFileA;