i\'m using spring batch 2.2.4 with quartz to run some jobs at certain time
the problem is the jobs always run after executing the code at the first time then it runs
I guess some configuration problem. Here is the configurations I tested with same cron expression. I've launch-context.xml with following configuration.
<bean class="org.springframework.batch.core.configuration.support.AutomaticJobRegistrar">
<property name="applicationContextFactories">
<bean
class="org.springframework.batch.core.configuration.support.ClasspathXmlApplicationContextsFactoryBean">
<property name="resources">
<list>
<value>classpath*:configurations/kp-batch.xml</value>
</list>
</property>
</bean>
</property>
<property name="jobLoader" >
<bean
class="org.springframework.batch.core.configuration.support.DefaultJobLoader">
<property name="jobRegistry" ref="jobRegistry" />
</bean>
</property>
</bean>
<bean id="jobRegistry"
class="org.springframework.batch.core.configuration.support.MapJobRegistry" />
<bean id="schedule"
class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="cronTrigger1"/>
</list>
</property>
</bean>
<bean id="cronTrigger1"
class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
<property name="jobDetail" ref="kpJobDetail" />
<property name="cronExpression" value="0 0 0 1/1 * ? *"/>
</bean>
<bean id="kpJobDetail" class="org.springframework.scheduling.quartz.JobDetailBean">
<property name="jobClass"
value="com.viasat.nbn.nms.webservices.util.SpringBatchQuartzJobLauncher" />
<property name="jobDataAsMap">
<map>
<entry key="jobName" value="Trigger Job for 12AM" />
<entry key="jobLocator" value-ref="jobRegistry" />
<entry key="jobLauncher" value-ref="jobLauncher" />
</map>
</property>
</bean>
<bean id="batchTransactionManager"
class="org.springframework.batch.support.transaction.ResourcelessTransactionManager">
<property name="rollbackOnCommitFailure" value="false" />
</bean>
<bean id="jobLauncher"
class="org.springframework.batch.core.launch.support.SimpleJobLauncher">
<property name="jobRepository" ref="jobRepository" />
</bean>
<bean id="jobRepository"
class="org.springframework.batch.core.repository.support.MapJobRepositoryFactoryBean">
<property name="transactionManager" ref="batchTransactionManager" />
</bean>
In the kp-batch.xml, I've defined job, itemreader, itemwriter etc.
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:
--spring.batch.job.enabled=false
)-Dspring.batch.job.enabled=false
)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-start
spring.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.