How to implement the thread pool configuration of the scheduler above springboot2.0
How to implement the above springboot2.0 scheduler configuration thread pool, I believe that many inexperienced people do not know what to do. Therefore, this paper summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.
When we open the spring task scheduler with @ EnableScheduling, we find that the scheduler is configured to be single-threaded by default.
Second, open the annotation and find its configuration information in this SchedulingConfiguration class. It is not difficult to find that the default configuration of the scheduler is the following code, and the thread pool is single-threaded.
Protected void scheduleTasks () {if (this.taskScheduler = = null) {this.localExecutor = Executors.newSingleThreadScheduledExecutor (); this.taskScheduler = new ConcurrentTaskScheduler (this.localExecutor);} if (this.triggerTasks! = null) {for (TriggerTask task: this.triggerTasks) {addScheduledTask (scheduleTriggerTask (task));} if (this.cronTasks! = null) {for (CronTask task: this.cronTasks) {addScheduledTask (scheduleCronTask (task)) } if (this.fixedRateTasks! = null) {for (IntervalTask task: this.fixedRateTasks) {addScheduledTask (scheduleFixedRateTask (task));}} if (this.fixedDelayTasks! = null) {for (IntervalTask task: this.fixedDelayTasks) {addScheduledTask (scheduleFixedDelayTask (task));}
How do I change this configuration?
If you want to change the configuration, you only need the following core code
Package com.ccbobe.common.config;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.scheduling.annotation.EnableScheduling;import org.springframework.scheduling.annotation.SchedulingConfigurer;import org.springframework.scheduling.config.ScheduledTaskRegistrar;import java.util.concurrent.ScheduledExecutorService;import java.util.concurrent.ScheduledThreadPoolExecutor;@EnableScheduling@Configurationpublic class SchedulerConfig implements SchedulingConfigurer {@ Bean public ScheduledExecutorService concurrentTaskScheduler () {ScheduledThreadPoolExecutor executorService = new ScheduledThreadPoolExecutor (20); executorService.setMaximumPoolSize (20); executorService.setRejectedExecutionHandler (new ScheduledThreadPoolExecutor.CallerRunsPolicy ()); return executorService @ Override public void configureTasks (ScheduledTaskRegistrar taskRegistrar) {taskRegistrar.setScheduler (concurrentTaskScheduler ());}}
Among them, Scheduler supports two kinds, which are TaskScheduler and ScheduledExecutorService
/ * Set the {@ link TaskScheduler} to register scheduled tasks with, or a * {@ link java.util.concurrent.ScheduledExecutorService} to be wrapped as a * {@ code TaskScheduler}. * / public void setScheduler (@ Nullable Object scheduler) {if (scheduler = = null) {this.taskScheduler = null;} else if (scheduler instanceof TaskScheduler) {this.taskScheduler = (TaskScheduler) scheduler;} else if (scheduler instanceof ScheduledExecutorService) {this.taskScheduler = new ConcurrentTaskScheduler (ScheduledExecutorService) scheduler));} else {throw new IllegalArgumentException ("Unsupported scheduler type:" + scheduler.getClass ());}}
After completing the above configuration, spring task can be run in a multithreaded environment.
After reading the above, have you mastered how to implement the thread pool configuration of the above springboot2.0 scheduler? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!