拨开荷叶行,寻梦已然成。仙女莲花里,翩翩白鹭情。
IMG-LOGO
主页 文章列表 SpringScheduler不执行阻塞任务

SpringScheduler不执行阻塞任务

白鹭 - 2022-03-04 1968 0 0

我在 Spring Boot 中运行以下调度程序。crontab 时间设定为每分钟执行该方法。然后我尝试阻止该方法 1.5 分钟。我观察到,该方法不会在第一次执行的下一分钟执行,直到回圈执行 1.5 分钟。我认为它以异步方式作业。现在很迷茫!春季调度程序是阻塞呼叫吗?

@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");
    }

uj5u.com热心网友回复:

Spring Scheduler 不执行阻塞任务

您可以通过例如(取自此处)配置多个执行绪:

 @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);
     }
 }
标签:

0 评论

发表评论

您的电子邮件地址不会被公开。 必填的字段已做标记 *