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

Joshua picture Joshua · Mar 11, 2014 · Viewed 20.9k times · Source

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>

Answer

Chris picture Chris · Apr 14, 2014

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