3

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?

  • you can try with JavaConfig – Siddhesh Aug 31 '17 at 09:45
  • Does [answer](https://stackoverflow.com/questions/29072628/how-to-override-spring-boot-application-properties-programmatically) by Roger Thomas help ? Idea is put to put hard coded value in code all the time. – Sabir Khan Aug 31 '17 at 11:46
  • 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 Sep 04 '17 at 09:12

1 Answers1

4
@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.

alturkovic
  • 990
  • 8
  • 31