JobBuilderFactory.get(job).incrementer(RunIdIncrementer)的作用是什么?

问题描述 投票:0回答:1

我正在使用 Spring-Boot 开发一个 Spring-Batch 项目,一切进展顺利。我已经做了一些 spring-batch 示例(包括一些来自 spring.io 的示例),但我不确定其中一些东西的作用,并且“它只是有效”并不能让我满意。

我的 Spring Boot 主类实现了

CommandLineRunner
,对于这个特定的工作,初始设置看起来像

@Bean
public Job myJob(JobExecutionListenerSupport listener) {
    return myJobBuilderFactory.get(JOB)
            .listener(listener)
            .start(myStep())
            .build();
}

这导致了

java.lang.IllegalStateException: Failed to execute CommandLineRunner
    at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:809) ~[spring-boot-1.3.2.RELEASE.jar:1.3.2.RELEASE]
    at org.springframework.boot.SpringApplication.callRunners(SpringApplication.java:790) ~[spring-boot-1.3.2.RELEASE.jar:1.3.2.RELEASE]
    at org.springframework.boot.SpringApplication.afterRefresh(SpringApplication.java:777) [spring-boot-1.3.2.RELEASE.jar:1.3.2.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:308) [spring-boot-1.3.2.RELEASE.jar:1.3.2.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1191) [spring-boot-1.3.2.RELEASE.jar:1.3.2.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1180) [spring-boot-1.3.2.RELEASE.jar:1.3.2.RELEASE]
    at org.bjc.providermodel.maintenance.MaintenanceApplication.main(MaintenanceApplication.java:20) [classes/:?]
Caused by: org.springframework.batch.core.repository.JobExecutionAlreadyRunningException: A job execution for this job is already running: JobInstance: id=99, version=0, Job=[myJob]

为什么将上面的bean改为

@Bean
public Job myJob(JobExecutionListenerSupport listener) {
    return myJobBuilderFactory.get(JOB)
            .incrementer(new RunIdIncrementer())
            .listener(listener)
            .start(myStep())
            .build();
}

让一切顺利?我尝试阅读

RunIdIncrementer
的文档,并在 here 阅读一些内容。据我所知,它需要这个增量器来跟踪正在运行的一组特定作业来执行“操作”,但不确定到底是什么操作。 Spring-Boot 抽象让我很难知道这里发生了什么

java spring spring-boot spring-batch
1个回答
21
投票

这不是“启动的事情”,而是“批处理的事情”。 Spring Batch 的规则是

JobInstance
只能运行一次才能完成。这意味着,对于识别作业参数的每种组合,您只能有一个
JobExecution
产生
COMPLETE
RunIdIncrementer
将在参数列表中附加一个附加的、唯一的参数,以便生成的组合是唯一的...每次使用相同的标识参数组合运行作业时,都会为您提供一个新的
JobInstance

RunIdIncrementer
实际上只是
JobParametersIncrementer
的一个特例,您可以在我们的文档这里中阅读更多信息。

© www.soinside.com 2019 - 2024. All rights reserved.