当springboot有多个不能名称但是相同bean的时候,可以使用 @Primary 注解设置其中一个为主要默认bean
有些类它没有用别名去注入bean,会导致注入的时候找到多个相同bean,报错
代码语言:javascript复制package com.jinw.config;
import com.jinw.utils.Threads;
import org.apache.commons.lang3.concurrent.BasicThreadFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.task.TaskExecutor;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.ThreadPoolExecutor;
/**
* 多线程配置
*
* @author admin
*/
@Configuration
@EnableAsync
public class ThreadConfig {
/**
* 短信类线程
*/
public static final String SMS = "smsTaskExecutor";
/**
* 默认通用线程池
*/
public static final String DEFAULT = "defaulTaskExecutor";
/**
* 周期性任务线程
*/
public static final String SCHEDULED = "scheduledExecutorService";
/**
* MAIL任务线程
*/
public static final String MAIL = "mailTaskExecutor";
// 核心线程池大小
private int corePoolSize = 50;
@Bean(DEFAULT)
@Primary
public TaskExecutor defaultExecutor() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
// 设置核心线程数
executor.setCorePoolSize(3);
// 设置最大线程数
executor.setMaxPoolSize(3);
// 设置队列容量
executor.setQueueCapacity(20);
// 设置线程活跃时间(秒)
executor.setKeepAliveSeconds(60);
// 设置默认线程名称
executor.setThreadNamePrefix(DEFAULT "-");
// 设置拒绝策略
executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
// 等待所有任务结束后再关闭线程池
executor.setWaitForTasksToCompleteOnShutdown(true);
return executor;
}
@Bean(SMS)
public TaskExecutor taskExecutor() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
// 设置核心线程数
executor.setCorePoolSize(3);
// 设置最大线程数
executor.setMaxPoolSize(3);
// 设置队列容量
executor.setQueueCapacity(20);
// 设置线程活跃时间(秒)
executor.setKeepAliveSeconds(60);
// 设置默认线程名称
executor.setThreadNamePrefix(SMS "-");
// 设置拒绝策略
executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
// 等待所有任务结束后再关闭线程池
executor.setWaitForTasksToCompleteOnShutdown(true);
return executor;
}
/**
* 执行周期性或定时任务
*/
@Bean(SCHEDULED)
protected ScheduledExecutorService scheduledExecutorService() {
return new ScheduledThreadPoolExecutor(corePoolSize,
new BasicThreadFactory.Builder().namingPattern("schedule-pool-%d").daemon(true).build()) {
@Override
protected void afterExecute(Runnable r, Throwable t) {
super.afterExecute(r, t);
Threads.printException(r, t);
}
};
}
/**
* MAIL定时任务
*/
@Bean(MAIL)
public TaskExecutor mailTaskExecutor() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
// 设置核心线程数
executor.setCorePoolSize(3);
// 设置最大线程数
executor.setMaxPoolSize(3);
// 设置队列容量
executor.setQueueCapacity(20);
// 设置线程活跃时间(秒)
executor.setKeepAliveSeconds(60);
// 设置默认线程名称
executor.setThreadNamePrefix(MAIL "-");
// 设置拒绝策略
executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
// 等待所有任务结束后再关闭线程池
executor.setWaitForTasksToCompleteOnShutdown(true);
return executor;
}
}