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


目录
相关文章
|
6月前
|
监控 Java BI
《深入理解Spring》定时任务——自动化调度的时间管理者
Spring定时任务通过@Scheduled注解和Cron表达式实现灵活调度,支持固定频率、延迟执行及动态配置,结合线程池与异常处理可提升可靠性,适用于报表生成、健康检查等场景,助力企业级应用自动化。
|
12月前
|
安全 Java API
深入解析 Spring Security 配置中的 CSRF 启用与 requestMatchers 报错问题
本文深入解析了Spring Security配置中CSRF启用与`requestMatchers`报错的常见问题。针对CSRF,指出默认已启用,无需调用`enable()`,只需移除`disable()`即可恢复。对于`requestMatchers`多路径匹配报错,分析了Spring Security 6.x中方法签名的变化,并提供了三种解决方案:分次调用、自定义匹配器及降级使用`antMatchers()`。最后提醒开发者关注版本兼容性,确保升级平稳过渡。
1322 2
|
9月前
|
前端开发 Java API
Spring Cloud Gateway Server Web MVC报错“Unsupported transfer encoding: chunked”解决
本文解析了Spring Cloud Gateway中出现“Unsupported transfer encoding: chunked”错误的原因,指出该问题源于Feign依赖的HTTP客户端与服务端的`chunked`传输编码不兼容,并提供了具体的解决方案。通过规范Feign客户端接口的返回类型,可有效避免该异常,提升系统兼容性与稳定性。
617 0
|
10月前
|
Java Spring
使用 Spring Boot 多个定时任务阻塞问题的解决方案
我是小假 期待与你的下一次相遇 ~
535 5
|
10月前
|
存储 前端开发 Java
|
Java 调度 Spring
Spring之定时任务基本使用篇
本文介绍了在Spring Boot项目中使用定时任务的基本方法。主要通过`@Scheduled`注解实现,需添加`@EnableScheduling`开启定时任务功能。文中详细解析了Cron表达式的语法及常见实例,如每秒、每天特定时间执行等。此外,还探讨了多个定时任务的执行方式(并行或串行)及其潜在问题,并留待后续深入讨论。
755 64
|
消息中间件 Java Kafka
【Azure Kafka】使用Spring Cloud Stream Binder Kafka 发送并接收 Event Hub 消息及解决并发报错
reactor.core.publisher.Sinks$EmissionException: Spec. Rule 1.3 - onSubscribe, onNext, onError and onComplete signaled to a Subscriber MUST be signaled serially.
262 7
|
12月前
|
前端开发 IDE Java
Spring MVC 中因导入错误的 Model 类报错问题解析
在 Spring MVC 或 Spring Boot 开发中,若导入错误的 `Model` 类(如 `ch.qos.logback.core.model.Model`),会导致无法解析 `addAttribute` 方法的错误。正确类应为 `org.springframework.ui.Model`。此问题通常因 IDE 自动导入错误类引起。解决方法包括:删除错误导入、添加正确包路径、验证依赖及清理缓存。确保代码中正确使用 Spring 提供的 `Model` 接口以实现前后端数据传递。
417 0
|
Java BI 调度
Java Spring的定时任务的配置和使用
遵循上述步骤,你就可以在Spring应用中轻松地配置和使用定时任务,满足各种定时处理需求。
848 2
|
存储 Java API
简单两步,Spring Boot 写死的定时任务也能动态设置:技术干货分享
【10月更文挑战第4天】在Spring Boot开发中,定时任务通常通过@Scheduled注解来实现,这种方式简单直接,但存在一个显著的限制:任务的执行时间或频率在编译时就已经确定,无法在运行时动态调整。然而,在实际工作中,我们往往需要根据业务需求或外部条件的变化来动态调整定时任务的执行计划。本文将分享一个简单两步的解决方案,让你的Spring Boot应用中的定时任务也能动态设置,从而满足更灵活的业务需求。
1254 4

热门文章

最新文章