2

我用@Scheduled註釋,使功能myProcess()在Spring MVC應用程序(版本3.0.6.RELEASE運行在Apache Tomcat/7.0.26)運行每小時一次( 3600000毫秒= 1小時):按預期執行春3.0 @Scheduled註釋工作不正常

@Scheduled(fixedRate = 3600000) 
public void myProcess() { ... } 

功能,但不是在清晨(見下文最近2天后該樣品日誌時間)。這每天都會發生。我在日誌文件中看不到任何例外。你有什麼想法可能是這種奇怪行爲的原因嗎?


Feb 13 02:11:15 
Feb 13 03:11:16 
Feb 13 06:17:34 
Feb 13 06:45:55 
Feb 13 07:03:22 
Feb 13 07:31:57 
Feb 13 08:11:16 
Feb 13 09:11:18 
Feb 13 10:11:18 
Feb 13 11:11:28 

Feb 14 01:11:37 
Feb 14 02:11:29 
Feb 14 03:11:29 
Feb 14 06:19:51 
Feb 14 06:49:17 
Feb 14 07:35:57 
Feb 14 08:11:29 
Feb 14 09:11:35 
+1

您使用的是哪個版本的Spring? – sp00m 2013-02-15 08:20:50

+0

@ sp00m:謝謝,Spring版本是3.0.6.RELEASE。我用這個信息更新了這個問題。 – Gruber 2013-02-15 08:34:34

回答

2

我有同樣的問題,但使用cron屬性:

// every day at midnight 
@Scheduled(cron = "0 0 0 * * ?") 
public void myProcess() { 
} 

我真的不記得它有行爲,但它不是一個我的預期。我終於發現它可能取決於Spring 3.0.x中的錯誤。我試圖(和工作)的解決方案,是除了申報任務中applicationContext.xml中文件,於註釋

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns:task="http://www.springframework.org/schema/task" xsi:schemaLocation="http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd"> 

    ... 

    <!-- scheduling and async --> 
    <task:annotation-driven /> 
    <task:scheduled-tasks> 
     <task:scheduled ref="myProcessHandler" method="myProcess" fixed-delay="0" /> 
    </task:scheduled-tasks> 
    <bean id="myProcessHandler" class="path.to.MyProcessHandler" /> 

    ... 

</beans> 

即使fixed-delay屬性是必要的修復bug (據我所知),它沒有考慮到,而註釋的cron屬性是。

我希望這將有助於。

+0

哦,可能是一個bug。我懷疑這一點。我會研究你提出的解決方案,看看我能否實現它。 – Gruber 2013-02-15 08:57:48

+0

不知道我是否錯過了一個觀點,但是如果我使用了_both_ XML和註解,我有兩個同時進程。只有XML也沒有工作,因爲事件只發射一次而不再發生。所以我切換到'cron'並添加了''給_root-context.xml_(必須刪除'')。終於奏效了。 – Gruber 2013-02-21 08:21:46

1

我不能給你一個答案的具體問題,我會嘗試使用最新春季版(3.2),因爲3.1,據我所知顯著的變化已在此實現在3.0和區。

但是,根據我的經驗,我發現cronTrigger在所有情況下更好的(它可以做當然一切的固定利率可以和這麼多)

就這樣定義你的屬性:

<util:properties id="props" location=classpath:/application.properties" /> 
<context:property-placeholder properties-ref="props" /> 
<task:annotation-driven /> 

然後用它:

@Scheduled(cron = "${cron.expression}") 
public void scheduledTask() throws .. { .. } 

凡在你有類似的application.properties: cron.expression = 0 0/60 * * *?

+0

是的,'cron'對我來說似乎是一個更好的選擇。雖然我喜歡'fixedRate',因爲它在啓動應用程序時正確啓動,在調試時非常方便。否則,我需要等待'cron'時間。 – Gruber 2013-02-15 08:46:16

+1

是的,這是事實。你可以有兩個:) – abalogh 2013-02-15 08:47:24

+0

我試過'@Scheduled(cron =「0 0 * * *?」)''。仍然出現相同的不規則執行模式。它對Spring 3.0.x中可能存在的錯誤的sp00m評論賦予更多權重。 – Gruber 2013-02-20 08:53:21