[英]java Quartz Scheduler cron expression for every after 30 minutes quarter past the hours
[英]Spring cron expression for every after 30 minutes
我每 30 分鍾就運行一次 Spring 作業。 請檢查我的 cron 表達式,對嗎?
0 0 0 * * 30
這是來自相關 Spring配置文件的完整 cron 作業定義:
<bean id="autoWeblogPingTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail" ref="jobDetailForWeblogPing"/>
<!-- run every 35 minutes -->
<property name="cronExpression" value="0 0 0 * * 30" />
</bean>
根據Quartz-Scheduler Tutorial它應該是value="0 0/30 * * * ?"
cronExpression 的字段順序是
1.秒
2分鍾
3小時
4.月份
5.月
6.星期幾
7.Year(可選字段)
確保你至少有 6 個參數,否則你會得到一個錯誤(年份是可選的)
從圖形上看,Quarz 的 cron 語法是 ( source ):
+-------------------- second (0 - 59)
| +----------------- minute (0 - 59)
| | +-------------- hour (0 - 23)
| | | +----------- day of month (1 - 31)
| | | | +-------- month (1 - 12)
| | | | | +----- day of week (0 - 6) (Sunday=0 or 7)
| | | | | | +-- year [optional]
| | | | | | |
* * * * * * * command to be executed
因此,如果您想每 30 分鍾運行一次命令,您可以使用以下任一命令:
0 0/30 * * * * ?
0 0,30 * * * * ?
您可以使用以下任一方法檢查 crontab 表達式:
<property name="cronExpression" value="0 0/30 * * * ?" />
在我的 Java Spring Web 應用程序中,這對我有用:
cron="0 0/30 * * * ?"
這將在例如上午 10:00、然后是上午 10:30 等觸發。
這是一個完整的配置文件:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task.xsd">
<beans profile="cron">
<bean id="executorService" class="java.util.concurrent.Executors" factory-method="newFixedThreadPool">
<beans:constructor-arg value="5" />
</bean>
<task:executor id="threadPoolTaskExecutor" pool-size="5" />
<task:annotation-driven executor="executorService" />
<beans:bean id="expireCronJob" class="com.cron.ExpireCron"/>
<task:scheduler id="serverScheduler" pool-size="5"/>
<task:scheduled-tasks scheduler="serverScheduler">
<!-- every thirty minutes -->
<task:scheduled ref="expireCronJob" method="runTask" cron="0 0/30 * * * ?"/>
</task:scheduled-tasks>
</beans>
</beans>
我不知道為什么,但這適用於我的本地開發和生產,但是如果我進行了其他更改,我必須小心,因為它可能適用於本地和開發,但不適用於生產。
如果有人使用 @Sceduled 這可能對你有用。
@Scheduled(cron = "${name-of-the-cron:0 0/30 * * * ?}")
這對我有用。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.