Spring boot TaskScheduler is not effective when TaskRegistrar schedule tasks

筅森魡賤 提交于 2020-07-23 08:55:09

问题


I create schedule task without @scheduled annotation instead using taskregistrar (as I get more flexibility in cancelling, dynamically changing the delay etc - please don't focus on why I use taskregistrar). But then I noticed that TaskScheduler has no effect while doing so. Code is given below

    //in my SchedulingConfigurer
    @Override
    public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
        ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler();
        scheduler.setThreadNamePrefix("proc-task-pool-");
        scheduler.setPoolSize(10);//No use - always use 8
        scheduler.initialize();
        taskRegistrar.setTaskScheduler(scheduler);//commenting this line also result in same behavior
        taskRegistrar.scheduleFixedRateTask(new FixedRateTask(myService::processData,
                1000L,
                0L));
    }
    //in my service class 
    @Async
    public void processData() {
        log.info("inside processData {} {}", Thread.currentThread().getName(), LocalDateTime.now());
        try {
            Thread.sleep(20000L);
        } catch (InterruptedException e) {
            log.error("InterruptedException ");
        }
    }

Whether I set a custom scheduler or not, I noticed that my fixedRateTask is always processed by 8 threads. Below is the console log

inside processData task-1 2020-05-12T10:33:31.762
inside processData task-2 2020-05-12T10:33:32.743
inside processData task-3 2020-05-12T10:33:33.747
inside processData task-4 2020-05-12T10:33:34.742
inside processData task-5 2020-05-12T10:33:35.742
inside processData task-6 2020-05-12T10:33:36.739
inside processData task-7 2020-05-12T10:33:37.742
inside processData task-8 2020-05-12T10:33:38.743
inside processData task-1 2020-05-12T10:33:51.762
inside processData task-2 2020-05-12T10:33:52.744
inside processData task-3 2020-05-12T10:33:53.747
inside processData task-4 2020-05-12T10:33:54.743
inside processData task-5 2020-05-12T10:33:55.743

What went wrong?

EDIT

m-deinum's comment below helped me to resolve the problem.

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

    @Override
    public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
        this.scheduledTask = taskRegistrar.scheduleFixedRateTask(new FixedRateTask(myService::processData,
                configParams.getFixedratedelay(),
                0L));
    }

EDIT 2

m-deinum's another comment helped me to optimize. I removed the TaskExecutor bean altogether and used the following configuration property.

spring.task.execution.pool.core-size=20=100  //I wrongly used scheduling.pool.size earlier

来源:https://stackoverflow.com/questions/61746222/spring-boot-taskscheduler-is-not-effective-when-taskregistrar-schedule-tasks

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!