本文介绍了JobBuilderFactory.get(job).incrementer(RunIdIncrementer)的功能是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Spring-Boot开发一个Spring-Batch项目,一切都顺利进行。我已经完成了一些春季批量示例(包括一些来自spring.io),但我不确定其中的一些内容是什么,它只是有效并不能满足我的需求。

I'm developing a Spring-Batch project using Spring-Boot and everything is going along nicely. I've done a few spring-batch examples (including some from spring.io), but I'm not sure what some of the stuff does, and "it just works" doesn't satiate me.

我的春季启动主类实现了 CommandLineRunner ,对于这个特定的工作,初始设置看起来像

My spring boot main class implements CommandLineRunner and for this particular job the initial set up looked like

@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更改为

Why does changing the above bean to

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

让一切顺利进行?我试图阅读 RunIdIncrementer 的文档,还读了一下。从我可以告诉它需要这个增量器来跟踪正在运行的特定工作集来做东西,但不确定究竟是什么东西。 Spring-Boot抽象让我很难知道这里发生了什么

Make everything go smoothly? I attempted to read up on the doc for RunIdIncrementer and also read up a little here. From what I can tell it needs this incrementer to keep track of a particular set of jobs that are running to do "stuff", but not sure what stuff is exactly. The Spring-Boot abstraction is making it hard for me to know what's going on here

推荐答案

这不是引物 就像它是一个批量的东西。 Spring Batch的规则是 JobInstance 只能运行一次才能完成。这意味着对于识别作业参数的每个组合,您只能有一个 JobExecution ,这将导致 COMPLETE RunIdIncrementer 会在参数列表中附加一个额外的唯一参数,以便生成的组合是唯一的......为您提供一个新的 JobInstance 每次使用相同的识别参数组合运行作业时。

This isn't a "Boot thing" as much as it is a "Batch thing". Spring Batch has the rule that a JobInstance can only be run once to completion. This means that for each combination of identifying job parameters, you can only have one JobExecution that results in COMPLETE. A RunIdIncrementer will append an additional, unique parameter to the list of parameters so that the resulting combination would be unique...giving you a new JobInstance each time you ran the job with the same combination of identifying parameters.

RunIdIncrementer 实际上只是 JobParametersIncrementer 的一个特例,您可以在我们的文档中详细了解:

The RunIdIncrementer is really just a special case of the JobParametersIncrementer which you can read more about in our documentation here: http://docs.spring.io/spring-batch/trunk/reference/htmlsingle/#JobParametersIncrementer

这篇关于JobBuilderFactory.get(job).incrementer(RunIdIncrementer)的功能是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-30 07:32