From 69a25cd577d5639f2869bcf80f498b373e80137e Mon Sep 17 00:00:00 2001
From: 于杰 <1210123631@qq.com>
Date: 星期三, 17 十二月 2025 10:43:44 +0800
Subject: [PATCH] 修改旋转逻辑,增加辅助旋转功能
---
north-glass-erp/src/main/java/com/example/erp/common/AsyncQueryExecutor.java | 75 +++++++++++++++++++++++++++++++++++++
1 files changed, 75 insertions(+), 0 deletions(-)
diff --git a/north-glass-erp/src/main/java/com/example/erp/common/AsyncQueryExecutor.java b/north-glass-erp/src/main/java/com/example/erp/common/AsyncQueryExecutor.java
new file mode 100644
index 0000000..3304db9
--- /dev/null
+++ b/north-glass-erp/src/main/java/com/example/erp/common/AsyncQueryExecutor.java
@@ -0,0 +1,75 @@
+package com.example.erp.common;
+
+import org.springframework.stereotype.Component;
+
+import javax.annotation.PreDestroy;
+import java.util.concurrent.*;
+
+/**
+ * 鍏ㄥ眬寮傛鏌ヨ鎵ц鍣�
+ * 缁熶竴绠$悊绾跨▼姹狅紝鏀寔鍦ㄥ涓� Service 骞惰鎵ц SQL 鏌ヨ浠诲姟
+ */
+@Component
+public class AsyncQueryExecutor {
+
+ // 鍏ㄥ眬鍙鐢ㄧ嚎绋嬫睜
+ private final ExecutorService executorService = new ThreadPoolExecutor(
+ 16, // 鏍稿績绾跨▼鏁�
+ 32, // 鏈�澶х嚎绋嬫暟
+ 60L, TimeUnit.SECONDS, // 绌洪棽绾跨▼瀛樻椿鏃堕棿
+ new LinkedBlockingQueue<>(500), // 闃诲闃熷垪瀹归噺
+ new ThreadFactory() {
+ private final ThreadFactory defaultFactory = Executors.defaultThreadFactory();
+ private int counter = 1;
+
+ @Override
+ public Thread newThread(Runnable r) {
+ Thread thread = defaultFactory.newThread(r);
+ thread.setName("async-query-pool-" + counter++);
+ thread.setDaemon(true);
+ return thread;
+ }
+ },
+ new ThreadPoolExecutor.CallerRunsPolicy() // 闃熷垪婊℃椂涓荤嚎绋嬫墽琛岋紝闃叉涓换鍔�
+ );
+
+ /**
+ * 寮傛鎵ц浠诲姟
+ * @param supplier 瑕佹墽琛岀殑鍑芥暟锛堣繑鍥炰换鎰忕被鍨嬶級
+ * @param <T> 杩斿洖绫诲瀷
+ * @return CompletableFuture<T>
+ */
+ public <T> CompletableFuture<T> runAsync(SupplierWithException<T> supplier) {
+ return CompletableFuture.supplyAsync(() -> {
+ try {
+ return supplier.get();
+ } catch (Exception e) {
+ throw new RuntimeException("寮傛浠诲姟鎵ц寮傚父: " + e.getMessage(), e);
+ }
+ }, executorService);
+ }
+
+ /**
+ * 搴旂敤鍏抽棴鏃跺畨鍏ㄥ叧闂嚎绋嬫睜
+ */
+ @PreDestroy
+ public void shutdown() {
+ executorService.shutdown();
+ try {
+ if (!executorService.awaitTermination(60, TimeUnit.SECONDS)) {
+ executorService.shutdownNow();
+ }
+ } catch (InterruptedException e) {
+ executorService.shutdownNow();
+ Thread.currentThread().interrupt();
+ }
+ }
+
+ /**
+ * 鑷畾涔夊嚱鏁板紡鎺ュ彛锛屽厑璁告姏鍑哄彈妫�寮傚父
+ */
+ @FunctionalInterface
+ public interface SupplierWithException<T> {
+ T get() throws Exception;
+ }
+}
--
Gitblit v1.8.0