Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Spring Scheduler doesn't execute for blocking tasks

I am running the below scheduler in spring boot. The crontab time is set for every minute to execute the method. And then I try to block the method for 1.5 mins. I observed that, the method doesn’t execute in next minute of the first execution until the loop executes for 1.5 mins.
I thought it works in asynch ways. Now confused! Is spring scheduler a blocking call?

@Override
    @Scheduled(cron = "0 0/1 * * * ?")
    //@SchedulerLock(name = "test")
    public void test()
    {
        System.out.println("test scheduler enters:"+ LocalDateTime.now());
        //LockAssert.assertLocked();
        //System.out.println("now locked\n");
        long st=System.currentTimeMillis();

        while(true)
        {
            long diff=(System.currentTimeMillis()-st)/1000;
            if(diff>90)
            {
                System.out.println("breaking time:"+(System.currentTimeMillis()-st)/1000+", timeNow:"+ LocalDateTime.now());
                break;
            }
        }
        System.out.println("Out of the loop");
    }

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

As described in spring scheduler docs the taskScheduler runs by default with a single thread.

enter image description here

You can configure multiple threads by e.g (taken from here):

 @Configuration
 @EnableScheduling
 public class AppConfig implements SchedulingConfigurer {

     @Override
     public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
         taskRegistrar.setScheduler(taskExecutor());
     }

     @Bean(destroyMethod="shutdown")
     public Executor taskExecutor() {
         return Executors.newScheduledThreadPool(100);
     }
 }
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading