【问题标题】:Restart a new instance of a job that is not restartable重新启动不可重新启动的作业的新实例
【发布时间】:2012-07-19 10:53:44
【问题描述】:

我已经定义了一个带有参数 restart="false" 的 Spring-Batch 作业,因为我不希望再次启动相同的作业实例。

<batch:job id="myJob" restartable="false">
    <batch:step id="myStep">
        <batch:tasklet>
            ...
        </batch:tasklet>
    </batch:step>
</batch:job>

该作业在另外两个更高级别的作业中被引用:

<batch:job id="hightLevelJob1" restartable="false">
    <batch:step id="myHighLevelJob1Step1" next="...">
        <batch:job ref="myJob"/>
    </batch:step>
    ...
</batch:job>

<batch:job id="hightLevelJob2" restartable="false">
    <batch:step id="myHighLevelJob2Step1" next="...">
        <batch:job ref="myJob"/>
    </batch:step>
    ...
</batch:job>

我现在有两个 JUnit-Test,每个都启动一个高级作业:

@Autowired
private JobOperator jobOperator;

@Test
public void testHighLevelJob1() throws JobExecutionException {
    final Long executionId = jobOperator.startNextInstance("myHighLevelJob1");
    ...
}

@Test
public void testHighLevelJob2() throws JobExecutionException {
    final Long executionId = jobOperator.startNextInstance("myHighLevelJob2");
    ...
}

第一个测试运行良好,而第二个则抛出 JobRestartException:

JobInstance already exists and is not restartable

我正在使用 jobOperator.startNextInstance() 所以它创建了一个 new 作业。但是,这似乎只适用于启动的高级作业。看起来 Spring-Batch 试图重用引用的作业。

如何将作业配置为每次都创建引用作业的新实例?

【问题讨论】:

    标签: java spring spring-batch


    【解决方案1】:

    您需要JobParametersIncrementer 才能使其工作,因为 startNextInstance 的 javadocs 明确指出:“在 JobInstance 的序列中启动下一个 JobInstance,该 JobInstance 由附加到指定作业的 JobParametersIncrementer 确定”

    JobParametersIncrementer 可帮助您增加新参数并将其放入 JobParameters 中,从而可以启动新的 Job 实例。

    修改您的作业定义 bean 并添加增量器

    <batch:job id="myJob" restartable="false" incrementer="incrementer">
        <batch:step id="myStep">
            <batch:tasklet>
                ...
            </batch:tasklet>
        </batch:step>
    </batch:job>
    

    并用一个简单的增量器实现 JobParametersIncrementer 类

    <bean id="incrementer"
            class="com...TrivialJobParametersIncrementer" />
    

    您可以在春季批处理管理示例中找到示例 TrivialJobParametersIncrementer

    【讨论】:

    • 我试过了,但异常还是一样。我已经在使用类 org.springframework.batch.core.launch.support.RunIdIncrementor。使用调试器检查时,增量器仅对更高级别的作业执行,但从不为引用的作业执行。
    猜你喜欢
    • 2014-11-09
    • 2013-01-20
    • 1970-01-01
    • 2013-10-12
    • 1970-01-01
    • 1970-01-01
    • 2018-06-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多