0
votes

im developing a Project using Spring Batch and JavaConfig (no XML). I am creating Jobs using an Autowired jobBuilderFactory.

is it somehow possible to set the Incrementer for the Factory globally ?

return jobBuilderFactory.get("jobName").incrementer(new RunIdIncrementer()).start(stepOne()).next(lastStep()).build();

sorry if this is a dump question but i am new to Spring Batch and did not find a working solution.

1
What do you mean by "setting the Incrementer for the Factory globally?" The incrementer is something related to a particular job, it increments the job parameters when you run the next instance of the job. So each job has its own incrementer. You said you did not find a working solution, what do you want to achieve?Mahmoud Ben Hassine
when i look at the spring batch samples the job definition looks like this: return jobs.get("myJob").start(step1).next(step2).build(); And it seems as if there is no incrementer set (sorry if i am wrong). We will only use the basic "RunIdIncrementer" in the whole batch Project and thats why i wanted to set it globally for all jobs. We trigger the jobs via Cron + Rest (with dummy parameters)eckad158

1 Answers

1
votes

With XML config you would use bean definition inheritance, but you said you don't use XML.

Since there is no equivalent of XML bean definition inheritance with Java config (See details here: https://stackoverflow.com/a/23266686/5019386), you can create the RunIdIncrementer globally in your config and use it in job definitions:

public JobParametersIncrementer jobParametersIncrementer() {
    return new RunIdIncrementer();
}

public JobBuilder getJobBuilder(String jobName) {
    return jobBuilderFactory.get(jobName)
            .incrementer(jobParametersIncrementer());
}

@Bean
public Job job1() {
    return getJobBuilder("job1")
            .start(step())
            .build();
}

@Bean
public Job job2() {
    return getJobBuilder("job2")
            .start(step())
            .build();
}

But again and as said in comments, you will end up having run.id values that are not consecutive for each job.