Search code examples
springspring-batchjobsspring-java-config

Spring Batch - How to set RunIdIncrementer globally using JavaConfig


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.


Solution

  • 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.