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


目录
相关文章
|
1月前
|
druid Java 数据库
Spring Boot的定时任务与异步任务
Spring Boot的定时任务与异步任务
|
3月前
|
缓存 负载均衡 Java
Spring Cloud Alibaba client升级问题之升级报错如何解决
Spring Cloud Alibaba提供了一套在Spring Cloud框架基础上构建的微服务解决方案,旨在简化分布式系统的开发和管理;本合集将探讨Spring Cloud Alibaba在实际应用中的部署和使用技巧,以及该框架常见问题的诊断方法和解决步骤。
|
3月前
|
Java 调度 Maven
Spring Task 自定义定时任务类
Spring Task 自定义定时任务类
36 0
|
30天前
|
Java Apache vr&ar
springmvc报错 nested exception is org.mybatis.spring.MyBatisSystemException:
springmvc报错 nested exception is org.mybatis.spring.MyBatisSystemException:
15 0
|
30天前
|
Java Windows Perl
mybatis+spring报错PropertyAccessException 1: org.springframework.beans.MethodInvocationException
mybatis+spring报错PropertyAccessException 1: org.springframework.beans.MethodInvocationException
8 0
|
30天前
|
前端开发 Java Spring
ssm中spring mvc找不到控制器,报错404
ssm中spring mvc找不到控制器,报错404
14 0
|
1月前
|
Java BI 调度
Spring Boot 整合xxl-job实现分布式定时任务
XXL-JOB是一个分布式任务调度平台,其核心设计目标是开发迅速、学习简单、轻量级、易扩展。现已开放源代码并接入多家公司线上产品线,开箱即用。 xxl是xxl-job的开发者大众点评的许雪里名称的拼音开头。
|
2月前
spring3.0定时任务
spring3.0定时任务
|
2月前
|
监控 Java 测试技术
Spring Boot和XXL-Job:高效定时任务管理
Spring Boot和XXL-Job:高效定时任务管理
57 0
|
2月前
|
Java Spring
spring-doc报错Unable to render this definition
spring-doc报错Unable to render this definition
155 0