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>
Joshua
  • 345
  • 3
  • 5
  • 11

4 Answers4

54

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
Chris
  • 691
  • 4
  • 12
  • 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 – Anuj Acharya Jul 07 '15 at 17:38
  • @AnujAcharya: Try my answer. it will help. – V_J Jun 23 '17 at 07:10
3

adding

spring.batch.job.enabled=false

in application.properties works with me.

Amir Choubani
  • 829
  • 2
  • 14
  • 28
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.

V_J
  • 1,081
  • 13
  • 33
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.

Karthik Prasad
  • 9,662
  • 10
  • 64
  • 112