Reputation: 99
I want to fire a trigger on 3rd Sunday of every month. In cron expression I used cron="0 0 23 ? * 1#3" But its gives me Exception
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)
Here is code I am trying
@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");
}
Please anyone tell about this error. How can I achieve this Cron expression.
Upvotes: 1
Views: 8246
Reputation: 675
I might come a bit late, but I happened to have the same exact issue, and I've come with a workaround. I've tested it, and it works! This expression:
@Scheduled(cron="0 0 23 1-7 * WED") // First wednesday of the month at 23:00
Will run for the first wednesday of the month. The explanation is simple: the first wednesday will always be a day within the 1-7 range, so if we filter the day of the month to be between 1 and 7, and the week day to be Wednesday, we have it done :) I haven't tested it, but using the same logic these expressions should be working as well:
@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
I hope it helps!
Upvotes: 5
Reputation: 718906
It seems that that the cron entry syntax you are using is not supported by Spring. Apparently, Spring 3.x only supports "classical" cron entry formats ... as documented in the crontab(5)
manual entry. Note that "x#y" syntax is not supported. See also the javadoc for the Spring CronSequenceGenerator
class.
But the syntax you are using appears to be the Quartz crontab expression syntax.
UPDATE
A brief examination of the source code for CronSequenceGenerator
in Spring 3.2.1 has no sign of support for "#" in field 6. Indeed, the line numbers match up with your stacktrace, so I'm confident that the following answer is definitive.
How can I achieve this Cron expression.
You can't with Spring. Spring does not support that kind of Cron expression. If you want to use that kind of expression, you will have to switch to using the Quartz scheduler.
Upvotes: 1
Reputation: 1
Try using quartz scheduler instead of spring scheduler. http://quartz-scheduler.org/documentation/quartz-2.x/tutorials/tutorial-lesson-06
Upvotes: -1