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