how to stop spring batch scheduled jobs from running at first time when executing the code?
I had the same problem and determined that it was caused by Spring boot's autoconfiguration service. By default, it will run all configured job beans after application start.
There are two properties that affect this behavior:
spring.batch.job.enabled
spring.batch.job.names
The first prevents the launching of all jobs when set to false. The second accepts a comma-delimited list of job names that will be run.
These two properties can be set a variety of ways specified in the Spring boot docs:
- Command line (
--spring.batch.job.enabled=false
) - Java system properties (
-Dspring.batch.job.enabled=false
) - OS environment variables
- @PropertySource annotations
- application.properties file in the jar directory
- application.properties file inside the jar
- SpringApplication.setDefaultProperties
adding
spring.batch.job.enabled=false
in application.properties works with me.
To solve this you will have to create one more properties file and name it "batch.properties".
# Disable batch auto-startspring.batch.job.enabled=false
You can give the reference to this file from your java configuration file.
Sample:
@Configuration@ComponentScan("com.code")@EnableBatchProcessing@PropertySource("classpath:batch.properties")public class AppConfig {}
@PropertySource("classpath:batch.properties")
Hope this helps.