轻量级前端框架助力开发者提升项目效率与性能
669
2023-07-03
springboot2.0以上调度器配置线程池的实现
一 我们使用@EnableScheduling 开启spring task 调度器的时候,发现此调度器默认配置为单线程的。
二 打开注解发现其配置信息在此SchedulingConfiguration类中。发现其创建了ScheduledTaskRegistrar类
研读代码不难发现调度器默认配置是如下代码,线程池为单线程的。
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.fihttp://xedDelayTasks) {
addScheduledTask(scheduleFixedDelayTask(task));
}
}
}
如何改变此配置呢?
如果想改变其中配置则只需要如下核心代码
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
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());
}
}
其中Scheduler 支持两种,种分别是:TaskScheduler 和 ScheduledExecutorService
/**
* Set the {@link TaskScheduler} to register scheduledhttp:// 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());
}
}
完成以上配置,即可让spring task 运行在多线程环境中。
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~