2013-06-28 111 views
1

我想在每個月的第3個週日發一個觸發器。在cron表達式我用 的cron = 「0 0 23?* 1#3」 但它給了我異常如何在每個月的第3個週日晚上11點寫Cron表達式來執行觸發器?

java.lang.NumberFormatException: For input string: "1#3" 
    at java.lang.NumberFormatException.forInputString(Unknown Source) 
    at java.lang.Integer.parseInt(Unknown Source) 
    at java.lang.Integer.valueOf(Unknown Source) 
    at org.springframework.scheduling.support.CronSequenceGenerator.getRange(CronSequenceGenerator.java:324) 
    at org.springframework.scheduling.support.CronSequenceGenerator.setNumberHits(CronSequenceGenerator.java:297) 
    at org.springframework.scheduling.support.CronSequenceGenerator.setDays(CronSequenceGenerator.java:275) 
    at org.springframework.scheduling.support.CronSequenceGenerator.parse(CronSequenceGenerator.java:241) 
    at org.springframework.scheduling.support.CronSequenceGenerator.<init>(CronSequenceGenerator.java:81) 
    at org.springframework.scheduling.support.CronTrigger.<init>(CronTrigger.java:54) 
    at org.springframework.scheduling.support.CronTrigger.<init>(CronTrigger.java:44) 
    at org.springframework.scheduling.config.ScheduledTaskRegistrar.afterPropertiesSet(ScheduledTaskRegistrar.java:188) 
    at org.springframework.scheduling.annotation.ScheduledAnnotationBeanPostProcessor.onApplicationEvent(ScheduledAnnotationBeanPostProcessor.java:209) 
    at org.springframework.scheduling.annotation.ScheduledAnnotationBeanPostProcessor.onApplicationEvent(ScheduledAnnotationBeanPostProcessor.java:1) 
    at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:97) 
    at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:324) 
    at org.springframework.context.support.AbstractApplicationContext.finishRefresh(AbstractApplicationContext.java:929) 
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:467) 
    at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:384) 
    at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:283) 
    at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:111) 
    at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4765) 
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5260) 
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150) 
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1525) 
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1515) 
    at java.util.concurrent.FutureTask$Sync.innerRun(Unknown Source) 
    at java.util.concurrent.FutureTask.run(Unknown Source) 
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(Unknown Source) 
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) 
    at java.lang.Thread.run(Unknown Source) 

下面是代碼,我想

@Scheduled(cron="0 0 23 ? * 1#3") // Fire at 11 PM on the third sunday of every month 
    public void sendReportNotCreatedNotificationToStudent() throws Exception{ 
     scheduleNotificationIntf.sendScheduleNotificationToStudent("createReportRemainder.html"); 
    } 

請任何人告訴有關此錯誤。我如何實現這個Cron表達式。

+0

你想要Java而不是基本shell的任何原因? –

+0

它是一個Web應用程序。我試圖在第三個星期天下午11點啓動一個調度程序 –

回答

1

看來你是使用cron條目語法不是由Spring支持。顯然,Spring 3.x僅支持「古典」cron條目格式......如crontab(5) manual entry中所述。請注意,不支持「x#y」語法。請參閱Spring CronSequenceGenerator class的javadoc。

但是您使用的語法似乎是Quartz crontab expression語法。


UPDATE

source codeCronSequenceGenerator在春天3.2.1檢查了一下是否有「#」不支持登錄場6.事實上,行號匹配您的堆棧跟蹤,所以我相信下面的答案是確定的。

我該如何實現這個Cron表達式。

你不能與春天。 Spring不支持那種Cron表達式。如果你想使用這種表達式,你將不得不切換到使用Quartz調度器。

+1

儘管這是被接受的答案,但下面的解決方法肯定有效。我在這裏發表評論,以防萬一有人想放棄... – Bartserk

0

你應該嘗試這種表達0 0 12 ? 1/1 SUN#3 *

+0

我試過這個,但仍然是相同的異常NumberFormatException:對於輸入字符串:「0#3」 –

5

我可能會晚一點,但碰巧遇到了同樣的問題,並且我提出了一個解決方法。我已經測試過了,它起作用了!此表達式:

@Scheduled(cron="0 0 23 1-7 * WED") // First wednesday of the month at 23:00 

將在本月的第一個星期三運行。解釋很簡單:第一個星期三總是在1-7範圍內的一天,所以如果我們過濾一個月的日子是在1到7之間,而星期幾是星期三,我們就完成了:)我沒有測試它,但使用相同的邏輯這些表達式應該也是如此:

@Scheduled(cron="0 0 23 8-14 * WED") // Second wednesday of the month at 23:00 
@Scheduled(cron="0 0 23 15-21 * WED") // Third wednesday of the month at 23:00 
@Scheduled(cron="0 0 23 22-28 * WED") // Fourth wednesday of the month at 23:00 
@Scheduled(cron="0 0 23 29-31 * WED") // Fifth wednesday of the month at 23:00 

我希望它有幫助!

相關問題