2011-11-02 260 views
36

我有以下彈簧作業每30分鐘後運行一次。請檢查我的cron表達式,是否正確?每30分鐘後彈簧cron表達

「0 0 0 * * 30」


下面是相關的春季配置文件中的完整的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> 
+4

我不幫助Spring選擇不使用cron格式,但將其命名爲'cronExpression' –

+0

@JosephLust - 這是非常真實的。另外在spring doc中,沒有提到這種區別... spring cron也能提供秒提供,但正常的unix cron是基於分鐘的......因爲在unix man pages分鐘是可以配置的最小時間單位。 – nanosoft

回答

36
<property name="cronExpression" value="0 0/30 * * * ?" /> 
93

按照Quartz-Scheduler Tutorial 它應該是value="0 0/30 * * * ?"

的cronExpression的場順序是

1.Seconds

2.Minutes

3.Hours

4.Day的日

5.Month

6.星期幾

7.Year(可選字段)

確保你至少有6個參數,否則你會得到一個錯誤(今年是可選)

+5

根據他們的文檔,Quartz需要額外的字段。這將是'0 0/30 * * *?',我相信。一般的方法是正確的,雖然 –

+0

你似乎正確 –

+0

堆棧它是拋出異常 屬性'cronExpression'拋出異常;嵌套異常是java.text.ParseException:意外的表達式結束。 –

29

圖形,對石英硅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分鐘可以說任一:

​​

您可以使用crontab.guru來檢查crontab表達式(聲明:我根本不涉及該頁面,只是我覺得它非常有用)。不過,考慮到這個頁面使用了UNIX中沒有秒的cron風格,而Spring作爲第一個字段。

+1

請注意,crontab.guru使用cron的Unix風格,它沒有秒數。 Spring cron使用秒數,所以第一個字段是第二個字段。 –

+0

@GaneshKrishnan好點。在答案中添加了它。謝謝! – fedorqui

1
在Web應用程序的Java春天什麼我

0 0/30 * * *工作

這將觸發例如10:00然後10:30 AM等..

<?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"> 
      <task:scheduled ref="expireCronJob" method="runTask" cron="0 0/30 * * * ?"/> <!-- every thirty minute --> 
     </task:scheduled-tasks> 

    </beans> 

</beans> 

我不知道爲什麼,但這是工作在我的本地開發和生產,但其他的變化,如果我做了我必須小心,因爲它可以在本地和開發上工作,但不能在生產上