From 5f697ceb21081b53aa11312c0f2ae3ddcdba356a Mon Sep 17 00:00:00 2001 From: chen <2710907404@qq.com> Date: Mon, 25 Nov 2024 14:42:34 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=201.=E4=BD=BF=E7=94=A8?= =?UTF-8?q?=E8=87=AA=E5=AE=9A=E4=B9=89=E7=BA=BF=E7=A8=8B=E6=B1=A0=EF=BC=8C?= =?UTF-8?q?=E5=AF=B9feign=E8=B0=83=E7=94=A8=E7=9A=84=E6=8E=A5=E5=8F=A3?= =?UTF-8?q?=E8=BF=9B=E8=A1=8C=E5=BC=82=E6=AD=A5=E8=B0=83=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../entrustment/config/GlobalThreadPool.java | 52 +++++++++++++++++++ .../service/impl/EntrustmentServiceImpl.java | 25 +++++---- 2 files changed, 66 insertions(+), 11 deletions(-) create mode 100644 src/main/java/digital/laboratory/platform/entrustment/config/GlobalThreadPool.java diff --git a/src/main/java/digital/laboratory/platform/entrustment/config/GlobalThreadPool.java b/src/main/java/digital/laboratory/platform/entrustment/config/GlobalThreadPool.java new file mode 100644 index 0000000..03e0340 --- /dev/null +++ b/src/main/java/digital/laboratory/platform/entrustment/config/GlobalThreadPool.java @@ -0,0 +1,52 @@ +package digital.laboratory.platform.entrustment.config; + +import lombok.extern.slf4j.Slf4j; +import org.springframework.stereotype.Component; + +import javax.annotation.PreDestroy; +import java.util.concurrent.*; + +/** + * 定义全局线程池 + */ +@Slf4j +@Component +public class GlobalThreadPool { + + // 定义全局线程池,使用单列模式 + private static final ExecutorService THREAD_POOL = new ThreadPoolExecutor( + 10, // 核心线程数 + 50, // 最大线程数 + 60L, TimeUnit.SECONDS, // 空闲线程存活时间 + new LinkedBlockingQueue(100), // 任务队列 + Executors.defaultThreadFactory(), // 线程工厂 + new ThreadPoolExecutor.CallerRunsPolicy() // 拒绝策略 + ); + + // 私有化构造方法,防止外部实例化 + private GlobalThreadPool() {} + + // 获取全局线程池实列 + public static ExecutorService getInstance() { + return THREAD_POOL; + } + + // 关闭线程池 + public static void shutdown() { + THREAD_POOL.shutdown(); + } + + // 在应用关闭前执行 + @PreDestroy + public void destroy() { + log.info("Spring 应用关闭,正在关闭线程池..."); + shutdown(); + try { + if (!THREAD_POOL.awaitTermination(60, TimeUnit.SECONDS)) { + THREAD_POOL.shutdownNow(); + } + } catch (InterruptedException e) { + THREAD_POOL.shutdownNow(); + } + } +} diff --git a/src/main/java/digital/laboratory/platform/entrustment/service/impl/EntrustmentServiceImpl.java b/src/main/java/digital/laboratory/platform/entrustment/service/impl/EntrustmentServiceImpl.java index e82ca63..b068faa 100644 --- a/src/main/java/digital/laboratory/platform/entrustment/service/impl/EntrustmentServiceImpl.java +++ b/src/main/java/digital/laboratory/platform/entrustment/service/impl/EntrustmentServiceImpl.java @@ -31,6 +31,7 @@ import digital.laboratory.platform.common.feign.RemoteWord2PDFService; import digital.laboratory.platform.common.mybatis.security.service.DLPUser; import digital.laboratory.platform.common.oss.service.OssFile; import digital.laboratory.platform.common.security.util.SecurityUtils; +import digital.laboratory.platform.entrustment.config.GlobalThreadPool; import digital.laboratory.platform.entrustment.constant.EntrustMarkConstants; import digital.laboratory.platform.entrustment.convert.DrugLiteConvert; import digital.laboratory.platform.entrustment.dto.EntrustmentDTO; @@ -78,6 +79,7 @@ import java.time.LocalDate; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; import java.util.*; +import java.util.concurrent.CompletableFuture; import java.util.concurrent.ConcurrentHashMap; import java.util.function.Function; import java.util.function.Predicate; @@ -2964,7 +2966,7 @@ public class EntrustmentServiceImpl extends ServiceImpl imQuantity = remoteSewageJobService.getIMQuantity(dlpUser.getOrgId()); - if (imQuantity.getCode() == CommonConstants.SUCCESS) { - sewageJobIdentificationMaterialQuantity = imQuantity.getData(); - } else { - log.error("查询这个账户(机构)下面所送检且已受理的所有污水检材的数量 失败!失败原因:{}", imQuantity.getMsg()); - sewageJobIdentificationMaterialQuantity = 0; - } - markersVOS.add(new MarkersVO("污水送检检材", sewageJobIdentificationMaterialQuantity, "", "SewageJobGet")); - + CompletableFuture.runAsync(() -> { + Integer sewageJobIdentificationMaterialQuantity = 0; + R imQuantity = remoteSewageJobService.getIMQuantity(dlpUser.getOrgId()); + if (imQuantity.getCode() == CommonConstants.SUCCESS) { + sewageJobIdentificationMaterialQuantity = imQuantity.getData(); + } else { + log.error("查询这个账户(机构)下面所送检且已受理的所有污水检材的数量 失败!失败原因:{}", imQuantity.getMsg()); + sewageJobIdentificationMaterialQuantity = 0; + } + markersVOS.add(new MarkersVO("污水送检检材", sewageJobIdentificationMaterialQuantity, "", "SewageJobGet")); + }, GlobalThreadPool.getInstance()); try { R sampleQuantity = remoteHairJobService.getSampleQuantity(orgId, 2);