Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to stop spring batch scheduled jobs from running at first time when executing the code?

Tags:

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> 
like image 811
Joshua Avatar asked Mar 11 '14 07:03

Joshua


People also ask

How do you stop a running job in Spring Batch?

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.

How do I stop a scheduled cron job in spring boot?

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.

How do you stop a scheduled task in Java?

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.


1 Answers

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:

  1. Command line (--spring.batch.job.enabled=false)
  2. Java system properties (-Dspring.batch.job.enabled=false)
  3. OS environment variables
  4. @PropertySource annotations
  5. application.properties file in the jar directory
  6. application.properties file inside the jar
  7. SpringApplication.setDefaultProperties
like image 53
Chris Avatar answered Oct 21 '22 19:10

Chris