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


目录
相关文章
|
3月前
|
资源调度 Java 调度
Spring Cloud Alibaba 集成分布式定时任务调度功能
定时任务在企业应用中至关重要,常用于异步数据处理、自动化运维等场景。在单体应用中,利用Java的`java.util.Timer`或Spring的`@Scheduled`即可轻松实现。然而,进入微服务架构后,任务可能因多节点并发执行而重复。Spring Cloud Alibaba为此发布了Scheduling模块,提供轻量级、高可用的分布式定时任务解决方案,支持防重复执行、分片运行等功能,并可通过`spring-cloud-starter-alibaba-schedulerx`快速集成。用户可选择基于阿里云SchedulerX托管服务或采用本地开源方案(如ShedLock)
129 1
|
1月前
|
Java BI 调度
Java Spring的定时任务的配置和使用
遵循上述步骤,你就可以在Spring应用中轻松地配置和使用定时任务,满足各种定时处理需求。
139 1
|
1月前
|
存储 Java API
简单两步,Spring Boot 写死的定时任务也能动态设置:技术干货分享
【10月更文挑战第4天】在Spring Boot开发中,定时任务通常通过@Scheduled注解来实现,这种方式简单直接,但存在一个显著的限制:任务的执行时间或频率在编译时就已经确定,无法在运行时动态调整。然而,在实际工作中,我们往往需要根据业务需求或外部条件的变化来动态调整定时任务的执行计划。本文将分享一个简单两步的解决方案,让你的Spring Boot应用中的定时任务也能动态设置,从而满足更灵活的业务需求。
100 4
|
1月前
|
XML Java 应用服务中间件
【Spring】运行Spring Boot项目,请求响应流程分析以及404和500报错
【Spring】运行Spring Boot项目,请求响应流程分析以及404和500报错
182 2
|
2月前
|
Java 应用服务中间件 Spring
IDEA 工具 启动 spring boot 的 main 方法报错。已解决
IDEA 工具 启动 spring boot 的 main 方法报错。已解决
|
2月前
|
前端开发 Java Spring
【非降版本解决】高版本Spring boot Swagger 报错解决方案
【非降版本解决】高版本Spring boot Swagger 报错解决方案
|
2月前
|
Java API 开发者
【已解决】Spring Cloud Feign 上传文件,提示:the request was rejected because no multipart boundary was found的问题
【已解决】Spring Cloud Feign 上传文件,提示:the request was rejected because no multipart boundary was found的问题
503 0
|
3月前
|
Java Spring
【Azure Spring Cloud】Spring Cloud Azure 4.0 调用Key Vault遇见认证错误 AADSTS90002: Tenant not found.
【Azure Spring Cloud】Spring Cloud Azure 4.0 调用Key Vault遇见认证错误 AADSTS90002: Tenant not found.
|
3月前
|
Java 开发者 Spring
Spring Boot实战宝典:揭秘定时任务的幕后英雄,让业务处理如流水般顺畅,轻松驾驭时间管理艺术!
【8月更文挑战第29天】在现代应用开发中,定时任务如数据备份、报告生成等至关重要。Spring Boot作为流行的Java框架,凭借其强大的集成能力和简洁的配置方式,为开发者提供了高效的定时任务解决方案。本文详细介绍了如何在Spring Boot项目中启用定时任务支持、编写定时任务方法,并通过实战案例展示了其在业务场景中的应用,同时提供了注意事项以确保任务的正确执行。
53 0
|
3月前
|
Java 开发工具 Spring
【Azure Spring Cloud】使用azure-spring-boot-starter-storage来上传文件报错: java.net.UnknownHostException: xxxxxxxx.blob.core.windows.net: Name or service not known
【Azure Spring Cloud】使用azure-spring-boot-starter-storage来上传文件报错: java.net.UnknownHostException: xxxxxxxx.blob.core.windows.net: Name or service not known
下一篇
无影云桌面