Encountered invalid @Scheduled method ‘task’: Exactly one of the ‘cron’, ‘fixedDelay(String)’, or ‘fixedRate(String)’ attributes is required

The exception “Encountered invalid @Scheduled method ‘task’: Exactly one of the ‘cron’, ‘fixedDelay(String)’, or ‘fixedRate(String)’ attributes is required” is thrown due to error while loading the scheduler bean at the startup of the spring boot application.

Spring boot application allows scheduler to run on a regular basis. If spring boot does not load bean due to the configuration of the scheduler then this exception will be thrown.



Exception

2019-10-10 09:10:09.760 ERROR 36280 --- [           main] o.s.boot.SpringApplication               : Application run failed
 org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'scheduler' defined in file [C:/STS/workspace/SpringHelloWorld/target/classes/com/yawintutor/Scheduler.class]: Initialization of bean failed; nested exception is java.lang.IllegalStateException: Encountered invalid @Scheduled method 'task': Exactly one of the 'cron', 'fixedDelay(String)', or 'fixedRate(String)' attributes is required
     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:601) ~[spring-beans-5.1.9.RELEASE.jar:5.1.9.RELEASE]
     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:515) ~[spring-beans-5.1.9.RELEASE.jar:5.1.9.RELEASE]
     at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:320) ~[spring-beans-5.1.9.RELEASE.jar:5.1.9.RELEASE]
     at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) ~[spring-beans-5.1.9.RELEASE.jar:5.1.9.RELEASE]
     at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:318) ~[spring-beans-5.1.9.RELEASE.jar:5.1.9.RELEASE]
     at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199) ~[spring-beans-5.1.9.RELEASE.jar:5.1.9.RELEASE]
     at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:845) ~[spring-beans-5.1.9.RELEASE.jar:5.1.9.RELEASE]
     at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:877) ~[spring-context-5.1.9.RELEASE.jar:5.1.9.RELEASE]
     at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:549) ~[spring-context-5.1.9.RELEASE.jar:5.1.9.RELEASE]
     at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:744) [spring-boot-2.1.8.RELEASE.jar:2.1.8.RELEASE]
     at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:391) [spring-boot-2.1.8.RELEASE.jar:2.1.8.RELEASE]
     at org.springframework.boot.SpringApplication.run(SpringApplication.java:312) [spring-boot-2.1.8.RELEASE.jar:2.1.8.RELEASE]
     at org.springframework.boot.SpringApplication.run(SpringApplication.java:1215) [spring-boot-2.1.8.RELEASE.jar:2.1.8.RELEASE]
     at org.springframework.boot.SpringApplication.run(SpringApplication.java:1204) [spring-boot-2.1.8.RELEASE.jar:2.1.8.RELEASE]
     at com.yawintutor.SpringHelloWorldApplication.main(SpringHelloWorldApplication.java:12) [classes/:na]
 Caused by: java.lang.IllegalStateException: Encountered invalid @Scheduled method 'task': Exactly one of the 'cron', 'fixedDelay(String)', or 'fixedRate(String)' attributes is required
     at org.springframework.scheduling.annotation.ScheduledAnnotationBeanPostProcessor.processScheduled(ScheduledAnnotationBeanPostProcessor.java:496) ~[spring-context-5.1.9.RELEASE.jar:5.1.9.RELEASE]
     at org.springframework.scheduling.annotation.ScheduledAnnotationBeanPostProcessor.lambda$null$1(ScheduledAnnotationBeanPostProcessor.java:359) ~[spring-context-5.1.9.RELEASE.jar:5.1.9.RELEASE]
     at java.lang.Iterable.forEach(Iterable.java:75) ~[na:1.8.0_101]
     at org.springframework.scheduling.annotation.ScheduledAnnotationBeanPostProcessor.lambda$postProcessAfterInitialization$2(ScheduledAnnotationBeanPostProcessor.java:359) ~[spring-context-5.1.9.RELEASE.jar:5.1.9.RELEASE]
     at java.util.LinkedHashMap.forEach(LinkedHashMap.java:684) ~[na:1.8.0_101]
     at org.springframework.scheduling.annotation.ScheduledAnnotationBeanPostProcessor.postProcessAfterInitialization(ScheduledAnnotationBeanPostProcessor.java:358) ~[spring-context-5.1.9.RELEASE.jar:5.1.9.RELEASE]
     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsAfterInitialization(AbstractAutowireCapableBeanFactory.java:429) ~[spring-beans-5.1.9.RELEASE.jar:5.1.9.RELEASE]
     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1782) ~[spring-beans-5.1.9.RELEASE.jar:5.1.9.RELEASE]
     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:593) ~[spring-beans-5.1.9.RELEASE.jar:5.1.9.RELEASE]
     … 14 common frames omitted


Root Cause

Scheduler is configured with @Scheduled annotation. Schedules are configured either by fixedRate or fixedDelay. Fixed Rate will run on fixed interval regardless of delay in the past task execution. Fixed Delay will run with a delay from the previous execution.

If a spring boot scheduler is not configured with fixedDelay or with fixedRate or with cron expression, then scheduler can not be able to identify the periodicity to which the java method is called. If those parameters are either not configured or configured with invalid value, this error will be thrown.

Duration should be configured in both cases. The duration of the negative value is irrelevant.



How to reproduce this issue

If a spring boot scheduler is not configured with fixedDelay or with fixedRate or with cron expression, scheduler will not be able to identify the periodicity to which the java method is called. In the example below, the scheduler is configured with negative values. The initialization of the bean will fail due to negative value.

package com.yawintutor;
 import java.text.SimpleDateFormat;
 import java.util.Date;
 import org.springframework.scheduling.annotation.Scheduled;
 import org.springframework.stereotype.Component;
 @Component
 public class Scheduler {
    private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
 @Scheduled(fixedRate  = -1)
    public void task() {
       System.out.println("Scheduler task : " + sdf.format(new Date()));
    }
 }


Solution

The scheduler should be configured with fixedDelay or with fixedRate of any positive integer value greater than zero. Otherwise, it is necessary to configure the scheduler with cron expression. Configure fixedRate value with a positive integer value greater than zero in the @Scheduled annotation will resolve this issue. In this example, scheduler is configured with fixed rate of 2000 ms.

package com.yawintutor;
 import java.text.SimpleDateFormat;
 import java.util.Date;
 import org.springframework.scheduling.annotation.Scheduled;
 import org.springframework.stereotype.Component;
 @Component
 public class Scheduler {
    private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
 @Scheduled(fixedRate  = 2000)
    public void task() {
       System.out.println("Scheduler task : " + sdf.format(new Date()));
    }
 }



Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *