Get the App
SLTechnology News&Howtos  ›  Development  › 

How to configure thread pool for scheduler above springboot2.0

Shulou Source: shulou.com Published: 2022-06-02 09:27:25 10月03日 Update

This article is about how the scheduler above springboot2.0 configures thread pools. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

Enable multithreading with spring task above springboot2.0

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. Found that it created the ScheduledTaskRegistrar class

It is not difficult to read the code to find that the default configuration of the scheduler is as follows, 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

@ Configuration

Public class SchedulerConfig implements SchedulingConfigurer {

@ Bean Zhengzhou artificial abortion surgery http://rl.zyfuke.com/

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.

Thank you for reading! This is the end of the article on "how to configure the thread pool above the springboot2.0". I hope the above content can be helpful to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!

Tags: Configuration threading scheduling code content more articles good practical flow of people information articles time core annotations environment see knowledge Zhengzhou reference Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Information Shulou Tech Info Microsoft Huawei NVidia