mirror of
				https://github.com/continew-org/continew-admin.git
				synced 2025-10-31 10:57:13 +08:00 
			
		
		
		
	refactor: 适配线程池自动配置
This commit is contained in:
		| @@ -1,74 +0,0 @@ | |||||||
| /* |  | ||||||
|  * Copyright (c) 2022-present Charles7c Authors. All Rights Reserved. |  | ||||||
|  * |  | ||||||
|  * Licensed under the Apache License, Version 2.0 (the "License"); |  | ||||||
|  * you may not use this file except in compliance with the License. |  | ||||||
|  * You may obtain a copy of the License at |  | ||||||
|  * |  | ||||||
|  *     http://www.apache.org/licenses/LICENSE-2.0 |  | ||||||
|  * |  | ||||||
|  * Unless required by applicable law or agreed to in writing, software |  | ||||||
|  * distributed under the License is distributed on an "AS IS" BASIS, |  | ||||||
|  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |  | ||||||
|  * See the License for the specific language governing permissions and |  | ||||||
|  * limitations under the License. |  | ||||||
|  */ |  | ||||||
|  |  | ||||||
| package top.charles7c.cnadmin.common.config.threadpool; |  | ||||||
|  |  | ||||||
| import java.util.Arrays; |  | ||||||
| import java.util.concurrent.Executor; |  | ||||||
| import java.util.concurrent.ScheduledExecutorService; |  | ||||||
|  |  | ||||||
| import lombok.RequiredArgsConstructor; |  | ||||||
| import lombok.extern.slf4j.Slf4j; |  | ||||||
|  |  | ||||||
| import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler; |  | ||||||
| import org.springframework.context.annotation.Configuration; |  | ||||||
| import org.springframework.scheduling.annotation.AsyncConfigurer; |  | ||||||
| import org.springframework.scheduling.annotation.EnableAsync; |  | ||||||
|  |  | ||||||
| import cn.hutool.core.util.ArrayUtil; |  | ||||||
|  |  | ||||||
| import top.charles7c.cnadmin.common.exception.ServiceException; |  | ||||||
|  |  | ||||||
| /** |  | ||||||
|  * 异步任务执行配置 |  | ||||||
|  * |  | ||||||
|  * @author Lion Li(RuoYi-Vue-Plus) |  | ||||||
|  * @author Charles7c |  | ||||||
|  * @since 2022/12/23 22:33 |  | ||||||
|  */ |  | ||||||
| @Slf4j |  | ||||||
| @Configuration |  | ||||||
| @RequiredArgsConstructor |  | ||||||
| @EnableAsync(proxyTargetClass = true) |  | ||||||
| public class AsyncConfiguration implements AsyncConfigurer { |  | ||||||
|  |  | ||||||
|     private final ScheduledExecutorService scheduledExecutorService; |  | ||||||
|  |  | ||||||
|     /** |  | ||||||
|      * 异步任务 @Async 执行时,使用 Java 内置线程池 |  | ||||||
|      */ |  | ||||||
|     @Override |  | ||||||
|     public Executor getAsyncExecutor() { |  | ||||||
|         return scheduledExecutorService; |  | ||||||
|     } |  | ||||||
|  |  | ||||||
|     /** |  | ||||||
|      * 异步任务执行时的异常处理 |  | ||||||
|      */ |  | ||||||
|     @Override |  | ||||||
|     public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() { |  | ||||||
|         return (throwable, method, objects) -> { |  | ||||||
|             throwable.printStackTrace(); |  | ||||||
|             StringBuilder sb = new StringBuilder(); |  | ||||||
|             sb.append("Exception message - ").append(throwable.getMessage()).append(", Method name - ") |  | ||||||
|                 .append(method.getName()); |  | ||||||
|             if (ArrayUtil.isNotEmpty(objects)) { |  | ||||||
|                 sb.append(", Parameter value - ").append(Arrays.toString(objects)); |  | ||||||
|             } |  | ||||||
|             throw new ServiceException(sb.toString()); |  | ||||||
|         }; |  | ||||||
|     } |  | ||||||
| } |  | ||||||
| @@ -1,85 +0,0 @@ | |||||||
| /* |  | ||||||
|  * Copyright (c) 2022-present Charles7c Authors. All Rights Reserved. |  | ||||||
|  * |  | ||||||
|  * Licensed under the Apache License, Version 2.0 (the "License"); |  | ||||||
|  * you may not use this file except in compliance with the License. |  | ||||||
|  * You may obtain a copy of the License at |  | ||||||
|  * |  | ||||||
|  *     http://www.apache.org/licenses/LICENSE-2.0 |  | ||||||
|  * |  | ||||||
|  * Unless required by applicable law or agreed to in writing, software |  | ||||||
|  * distributed under the License is distributed on an "AS IS" BASIS, |  | ||||||
|  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |  | ||||||
|  * See the License for the specific language governing permissions and |  | ||||||
|  * limitations under the License. |  | ||||||
|  */ |  | ||||||
|  |  | ||||||
| package top.charles7c.cnadmin.common.config.threadpool; |  | ||||||
|  |  | ||||||
| import java.util.concurrent.ScheduledExecutorService; |  | ||||||
| import java.util.concurrent.ScheduledThreadPoolExecutor; |  | ||||||
| import java.util.concurrent.ThreadPoolExecutor; |  | ||||||
|  |  | ||||||
| import lombok.extern.slf4j.Slf4j; |  | ||||||
|  |  | ||||||
| import org.apache.commons.lang3.concurrent.BasicThreadFactory; |  | ||||||
| import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; |  | ||||||
| import org.springframework.boot.context.properties.EnableConfigurationProperties; |  | ||||||
| import org.springframework.context.annotation.Bean; |  | ||||||
| import org.springframework.context.annotation.Configuration; |  | ||||||
| import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; |  | ||||||
|  |  | ||||||
| import top.charles7c.cnadmin.common.util.ExceptionUtils; |  | ||||||
|  |  | ||||||
| /** |  | ||||||
|  * 线程池配置 |  | ||||||
|  * |  | ||||||
|  * @author Lion Li(RuoYi-Vue-Plus) |  | ||||||
|  * @author Charles7c |  | ||||||
|  * @since 2022/12/23 23:13 |  | ||||||
|  */ |  | ||||||
| @Slf4j |  | ||||||
| @Configuration |  | ||||||
| @EnableConfigurationProperties(ThreadPoolProperties.class) |  | ||||||
| public class ThreadPoolConfiguration { |  | ||||||
|  |  | ||||||
|     /** 核心(最小)线程数 = CPU 核心数 + 1 */ |  | ||||||
|     private final int corePoolSize = Runtime.getRuntime().availableProcessors() + 1; |  | ||||||
|  |  | ||||||
|     /** |  | ||||||
|      * Spring 内置线程池:ThreadPoolTaskExecutor |  | ||||||
|      */ |  | ||||||
|     @Bean |  | ||||||
|     @ConditionalOnProperty(prefix = "thread-pool", name = "enabled", havingValue = "true") |  | ||||||
|     public ThreadPoolTaskExecutor threadPoolTaskExecutor(ThreadPoolProperties threadPoolProperties) { |  | ||||||
|         ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor(); |  | ||||||
|         // 核心(最小)线程数 |  | ||||||
|         executor.setCorePoolSize(corePoolSize); |  | ||||||
|         // 最大线程数 |  | ||||||
|         executor.setMaxPoolSize(corePoolSize * 2); |  | ||||||
|         // 队列容量 |  | ||||||
|         executor.setQueueCapacity(threadPoolProperties.getQueueCapacity()); |  | ||||||
|         // 活跃时间 |  | ||||||
|         executor.setKeepAliveSeconds(threadPoolProperties.getKeepAliveSeconds()); |  | ||||||
|         // 配置当池内线程数已达到上限的时候,该如何处理新任务:不在新线程中执行任务,而是由调用者所在的线程来执行 |  | ||||||
|         executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy()); |  | ||||||
|         return executor; |  | ||||||
|     } |  | ||||||
|  |  | ||||||
|     /** |  | ||||||
|      * Java 内置线程池:ScheduledExecutorService(适用于执行周期性或定时任务) |  | ||||||
|      */ |  | ||||||
|     @Bean |  | ||||||
|     public ScheduledExecutorService scheduledExecutorService() { |  | ||||||
|         return new ScheduledThreadPoolExecutor(corePoolSize, |  | ||||||
|             new BasicThreadFactory.Builder().namingPattern("schedule-pool-%d").daemon(true).build(), |  | ||||||
|             new ThreadPoolExecutor.CallerRunsPolicy()) { |  | ||||||
|  |  | ||||||
|             @Override |  | ||||||
|             protected void afterExecute(Runnable runnable, Throwable throwable) { |  | ||||||
|                 super.afterExecute(runnable, throwable); |  | ||||||
|                 ExceptionUtils.printException(runnable, throwable); |  | ||||||
|             } |  | ||||||
|         }; |  | ||||||
|     } |  | ||||||
| } |  | ||||||
| @@ -1,43 +0,0 @@ | |||||||
| /* |  | ||||||
|  * Copyright (c) 2022-present Charles7c Authors. All Rights Reserved. |  | ||||||
|  * |  | ||||||
|  * Licensed under the Apache License, Version 2.0 (the "License"); |  | ||||||
|  * you may not use this file except in compliance with the License. |  | ||||||
|  * You may obtain a copy of the License at |  | ||||||
|  * |  | ||||||
|  *     http://www.apache.org/licenses/LICENSE-2.0 |  | ||||||
|  * |  | ||||||
|  * Unless required by applicable law or agreed to in writing, software |  | ||||||
|  * distributed under the License is distributed on an "AS IS" BASIS, |  | ||||||
|  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |  | ||||||
|  * See the License for the specific language governing permissions and |  | ||||||
|  * limitations under the License. |  | ||||||
|  */ |  | ||||||
|  |  | ||||||
| package top.charles7c.cnadmin.common.config.threadpool; |  | ||||||
|  |  | ||||||
| import lombok.Data; |  | ||||||
|  |  | ||||||
| import org.springframework.boot.context.properties.ConfigurationProperties; |  | ||||||
|  |  | ||||||
| /** |  | ||||||
|  * 线程池配置属性 |  | ||||||
|  * |  | ||||||
|  * @author Lion Li(RuoYi-Vue-Plus) |  | ||||||
|  * @author Charles7c |  | ||||||
|  * @since 2022/12/23 23:06 |  | ||||||
|  */ |  | ||||||
| @Data |  | ||||||
| @ConfigurationProperties(prefix = "thread-pool") |  | ||||||
| public class ThreadPoolProperties { |  | ||||||
|  |  | ||||||
|     /** |  | ||||||
|      * 队列容量 |  | ||||||
|      */ |  | ||||||
|     private int queueCapacity; |  | ||||||
|  |  | ||||||
|     /** |  | ||||||
|      * 活跃时间 |  | ||||||
|      */ |  | ||||||
|     private int keepAliveSeconds; |  | ||||||
| } |  | ||||||
| @@ -216,12 +216,11 @@ management.health: | |||||||
|  |  | ||||||
| --- ### 线程池配置 | --- ### 线程池配置 | ||||||
| thread-pool: | thread-pool: | ||||||
|   # 是否启用线程池 |  | ||||||
|   enabled: true |   enabled: true | ||||||
|   # 队列容量 |   # 队列容量 | ||||||
|   queueCapacity: 128 |   queue-capacity: 128 | ||||||
|   # 活跃时间 |   # 活跃时间(单位:秒) | ||||||
|   keepAliveSeconds: 300 |   keep-alive-seconds: 300 | ||||||
|  |  | ||||||
| --- ### 代码生成器配置 | --- ### 代码生成器配置 | ||||||
| generator: | generator: | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user