Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to configure thread pool for scheduler above springboot2.0

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report