Unexpected error occurred in scheduled task

Schedulers in spring boot framework runs the configured the task method periodically. If any error occurred in the task method, the unexpected error exception is thrown by the spring boot. This is due to the logical issue in the code.



Exception

2019-10-10 12:14:00.007 ERROR 49298 --- [   scheduling-1] o.s.s.s.TaskUtils$LoggingErrorHandler    : Unexpected error occurred in scheduled task.
 java.lang.RuntimeException: Example to throw a runtime exception
     at com.yawintutor.Scheduler.task(Scheduler.java:16) ~[classes/:na]
     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_101]
     at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_101]
     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_101]
     at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_101]
     at org.springframework.scheduling.support.ScheduledMethodRunnable.run(ScheduledMethodRunnable.java:84) ~[spring-context-5.1.9.RELEASE.jar:5.1.9.RELEASE]
     at org.springframework.scheduling.support.DelegatingErrorHandlingRunnable.run(DelegatingErrorHandlingRunnable.java:54) ~[spring-context-5.1.9.RELEASE.jar:5.1.9.RELEASE]
     at org.springframework.scheduling.concurrent.ReschedulingRunnable.run(ReschedulingRunnable.java:93) [spring-context-5.1.9.RELEASE.jar:5.1.9.RELEASE]
     at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) [na:1.8.0_101]
     at java.util.concurrent.FutureTask.run(FutureTask.java:266) [na:1.8.0_101]
     at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:180) [na:1.8.0_101]
     at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:293) [na:1.8.0_101]
     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) [na:1.8.0_101]
     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) [na:1.8.0_101]
     at java.lang.Thread.run(Thread.java:745) [na:1.8.0_101]


Root Cause

Schedulers will invoke the configured task class method as per configured interval. If any error occurred in the task method due to any logical issue or bug in the code, then the configured method will throw exception. Scheduler can not understood the issue as it is thrown from the custom code. So the scheduler will throw unexpected error.



How to reproduce this issue

If the task method throws any exception, then this issue occurs. The example below will create a customized RuntimeException.

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  = 5000)
   public void task() {
      System.out.println("Scheduler task with duration : " + sdf.format(new Date()));
      throw new RuntimeException("Example to throw a runtime exception");
   }
}


Solution

Do the code review in the task method, identify the logical issue or code bug. Fixing the bug 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(fixedRate  = 5000)
    public void task() {
       System.out.println("Scheduler task with duration : " + sdf.format(new Date()));
       // throw new RuntimeException("Example to throw a runtime exception");
    }
 }



Related Articles

Leave a Reply

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