24

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>
8
  • Do you mean to say first run, runs as soon as the you application starts without considering the cron expression? Mar 11, 2014 at 10:20
  • I had similar issue but, soon realized that it was due to cron expression. can you share your cron expression? Mar 11, 2014 at 19:28
  • yes sure, it's "0 0 0 * * ?"
    – Joshua
    Mar 12, 2014 at 7:22
  • Can you use this cron expression. "0 0 0 1/1 * ? *" Hope there is no spaces at the begining. Here is the output of the cron expression 2014.03.13 AD at 00:00:00 IST 2014.03.14 AD at 00:00:00 IST 2014.03.15 AD at 00:00:00 IST 2014.03.16 AD at 00:00:00 IST Mar 12, 2014 at 7:39
  • I just saw the output, okay thank you I will try it.
    – Joshua
    Mar 12, 2014 at 7:40

4 Answers 4

55

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
2
  • 1
    ~Chris I tried adding spring.batch.job.enabled=false in application.properties and also tried running using Java System properties, still it run the batch jobs Jul 7, 2015 at 17:38
  • @AnujAcharya: Try my answer. it will help.
    – V_J
    Jun 23, 2017 at 7:10
3

adding

spring.batch.job.enabled=false

in application.properties works with me.

1

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.

0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.