Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

spring boot/batch disable batch auto start via code

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?

like image 280
Giovanni Di Santo Avatar asked Oct 15 '25 18:10

Giovanni Di Santo


1 Answers

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

like image 57
alturkovic Avatar answered Oct 17 '25 22:10

alturkovic



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!