Prevent Spring Batch automatic job trigger after context creation without Spring Boot
10,186
Solution 1
spring.batch.job.enabled=false
this configuration is worked for me
Solution 2
As per the Spring docs, adding below line to application.properties works fine for me:
spring.batch.job.enabled=false
Author by
saurb
Updated on June 27, 2022Comments
-
saurb almost 2 years
I am setting up a project with Spring Batch without using Spring Boot. When the Spring application context is created, all the jobs get executed.
I tried adding
spring.batch.job.enbled=false
to the application.properties to prevent this but it still does not work.Is there any other way to stop Spring from executing the jobs at start?
Main Class:
package com.project.batch; import ... @Configuration @EnableBatchProcessing @PropertySource("classpath:application.properties") public class App { public static void main(String [] args) throws JobParametersInvalidException, JobExecutionAlreadyRunningException, JobRestartException, JobInstanceAlreadyCompleteException { System.out.println("starting main"); AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); context.scan("com.project.batch"); context.refresh(); //JobParameters jobParameters = new JobParametersBuilder().toJobParameters(); //JobLauncher jobLauncher = context.getBean(JobLauncher.class); //JobExecution execution = jobLauncher.run(context.getBean("loaderJob",Job.class),jobParameters); System.out.println("finished!!"); } }
Job Class:
package com.project.batch; import ... @Configuration public class LoaderJobConfig { @Autowired private JobBuilderFactory jobBuilderFactory; @Autowired JdbcTemplate jdbcTemplate; @Autowired private StepBuilderFactory stepBuilderFactory; @Bean public Job loaderJob(Step step1) throws Exception { System.out.println("Starting loaderJob"); ... } }
application.properties:
spring.batch.job.enbled=false spring.batch.job.names=
Run logs:
starting main Nov 06, 2017 9:29:02 AM org.springframework.context.annotation.AnnotationConfigApplicationContext prepareRefresh INFO: Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@306a30c7: startup date [Mon Nov 06 09:29:02 EST 2017]; root of context hierarchy Nov 06, 2017 9:29:03 AM org.springframework.context.annotation.ConfigurationClassEnhancer intercept WARNING: @Bean method ScopeConfiguration.stepScope is non-static and returns an object assignable to Spring's BeanFactoryPostProcessor interface. This will result in a failure to process annotations such as @Autowired, @Resource and @PostConstruct within the method's declaring @Configuration class. Add the 'static' modifier to this method to avoid these container lifecycle issues; see @Bean Javadoc for complete details Nov 06, 2017 9:29:03 AM org.springframework.context.annotation.ConfigurationClassEnhancer intercept WARNING: @Bean method ScopeConfiguration.jobScope is non-static and returns an object assignable to Spring's BeanFactoryPostProcessor interface. This will result in a failure to process annotations such as @Autowired, @Resource and @PostConstruct within the method's declaring @Configuration class. Add the 'static' modifier to this method to avoid these container lifecycle issues; see @Bean Javadoc for complete details Nov 06, 2017 9:29:03 AM org.springframework.jdbc.datasource.DriverManagerDataSource setDriverClassName INFO: Loaded JDBC driver: org.postgresql.Driver Starting loaderJob found the value: [MER] Completed loaderJob finished!! Process finished with exit code 0
EDIT: Removed the job execution code from main class, the jobs still get triggered at context refresh
EDIT 2: Including run logs
EDIT 3: Fixed typo and updated logs