springboot2.0以上调度器如何配置线程池

发布时间:2021-11-30 16:20:37 作者:小新
来源:亿速云 阅读:244

这篇文章给大家分享的是有关springboot2.0以上调度器如何配置线程池的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。

  springboot2.0 以上spring task 开启多线程

  一 我们使用@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.fixedDelayTasks) {

  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郑州做人流手术 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());

  }

  }

  其中Scheduler 支持两种,种分别是:TaskScheduler 和 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());

  }

  }

  完成以上配置,即可让spring task 运行在多线程环境中。

感谢各位的阅读!关于“springboot2.0以上调度器如何配置线程池”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!

推荐阅读:
  1. laravel 任务调度器
  2. Quartz.Net调度框架配置解析

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

springboot2.0

上一篇:latch cache buffers chains的解决步骤是什么

下一篇:C/C++ Qt TreeWidget单层树形组件怎么应用

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》