Spring 定时任务报错:Cron expression must consist of 6 fields (found 5 in “0 * * * *“)

简介: Spring定时任务-cron只能包含6个字段,即使不支持年份
+关注继续查看
Caused by: java.lang.IllegalStateException: Encountered invalid @Scheduled method 'vehicleTestingResultGenerator': Cron expression must consist of 6 fields (found 5 in "0 * * * *")
    at org.springframework.scheduling.annotation.ScheduledAnnotationBeanPostProcessor.processScheduled(ScheduledAnnotationBeanPostProcessor.java:499) ~[spring-context-5.2.5.RELEASE.jar:5.2.5.RELEASE]
    at org.springframework.scheduling.annotation.ScheduledAnnotationBeanPostProcessor.lambda$null$1(ScheduledAnnotationBeanPostProcessor.java:362) ~[spring-context-5.2.5.RELEASE.jar:5.2.5.RELEASE]
    at java.lang.Iterable.forEach(Iterable.java:75) ~[na:1.8.0_362]
    at org.springframework.scheduling.annotation.ScheduledAnnotationBeanPostProcessor.lambda$postProcessAfterInitialization$2(ScheduledAnnotationBeanPostProcessor.java:362) ~[spring-context-5.2.5.RELEASE.jar:5.2.5.RELEASE]
    at java.util.LinkedHashMap.forEach(LinkedHashMap.java:684) ~[na:1.8.0_362]
    at org.springframework.scheduling.annotation.ScheduledAnnotationBeanPostProcessor.postProcessAfterInitialization(ScheduledAnnotationBeanPostProcessor.java:361) ~[spring-context-5.2.5.RELEASE.jar:5.2.5.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsAfterInitialization(AbstractAutowireCapableBeanFactory.java:431) ~[spring-beans-5.2.5.RELEASE.jar:5.2.5.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1800) ~[spring-beans-5.2.5.RELEASE.jar:5.2.5.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:595) ~[spring-beans-5.2.5.RELEASE.jar:5.2.5.RELEASE]

Spring定时任务-cron只能包含6个字段,即使不支持年份


源码分析


@Scheduled

/**

* Annotation that marks a method to be scheduled. Exactly one of the

* {@link #cron}, {@link #fixedDelay}, or {@link #fixedRate} attributes must be

* specified.

*

* <p>The annotated method must expect no arguments. It will typically have

* a {@code void} return type; if not, the returned value will be ignored

* when called through the scheduler.

*

* <p>Processing of {@code @Scheduled} annotations is performed by

* registering a {@link ScheduledAnnotationBeanPostProcessor}. This can be

* done manually or, more conveniently, through the {@code <task:annotation-driven/>}

* element or @{@link EnableScheduling} annotation.

*

* <p>This annotation may be used as a <em>meta-annotation</em> to create custom

* <em>composed annotations</em> with attribute overrides.

*

* @author Mark Fisher

* @author Juergen Hoeller

* @author Dave Syer

* @author Chris Beams

* @since 3.0

* @see EnableScheduling

* @see ScheduledAnnotationBeanPostProcessor

* @see Schedules

*/

@Target({ElementType.METHOD, ElementType.ANNOTATION_TYPE})

@Retention(RetentionPolicy.RUNTIME)

@Documented

@Repeatable(Schedules.class)

public @interface Scheduled {

/**

 * A special cron expression value that indicates a disabled trigger: {@value}.

 * <p>This is primarily meant for use with <code>${...}</code> placeholders,

 * allowing for external disabling of corresponding scheduled methods.

 * @since 5.1

 * @see ScheduledTaskRegistrar#CRON_DISABLED

 */

String CRON_DISABLED = ScheduledTaskRegistrar.CRON_DISABLED;



/**

 * A cron-like expression, extending the usual UN*X definition to include triggers

 * on the second, minute, hour, day of month, month, and day of week.

 * <p>For example, {@code "0 * * * * MON-FRI"} means once per minute on weekdays

 * (at the top of the minute - the 0th second).

 * <p>The fields read from left to right are interpreted as follows.

 * <ul>

 * <li>second</li>

 * <li>minute</li>

 * <li>hour</li>

 * <li>day of month</li>

 * <li>month</li>

 * <li>day of week</li>

 * </ul>

 * <p>The special value {@link #CRON_DISABLED "-"} indicates a disabled cron

 * trigger, primarily meant for externally specified values resolved by a

 * <code>${...}</code> placeholder.

 * @return an expression that can be parsed to a cron schedule

 * @see org.springframework.scheduling.support.CronSequenceGenerator

 */

String cron() default "";

CronSequenceGenerator

/**

* Date sequence generator for a

* <a href="https://www.manpagez.com/man/5/crontab/">Crontab pattern</a>,

* allowing clients to specify a pattern that the sequence matches.

*

* <p>The pattern is a list of six single space-separated fields: representing

* second, minute, hour, day, month, weekday. Month and weekday names can be

* given as the first three letters of the English names.

*

* <p>Example patterns:

* <ul>

* <li>"0 0 * * * *" = the top of every hour of every day.</li>

* <li>"*&#47;10 * * * * *" = every ten seconds.</li>

* <li>"0 0 8-10 * * *" = 8, 9 and 10 o'clock of every day.</li>

* <li>"0 0 6,19 * * *" = 6:00 AM and 7:00 PM every day.</li>

* <li>"0 0/30 8-10 * * *" = 8:00, 8:30, 9:00, 9:30, 10:00 and 10:30 every day.</li>

* <li>"0 0 9-17 * * MON-FRI" = on the hour nine-to-five weekdays</li>

* <li>"0 0 0 25 12 ?" = every Christmas Day at midnight</li>

* </ul>

*

* @author Dave Syer

* @author Juergen Hoeller

* @author Ruslan Sibgatullin

* @since 3.0

* @see CronTrigger

*/

public class CronSequenceGenerator {


通过源码和源码中的例子,我们可以看到自从Spring3.0的版本后,cron表达式只能支持6个字段


解决方案

Spring集成Quartz


目录
相关文章
|
20天前
|
XML 监控 Java
spring定时任务Spring Scheduler
spring定时任务Spring Scheduler
9 0
|
22天前
|
XML Java 调度
Spring定时任务-任务执行和调度—官方原版
Spring定时任务-任务执行和调度—官方原版
22 0
|
2月前
|
Java Spring 容器
Spring Boot 中实现定时任务的两种方式!(二)
Spring Boot 中实现定时任务的两种方式!(二)
|
2月前
|
Java Spring
Spring Boot 中实现定时任务的两种方式!(一)
Spring Boot 中实现定时任务的两种方式!(一)
|
3月前
|
Java Spring
设置 Spring Boot 的定时任务线程池以优雅退出
设置 Spring Boot 的定时任务线程池以优雅退出
97 0
|
3月前
|
Java 应用服务中间件 Spring
Jeesite中部署定时任务,定时任务tomcat部署详细,定时任务在spring项目中部署
Jeesite中部署定时任务,定时任务tomcat部署详细,定时任务在spring项目中部署
101 0
|
3月前
|
Java Spring
Spring Boot 实现定时任务动态管理,太爽了!
Spring Boot 实现定时任务动态管理,太爽了!
|
4月前
|
Java BI 调度
Spring Boot定时任务
1.概述 在某些业务场景中,需要定时执行一些任务,有可能是定时统计然后生成报表,有可能是定时发起一个任务。最近在工作中就正好遇见一个定时发起问卷任务的一个业务场景,此处集合业务场景聊聊如何用spring boot来实现功能。
74 0
|
6月前
|
Java Spring
Spring Boot 如何解决多个定时任务阻塞问题?
Spring Boot 如何解决多个定时任务阻塞问题?
|
7月前
|
前端开发 Java Spring
Java Spring 定时任务配置调试多线程和执行多线程
遇到同一台服务器的定时任务部分执行的情况,暂时找不到原因,估计是因为线程被占满了,有些任务无法执行。 因项目用的是spring mvc的,这里暂时记录spring mvc配置,回头将spring boot的配置详细说明也加上。
148 0
相关产品
云迁移中心
推荐文章
更多