Invalid initialDelayString cannot parse into long

Spring boot provides good support for the creation of spring boot schedulers. Schedulers are the tasks that run periodically at a specific interval. As soon as the application starts, these schedulers will run. Spring boot allows these schedulers to be started after a certain delay period. This delay is configured in the annotation @scheduler in the initialDelay parameter.

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'schedulerInitialDelayString' 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 initialDelayString value "6000l" - cannot parse into long
     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:603) ~[spring-beans-5.2.1.RELEASE.jar:5.2.1.RELEASE]
     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:517) ~[spring-beans-5.2.1.RELEASE.jar:5.2.1.RELEASE]
     at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:323) ~[spring-beans-5.2.1.RELEASE.jar:5.2.1.RELEASE]
     at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) ~[spring-beans-5.2.1.RELEASE.jar:5.2.1.RELEASE]
     at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:321) ~[spring-beans-5.2.1.RELEASE.jar:5.2.1.RELEASE]
     at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202) ~[spring-beans-5.2.1.RELEASE.jar:5.2.1.RELEASE]
     at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:879) ~[spring-beans-5.2.1.RELEASE.jar:5.2.1.RELEASE]
     at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:878) ~[spring-context-5.2.1.RELEASE.jar:5.2.1.RELEASE]
     at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:550) ~[spring-context-5.2.1.RELEASE.jar:5.2.1.RELEASE]
     at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:747) [spring-boot-2.2.1.RELEASE.jar:2.2.1.RELEASE]
     at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397) [spring-boot-2.2.1.RELEASE.jar:2.2.1.RELEASE]
     at org.springframework.boot.SpringApplication.run(SpringApplication.java:315) [spring-boot-2.2.1.RELEASE.jar:2.2.1.RELEASE]
     at org.springframework.boot.SpringApplication.run(SpringApplication.java:1226) [spring-boot-2.2.1.RELEASE.jar:2.2.1.RELEASE]
     at org.springframework.boot.SpringApplication.run(SpringApplication.java:1215) [spring-boot-2.2.1.RELEASE.jar:2.2.1.RELEASE]
     at com.test.SpringSchedulerApplication.main(SpringSchedulerApplication.java:12) [classes/:na]
 Caused by: java.lang.IllegalStateException: Encountered invalid @Scheduled method 'task': Invalid initialDelayString value "6000l" - cannot parse into long
     at org.springframework.scheduling.annotation.ScheduledAnnotationBeanPostProcessor.processScheduled(ScheduledAnnotationBeanPostProcessor.java:499) ~[spring-context-5.2.1.RELEASE.jar:5.2.1.RELEASE]
     at org.springframework.scheduling.annotation.ScheduledAnnotationBeanPostProcessor.lambda$null$1(ScheduledAnnotationBeanPostProcessor.java:362) ~[spring-context-5.2.1.RELEASE.jar:5.2.1.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:362) ~[spring-context-5.2.1.RELEASE.jar:5.2.1.RELEASE]
     at java.util.LinkedHashMap.forEach(LinkedHashMap.java:684) ~[na:1.8.0_101]
     at org.springframework.scheduling.annotation.ScheduledAnnotationBeanPostProcessor.postProcessAfterInitialization(ScheduledAnnotationBeanPostProcessor.java:361) ~[spring-context-5.2.1.RELEASE.jar:5.2.1.RELEASE]
     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsAfterInitialization(AbstractAutowireCapableBeanFactory.java:431) ~[spring-beans-5.2.1.RELEASE.jar:5.2.1.RELEASE]
     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1807) ~[spring-beans-5.2.1.RELEASE.jar:5.2.1.RELEASE]
     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:595) ~[spring-beans-5.2.1.RELEASE.jar:5.2.1.RELEASE]
     … 14 common frames omitted


Root Cause

Spring boot allows these schedulers to be started after a certain delay period. This delay is configured in the annotation @scheduler in the initialDelayString parameter. If there should be a valid integer value for the initialDelayString parameter. Otherwise, the above exception will be thrown



How to reproduce this issue

This problem is replicated by the following code

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


Solution 1

In the above example, the configured initialDelayString value contains a invalid character. Parsing will fail if the initialDelayString 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(fixedDelayString  = "2000", initialDelayString = "6000")
   public void task() {
      System.out.println("Scheduler task with duration : " + sdf.format(new Date()));
   }
}


Solution 2

Instead of using initialDelayString, the initial delay value of the scheduler is configured using initialDelay parameter. During initialization, this parameter validates the 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(fixedDelayString  = "2000", initialDelay = 6000)
   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 *