如何在执行代码时第一次停止春天批处理计划作业的运行? [英] how to stop spring batch scheduled jobs from running at first time when executing the code?

查看:137
本文介绍了如何在执行代码时第一次停止春天批处理计划作业的运行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



问题是作业总是在第一次执行代码之后运行时间,那么它根据预定时间运行。



我的cron表达式是0 0 0 * *?,我想要停止第一次运行并让它只在预定时间运行。 &我也尝试了0 0 0 1/1 *?*,但它仍然执行一次当应用程序启动



当应用程序启动时如何停止第一次执行?



这是工作上下文文件:

 < batch: job id =exceptionLogJob> 
< batch:step id =exceptionLogReadWriteStep>
< batch:tasklet>
< batch:chunk reader =exceptionLogReaderwriter =exceptionLogWriter
commit-interval =1000/>
< / batch:tasklet>
< / batch:step>
< / batch:job>


<! - =============================== ======================>
<! - READER - >
<! - ========================================= ============== - >
< bean id =exceptionLogReader
class =org.springframework.batch.item.database.JdbcCursorItemReader>
< property name =dataSourceref =dataSource/>
< property name =sqlvalue =SELECT a。*,a.rowid FROM SF_EXCEPTION_LOG WHERE DATETIME> SYSDATE - 1/>
< property name =rowMapperref =ExceptionLogRowMapper/>
< / bean>
<! - ========================================= ============== - >
<! - Writer - >
<! - ========================================= ============== - >
< bean id =exceptionLogWriter
class =com.mobily.sf.batchprocessor.exceptionlog.ExceptionLogWriter/>

< bean id =jobDetailExceptionLogclass =org.springframework.scheduling.quartz.JobDetailBean>
< property name =jobClass
value =com.sf.batchprocessor.commons.JobLauncherDetails/>
< property name =jobDataAsMap>
< map>
< entry key =jobNamevalue =exceptionLogJob/>
< entry key =jobLocatorvalue-ref =jobRegistry/>
< entry key =jobLaunchervalue-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 =jobDetailref =jobDetailExceptionLog/>
< property name =cronExpressionvalue =0 0 0 1/1 *?*/>
< / bean>
< / property>
< / bean>

< / beans>


解决方案

我有同样的问题,通过Spring启动的自动配置服务。默认情况下,它将在应用程序启动后运行所有配置的作业bean。



有两种属性会影响此行为:




  • spring.batch.job.enabled

  • spring.batch.job.names



第一个阻止设置为false时启动所有作业。第二个接受以逗号分隔的将运行的作业名称列表。



这两个属性可以通过 Spring启动文档


  1. 命令行( - spring.batch.job.enabled = false

  2. Java系统属性( -Dspring.batch.job.enabled = false

  3. 操作系统环境变量

  4. @ bt $ b


  5. jar中的application.properties文件

  6. b $ b
  7. SpringApplication.setDefaultProperties


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>

解决方案

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

这篇关于如何在执行代码时第一次停止春天批处理计划作业的运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆