问题描述
我正在使用带有石英的 spring 批处理 2.2.4 在特定时间运行一些作业
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.
我的 cron 表达式是0 0 0 * * ?"&我也试过0 0 0 1/1 * ? *"但它在应用程序启动时仍然执行一次
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?
这是作业上下文文件:
<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>
推荐答案
我遇到了同样的问题,确定是 Spring boot 的自动配置服务引起的.默认情况下,它将在应用程序启动后运行所有配置的作业 bean.
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
当设置为 false 时,第一个会阻止启动所有作业.第二个接受将运行的作业名称的逗号分隔列表.
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.
这两个属性可以通过多种方式设置Spring 启动文档:
These two properties can be set a variety of ways specified in the Spring boot docs:
- 命令行(
--spring.batch.job.enabled=false
) - Java 系统属性(
-Dspring.batch.job.enabled=false
) - 操作系统环境变量
- @PropertySource 注释
- jar 目录下的application.properties 文件
- jar 内的application.properties 文件
- SpringApplication.setDefaultProperties
这篇关于执行代码时如何在第一次停止运行spring批处理计划作业?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!