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

白茶清欢无别事,我在等风也等你,苦酒折柳今相离,无风无月也无你。
原文地址:https://www.cnblogs.com/jiannanchun/p/15476415.html