The spring boot @Scheduled method is not working and throws Application run failed, java.lang.IllegalArgumentException: null exception if the scheduler is configured with an invalid value in the @Scheduled annotation. The @Scheduled annotation is configured for two arguments, fixedRate and fixedDelay. If the argument is set to an invalid value, the exception will be thrown.

@Scheduled(fixedRate = 0)
OR
@Scheduled(fixedDelay = 0)

The fixedRate argument in the @Scheduled annotation should be configured with positive values greater than zero. The fixed rate scheduler would operate on a periodic basis with the delay configured in the fixed rate. If the fixedRate argument is set to zero or negative values, the exception Application run failed, java.lang.IllegalArgumentException: null would be thrown.

The fixedDelay argument in the annotation @Scheduled should be configured with positive values greater than zero. The fixed delay scheduler will executed periodically with the fixed delay in between two execution. If the fixedDelay argument is configured with zero or negative values, the exception Application run failed, java.lang.IllegalArgumentException: null will be thrown.

The scheduler in the spring boot application is failed to start. The scheduler argument is invalid To make this scheduler functional, the proper value should be added.



Exception

2019-10-10 09:49:18.506 ERROR 39651 --- [           main] o.s.boot.SpringApplication               : Application run failed
 java.lang.IllegalArgumentException: null
     at java.util.concurrent.ScheduledThreadPoolExecutor.scheduleAtFixedRate(ScheduledThreadPoolExecutor.java:565) ~[na:1.8.0_101]
     at org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler.scheduleAtFixedRate(ThreadPoolTaskScheduler.java:348) ~[spring-context-5.1.9.RELEASE.jar:5.1.9.RELEASE]
     at org.springframework.scheduling.config.ScheduledTaskRegistrar.scheduleFixedRateTask(ScheduledTaskRegistrar.java:462) ~[spring-context-5.1.9.RELEASE.jar:5.1.9.RELEASE]
     at org.springframework.scheduling.config.ScheduledTaskRegistrar.scheduleFixedRateTask(ScheduledTaskRegistrar.java:436) ~[spring-context-5.1.9.RELEASE.jar:5.1.9.RELEASE]
     at org.springframework.scheduling.config.ScheduledTaskRegistrar.scheduleTasks(ScheduledTaskRegistrar.java:357) ~[spring-context-5.1.9.RELEASE.jar:5.1.9.RELEASE]
     at org.springframework.scheduling.config.ScheduledTaskRegistrar.afterPropertiesSet(ScheduledTaskRegistrar.java:332) ~[spring-context-5.1.9.RELEASE.jar:5.1.9.RELEASE]
     at org.springframework.scheduling.annotation.ScheduledAnnotationBeanPostProcessor.finishRegistration(ScheduledAnnotationBeanPostProcessor.java:300) ~[spring-context-5.1.9.RELEASE.jar:5.1.9.RELEASE]
     at org.springframework.scheduling.annotation.ScheduledAnnotationBeanPostProcessor.onApplicationEvent(ScheduledAnnotationBeanPostProcessor.java:231) ~[spring-context-5.1.9.RELEASE.jar:5.1.9.RELEASE]
     at org.springframework.scheduling.annotation.ScheduledAnnotationBeanPostProcessor.onApplicationEvent(ScheduledAnnotationBeanPostProcessor.java:103) ~[spring-context-5.1.9.RELEASE.jar:5.1.9.RELEASE]
     at org.springframework.context.event.SimpleApplicationEventMulticaster.doInvokeListener(SimpleApplicationEventMulticaster.java:172) ~[spring-context-5.1.9.RELEASE.jar:5.1.9.RELEASE]
     at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:165) ~[spring-context-5.1.9.RELEASE.jar:5.1.9.RELEASE]
     at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:139) ~[spring-context-5.1.9.RELEASE.jar:5.1.9.RELEASE]
     at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:402) ~[spring-context-5.1.9.RELEASE.jar:5.1.9.RELEASE]
     at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:359) ~[spring-context-5.1.9.RELEASE.jar:5.1.9.RELEASE]
     at org.springframework.context.support.AbstractApplicationContext.finishRefresh(AbstractApplicationContext.java:896) ~[spring-context-5.1.9.RELEASE.jar:5.1.9.RELEASE]
     at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:552) ~[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]


How to reproduce this issue

The scheduler is configured with fixed delay or with fixed rate. The scheduler will periodically run the java method based on the configuration. If the scheduler is configured with invalid value, scheduler will not be able to identify the periodicity to execute a java method. In the below example, the fixed delay value is configured as zero. scheduler can not run with the interval of zero milliseconds.

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


Root Cause

The Scheduler runs periodically through the interval specified in fixedRate or fixedDelay. If the irrelevant interval value is configured, the scheduler can not execute the task on a regular basic. The interval value should be greater than zero. It should be configured in @Scheduled annotation.



Scenario 1

The scheduler should be configured with fixed delay or with fixed rate of any positive integer value greater than zero. The scheduler runs the java method on a regular basis, based on the configured value. In the @Scheduled annotation, fixed delay should be configured with valid positive integer value greater than zero. The value is considered as time in milliseconds.

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


Scenario 2

This scenario shows how to configure with fixedDelay argument in the annotation @Scheduled. The fixedDelay should be configured with valid positive integer value greater than zero. The value is considered as time in milliseconds.

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



Leave a Reply