zoukankan      html  css  js  c++  java
  • 使@schedule支持多线程的配置类

    package com.longshine.goverquartz.core.config;

    import org.springframework.boot.autoconfigure.batch.BatchProperties;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.scheduling.annotation.Scheduled;
    import org.springframework.scheduling.annotation.SchedulingConfigurer;
    import org.springframework.scheduling.config.ScheduledTaskRegistrar;
    import org.springframework.util.CollectionUtils;

    import java.lang.reflect.Method;
    import java.util.Arrays;
    import java.util.concurrent.Executors;

    /**
    * @description: 使@schedule支持多线程的配置类
    * @author: cc x
    * @create: 2020-12-08
    **/
    @Configuration
    public class ScheduleConfig implements SchedulingConfigurer {

    @Override
    public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
    Method[] methods = BatchProperties.Job.class.getMethods();
    int defaultPoolSize = 3;//自定义默认线程池大小
    int corePoolSize = 0;
    if (!CollectionUtils.isEmpty(Arrays.asList(methods))) {
    for (Method method : methods) {
    Scheduled annotation = method.getAnnotation(Scheduled.class);//扫描方法上的注解
    if (annotation != null) {
    corePoolSize++;//有多少个方法上边有注解就初始化线程池多少个线程数量
    }
    }
    if (defaultPoolSize > corePoolSize) {//当你需要的线程数量大于基础线程数量 就按基础的来
    corePoolSize = defaultPoolSize;
    }
    taskRegistrar.setScheduler(Executors.newScheduledThreadPool(corePoolSize));
    }
    }
    }

    白茶清欢无别事,我在等风也等你,苦酒折柳今相离,无风无月也无你。
  • 相关阅读:
    23 抽象类 abstract
    22.4 Extends --super 和 this 的区别
    22.3 Extends 构造方法的执行顺序
    22.2 继承的 成员变量的执行顺序
    22.1 Extends 继承的 方法重写@Override 和 方法重载Overload 的区别
    22 Extends 继承(子类、父类)
    21.3代码块考试题
    vue-cli3安装
    ==隐式转换是怎样进行的?
    为什么有时候人们用translate来改变位置而不是定位?
  • 原文地址:https://www.cnblogs.com/jiannanchun/p/15476415.html
Copyright © 2011-2022 走看看