What is the function of JobBuilderFactory.get(job).incrementer(RunIdIncrementer)?

自闭症网瘾萝莉.ら 提交于 2019-12-12 08:12:21

问题


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.

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();
}

Which caused

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]

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();
}

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


回答1:


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.

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



来源:https://stackoverflow.com/questions/41271606/what-is-the-function-of-jobbuilderfactory-getjob-incrementerrunidincrementer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!