java - 使用作业参数进行 Spring 批量重启

标签 java spring spring-batch

是否可以在Spring批处理中针对特定参数重新启 Action 业?

我在元表中有两个作业失败的实例。每个作业都有自己的作业参数。现在,我想用给定的参数重新启 Action 业。即使我传递了作业参数,它总是只重新启动最后一次失败的执行。

我的工作:

<batch:job id="JobA"  incrementer="runIdIncrementer">
    <batch:step id="abcde">
        <batch:tasklet transaction-manager="transactionManager">
            <batch:chunk reader="Reader" writer="Writer" processor="Processor" commit-interval="100" />
        </batch:tasklet>
    </batch:step>
</batch:job>

我将在两个不同的实例中使用不同的参数启动 JobA。假设 JobA 为 1234 帐户,JobA 为 5678 帐户。两个实例都失败了。现在我想重新启动 1234 帐户的 JobA。但是 Spring Batch 总是重新启动最后一次失败的执行,无论参数如何。

已编辑 调试CommandLineJobRunner我发现这段代码总是重新启动最新失败的作业,无论作业参数如何:

private JobExecution getLastFailedJobExecution(String jobIdentifier) {
    List<JobExecution> jobExecutions = getJobExecutionsWithStatusGreaterThan(jobIdentifier, BatchStatus.STOPPING);
    if (jobExecutions.isEmpty()) {
        return null;
    }
    return jobExecutions.get(0);
}

最佳答案

我们遇到了同样的问题,对我来说这看起来像是一个缺陷,特别是当您比较 SimpleJobLauncher 如何评估上一个作业时。

正如您所提到的,getLasFailedJobExecution() 不考虑参数。 (至少在我看来,这是错误的。您可以使用不同的标识参数运行相同的作业,但只有其中一次运行失败时,重新启动才会起作用。)

另一方面,SimpleJobLauncher根据参数获取最后一次作业执行:

public JobExecution run(final Job job, final JobParameters jobParameters)
        throws JobExecutionAlreadyRunningException, JobRestartException, JobInstanceAlreadyCompleteException,
        JobParametersInvalidException {
...

    JobExecution lastExecution = jobRepository.getLastJobExecution(job.getName(), jobParameters);
...

这并不一致。

我们解决这个问题的方法是实现我们自己的从 CommandLineJobRunner 派生的 CommandLineJobRunner,但是您必须覆盖整个 start 方法,因此,您还必须复制几个私有(private)方法。

启动方法中的整个重新启动部分必须进行如下更改:

        if (opts.contains("-restart")) {
            JobExecution lastExecution = jobRepository.getLastJobExecution(job.getName(), jobParameters);

            if (!jobExecution.getStatus().isGreaterThan(BatchStatus.STOPPING)) {
                throw new JobExecutionNotFailedException("No failed or stopped execution found for job="
                        + jobIdentifier);
            }
            jobParameters = jobExecution.getJobParameters();
            jobName = jobExecution.getJobInstance().getJobName();
        }

已编辑

有时我真的应该阅读 javadoc...

如果重新启动,您可以传递executionid 而不是作业名称。在这种情况下,它将找到正确的执行来重新启动。

来自main方法的javadoch:

restart: (optional) if the job has failed or stopped and the most should be restarted. If specified then the jobIdentifier parameter can be interpreted either as the name of the job or the id of the job execution that failed.

因此,在重新启动时使用执行 ID 将确保重新启动正确的实例。在这种情况下,您也不必提供任何其他参数,因为它们将从上次执行中获取。

关于java - 使用作业参数进行 Spring 批量重启,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34237442/

相关文章:

spring - Spring Boot Rest 服务是否需要 "produces"和 "consumes"属性?

java - 如何将 Spring Security 与 Spring Batch 集成?

java - 不使用重量级框架如何解决Jar hell?

java - JSON日期到Java日期?

java - Spring Scheduling - 每个月晚上 11 点结束的 Cron 表达式?

java - 我应该为我的新 Spring Batch 作业使用 Spring Data flow 服务器吗?

java - 创建名为 'batchPropertyPostProcessor' 的 bean 时,批处理作业初始化因错误而失败

java - 如何在 Spring Boot REST API 中启用 JSON/Jackson @RequestBody 的严格验证?

java - 尝试显示整数因子,但遇到困难

java - 我无法访问外部 javascript 文件中的 freemarker 变量