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 based on the scheduled time. I want to stop the first run and let it only runs based on the scheduled time.
my cron expression is "0 0 0 * * ?" & I also tried "0 0 0 1/1 * ? *" but it still executes once when the application starts
how can I stop the first execution when the application starts?
this is the job context file:
<batch:job id="exceptionLogJob"> <batch:step id="exceptionLogReadWriteStep"> <batch:tasklet > <batch:chunk reader="exceptionLogReader" writer="exceptionLogWriter" commit-interval="1000" /> </batch:tasklet> </batch:step> </batch:job> <!-- ======================================================= --> <!-- READER --> <!-- ======================================================= --> <bean id="exceptionLogReader" class="org.springframework.batch.item.database.JdbcCursorItemReader"> <property name="dataSource" ref="dataSource" /> <property name="sql" value="SELECT a.*,a.rowid FROM SF_EXCEPTION_LOG a WHERE DATETIME > SYSDATE - 1" /> <property name="rowMapper" ref="ExceptionLogRowMapper" /> </bean> <!-- ======================================================= --> <!-- Writer --> <!-- ======================================================= --> <bean id="exceptionLogWriter" class="com.mobily.sf.batchprocessor.exceptionlog.ExceptionLogWriter" /> <bean id="jobDetailExceptionLog" class="org.springframework.scheduling.quartz.JobDetailBean"> <property name="jobClass" value="com.sf.batchprocessor.commons.JobLauncherDetails" /> <property name="jobDataAsMap"> <map> <entry key="jobName" value="exceptionLogJob" /> <entry key="jobLocator" value-ref="jobRegistry" /> <entry key="jobLauncher" value-ref="jobLauncher" /> </map> </property> </bean> <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <property name="triggers"> <bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean"> <property name="jobDetail" ref="jobDetailExceptionLog" /> <property name="cronExpression" value="0 0 0 1/1 * ? *" /> </bean> </property> </bean> </beans>
With above conditional flag, we're able to trigger the scheduled Spring Batch job with the scheduled task alive. If we don't need to resume the job, then we can actually stop the scheduled task to save resources.
The schedulers do not start or stop. In the real world, it is necessary to stop and restart the scheduler without restarting the spring boot application. The ScheduledAnnotationBeanPostProcessor class allows you to programmatically start and stop the scheduler without having to restart the spring boot application.
The cancel() method is used to cancel the timer task. The cancel() methods returns true when the task is scheduled for one-time execution and has not executed until now and returns false when the task was scheduled for one-time execution and has been executed already.
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
)If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With