Sfoglia il codice sorgente

1.生成文书异步执行保存下载接口

赵越越 1 settimana fa
parent
commit
6ef9484e3d

+ 73 - 0
assistMg/src/main/java/com/hotent/common/MyAsyncExecutor.java

@@ -0,0 +1,73 @@
+package com.hotent.common;/**
+ * @program: cbjs-mvue-master
+ * @description:
+ * @author: zhao yue yue
+ * @create: 2025-12-16 09:14
+ */
+
+import org.springframework.stereotype.Component;
+import javax.annotation.PostConstruct;
+import javax.annotation.PreDestroy;
+import java.util.concurrent.*;
+
+/**
+ *@author: zhao yue yue
+ *@create: 2025-12-16 09:14
+ */
+@Component
+public class MyAsyncExecutor {
+    private ThreadPoolExecutor threadPool;
+
+    @PostConstruct
+    public void init() {
+        // 创建线程池
+        threadPool = new ThreadPoolExecutor(
+                5,      // 核心线程数
+                20,     // 最大线程数
+                60L,    // 空闲线程存活时间(秒)
+                TimeUnit.SECONDS,
+                new LinkedBlockingQueue<>(100),  // 任务队列
+                r -> {
+                    Thread t = new Thread(r);
+                    t.setName("async-thread-" + System.currentTimeMillis());
+                    t.setDaemon(false);  // 设置为非守护线程
+                    return t;
+                },
+                new ThreadPoolExecutor.CallerRunsPolicy()  // 拒绝策略:调用者线程执行
+        );
+    }
+
+    /**
+     * 执行异步任务(最常用)
+     */
+    public void execute(Runnable task) {
+        threadPool.execute(task);
+    }
+
+    /**
+     * 执行异步任务(带返回值)
+     */
+    public <T> Future<T> submit(Callable<T> task) {
+        return threadPool.submit(task);
+    }
+
+    /**
+     * 优雅关闭
+     */
+    @PreDestroy
+    public void shutdown() {
+        if (threadPool != null) {
+            threadPool.shutdown();  // 不再接受新任务
+            try {
+                // 等待现有任务完成,最多等待30秒
+                if (!threadPool.awaitTermination(30, TimeUnit.SECONDS)) {
+                    threadPool.shutdownNow();  // 取消尚未开始的任务
+                }
+            } catch (InterruptedException e) {
+                threadPool.shutdownNow();
+                Thread.currentThread().interrupt();
+            }
+            System.out.println("异步执行器已安全关闭");
+        }
+    }
+}

+ 7 - 6
assistMg/src/main/java/com/hotent/project/manager/impl/CostProjectDocumentManagerImpl.java

@@ -15,6 +15,7 @@ import com.hotent.baseInfo.model.AuditedUnit;
 import com.hotent.baseInfo.model.CostDocumentTemplate;
 import com.hotent.baseInfo.model.CostDocumentTemplateFile;
 import com.hotent.baseInfo.model.CostDocumentWh;
+import com.hotent.common.MyAsyncExecutor;
 import com.hotent.config.EipConfig;
 import com.hotent.constant.BaseConstant;
 import com.hotent.project.manager.*;
@@ -38,7 +39,6 @@ import org.apache.poi.xwpf.usermodel.XWPFDocument;
 import org.apache.tools.ant.util.DateUtils;
 import org.springframework.beans.BeanUtils;
 import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.scheduling.annotation.Async;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
 
@@ -64,7 +64,8 @@ import static org.apache.tools.ant.util.DateUtils.ISO8601_DATE_PATTERN;
 @Service
 public class CostProjectDocumentManagerImpl extends BaseManagerImpl<CostProjectDocumentDao, CostProjectDocument> implements CostProjectDocumentManager {
 
-
+    @Autowired
+    private MyAsyncExecutor asyncExecutor;
 
     @Autowired
     private CostProjectDocumentFileManager costProjectDocumentFileManager;
@@ -389,7 +390,7 @@ public class CostProjectDocumentManagerImpl extends BaseManagerImpl<CostProjectD
             });
             //asyncService.executeAsync(projectDocument.getId());
             // 异步执行耗时任务
-            /*asyncExecutor.execute(() -> {
+            asyncExecutor.execute(() -> {
                 // 这里是你的业务逻辑
                 try {
                     System.out.println("开始处理数据...");
@@ -399,7 +400,7 @@ public class CostProjectDocumentManagerImpl extends BaseManagerImpl<CostProjectD
                 } catch (InterruptedException e) {
                     Thread.currentThread().interrupt();
                 }
-            });*/
+            });
             costProjectDocumentFileManager.saveBatch(arrayList);
         }
 
@@ -408,7 +409,7 @@ public class CostProjectDocumentManagerImpl extends BaseManagerImpl<CostProjectD
         //asyncService.executeAsync(costProjectDocument.getId());
 
         // 异步执行耗时任务
-        /*asyncExecutor.execute(() -> {
+        asyncExecutor.execute(() -> {
             // 这里是你的业务逻辑
             try {
                 System.out.println("开始处理数据...");
@@ -418,7 +419,7 @@ public class CostProjectDocumentManagerImpl extends BaseManagerImpl<CostProjectD
             } catch (InterruptedException e) {
                 Thread.currentThread().interrupt();
             }
-        });*/
+        });
         return costProjectDocument.getId();
     }