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));
    }
    }
    }

    白茶清欢无别事,我在等风也等你,苦酒折柳今相离,无风无月也无你。
  • 相关阅读:
    Spring 定时器的使用
    spring MVC 资料
    Thrift入门及Java实例演示<转载备用>
    json数组转数组对象
    UiPath Outlook邮件正文引用图片
    UiPath 执行VBA代码Selection.Copy复制不生效
    RPA工程师学习路径是怎样的?企业面试开发者从哪些方面考察?
    一个RPA项目需要部署多少个机器人
    未来的企业软件和RPA
    RPA——被遮住的代码
  • 原文地址:https://www.cnblogs.com/jiannanchun/p/15476415.html
Copyright © 2011-2022 走看看