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 :
As described in spring scheduler docs the taskScheduler runs by default with a single thread.
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);
}
}
