我想在每个月的第三个星期日触发触发器。在我使用的cron表达式中 cron="0 0 23 ? * 1#3" 但它给了我异常(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)
这是我正在尝试的代码
@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 表达式。
最佳答案
我可能来晚了一点,但我碰巧遇到了完全相同的问题,而且我已经找到了解决方法。我已经测试过了,它有效!这个表达式:
@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
希望对您有所帮助!
关于java - 如何编写 Cron 表达式在每个月的第三个星期日晚上 11 点执行触发器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17357235/