Invalid fixedRateString cannot parse into long

The scheduler interval is configured in milliseconds in spring boot application. The duration is a long value defined in @Scheduler configuration in fixedRate and fixedDelay variables. You can configure these values as a string parameter. If any error occurred when the string was converted to long, this exception will be thrown from the application.



Exception

2019-10-10 10:27:04.808 ERROR 42272 --- [           main] o.s.boot.SpringApplication               : Application run failed
 org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'scheduler' defined in file [D:/workspace/SpringHelloWorld/target/classes/com/yawintutor/Scheduler.class]: Initialization of bean failed; nested exception is java.lang.IllegalStateException: Encountered invalid @Scheduled method 'task': Invalid fixedRateString value "2000l" - cannot parse into long
     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': Invalid fixedRateString value "2000l" - cannot parse into long
     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

The spring boot scheduler interval value can be configured either as long value or string value. fixedRate and fixedDelay parameters are used to configure long value. fixedDelayString and fixedRateString are used to configure interval as string. These string values intern converts to long value before starting the scheduler. If any error occurred while converting from string to long, this error would happen



How to reproduce this issue

The below example will recreate this issue

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(fixedRateString  = "2000l")
   public void task() {
      System.out.println("Scheduler task with duration : " + sdf.format(new Date()));
   }
}


Solution

In the above example, the configured interval value contains a character. Parsing will fail if the interval value is converted from String to long value. add the long value (only digits) will resolve this issue.

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(fixedRateString  = "2000")
   public void task() {
      System.out.println("Scheduler task with duration : " + sdf.format(new Date()));
   }
}



Related Articles

Leave a Reply

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