From b0a4de74b47e2626f51eb4a844db3e62f6a4bf9d Mon Sep 17 00:00:00 2001
From: guoyujie <guoyujie@ng.com>
Date: 星期四, 18 十二月 2025 10:52:48 +0800
Subject: [PATCH] Merge branch 'master' of http://10.153.19.25:10101/r/ERP_override

---
 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