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

爱⌒轻易说出口 提交于 2019-11-30 12:36:56

问题


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>

回答1:


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:


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.




回答3:


adding

spring.batch.job.enabled=false

in application.properties works with me.




回答4:


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.



来源:https://stackoverflow.com/questions/22318907/how-to-stop-spring-batch-scheduled-jobs-from-running-at-first-time-when-executin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!