3
votes

I'm writing a spring batch application consisting of different jobs who need to be executed in a specific order. In order to do that I'm running the jobs manually through a JobLauncher, and I disabled the auto start feature provided by Spring batch by adding the following property in my properties file:

spring.batch.job.enabled=false

I would like to disable this feature directly in the code, instead of relying on a configuration file that can be accessed and modified by anyone.

Is there a way to do that?

1
you can try with JavaConfig - Siddhesh
Does answer by Roger Thomas help ? Idea is put to put hard coded value in code all the time. - Sabir Khan
Hi @SabirKhan, thanks for your reply. It is working but it can be bypassed by explicitly setting "spring.batch.job.enabled=true" in the property files. Better than nothing though. - Giovanni Di Santo

1 Answers

3
votes
@Bean
@ConditionalOnMissingBean
@ConditionalOnProperty(prefix = "spring.batch.job", name = "enabled", havingValue = "true", matchIfMissing = true)
public JobLauncherCommandLineRunner jobLauncherCommandLineRunner(
        JobLauncher jobLauncher, JobExplorer jobExplorer) {
    JobLauncherCommandLineRunner runner = new JobLauncherCommandLineRunner(
            jobLauncher, jobExplorer);
    String jobNames = this.properties.getJob().getNames();
    if (StringUtils.hasText(jobNames)) {
        runner.setJobNames(jobNames);
    }
    return runner;
}

This is from BatchAutoConfiguration.

Judging by this, you could try to add your own implementation of JobLauncherCommandLineRunner which does nothing. This will affect the @ConditionalOnMissingBean and it shouldn't run.