From c426ba5b0878733082216744c83907c24732c936 Mon Sep 17 00:00:00 2001 From: wxz Date: Thu, 10 Sep 2020 17:48:57 +0800 Subject: [PATCH] =?UTF-8?q?=E5=BC=82=E6=AD=A5=E8=AE=A1=E7=AE=97=EF=BC=8C?= =?UTF-8?q?=E6=A0=B9=E6=8D=AE=E5=AE=A2=E6=88=B7=E5=8A=A0=E9=94=81=EF=BC=8C?= =?UTF-8?q?=E5=90=8C=E4=B8=80=E4=B8=AA=E5=AE=A2=E6=88=B7=E5=90=8C=E6=97=B6?= =?UTF-8?q?=E5=8F=AA=E8=83=BD=E6=9C=89=E4=B8=80=E4=B8=AA=E7=BA=BF=E7=A8=8B?= =?UTF-8?q?=E6=89=A7=E8=A1=8C=E8=AE=A1=E7=AE=97=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../epmet/commons/tools/redis/RedisKeys.java | 11 +++++ .../controller/IndexCalculateController.java | 41 +++++++++++++++---- 2 files changed, 45 insertions(+), 7 deletions(-) diff --git a/epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/redis/RedisKeys.java b/epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/redis/RedisKeys.java index 0d623e2010..fc5c0c7a31 100644 --- a/epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/redis/RedisKeys.java +++ b/epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/redis/RedisKeys.java @@ -305,4 +305,15 @@ public class RedisKeys { public static String getIndexCodeFieldReKey() { return rootPrefix.concat("data:index:indexcode:field"); } + + /** + * 客户统计的计算标记。 + * 0:未在计算 + * 1:正在计算 + * @param customerId + * @return + */ + public static String getCustomerStatsCalFlag(String customerId) { + return String.format(rootPrefix+"stats:calflag:%s", customerId); + } } diff --git a/epmet-module/data-statistical/data-statistical-server/src/main/java/com/epmet/controller/IndexCalculateController.java b/epmet-module/data-statistical/data-statistical-server/src/main/java/com/epmet/controller/IndexCalculateController.java index e1e2c85d78..887482d1a4 100644 --- a/epmet-module/data-statistical/data-statistical-server/src/main/java/com/epmet/controller/IndexCalculateController.java +++ b/epmet-module/data-statistical/data-statistical-server/src/main/java/com/epmet/controller/IndexCalculateController.java @@ -2,6 +2,8 @@ package com.epmet.controller; import com.epmet.commons.extappauth.annotation.ExternalAppRequestAuth; import com.epmet.commons.extappauth.bean.ExternalAppRequestParam; +import com.epmet.commons.tools.redis.RedisKeys; +import com.epmet.commons.tools.redis.RedisUtils; import com.epmet.commons.tools.utils.DateUtils; import com.epmet.commons.tools.utils.Result; import com.epmet.dto.indexcal.CalculateCommonFormDTO; @@ -9,6 +11,7 @@ import com.epmet.service.evaluationindex.indexcal.CpcIndexCalculateService; import com.epmet.service.evaluationindex.indexcal.IndexCalculateService; import com.epmet.util.DimIdGenerator; import com.google.common.util.concurrent.ThreadFactoryBuilder; +import com.sun.org.apache.xpath.internal.operations.Bool; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Autowired; @@ -43,6 +46,12 @@ public class IndexCalculateController { @Autowired private CpcIndexCalculateService cpcIndexCalculateService; + @Autowired + private RedisUtils redisUtils; + + // 计算同步锁 + private Object statsCalLock = new Object(); + /** * 按照客户计算所有指标(按照月份) * @@ -54,14 +63,32 @@ public class IndexCalculateController { @ExternalAppRequestAuth @PostMapping("all") public Result indexCalculate(ExternalAppRequestParam externalAppRequestParam, @RequestBody CalculateCommonFormDTO formDTO) { - singleThreadPool.execute(() -> { - formDTO.setCustomerId(externalAppRequestParam.getCustomerId()); - long start = System.currentTimeMillis(); - Boolean aBoolean = indexCalculateService.indexCalculate(formDTO); - if (aBoolean) { - log.error("全部指标计算完成,总耗时:{}秒", (System.currentTimeMillis() - start) / 1000); + String customerId = externalAppRequestParam.getCustomerId(); + Boolean executing = (Boolean) redisUtils.get(RedisKeys.getCustomerStatsCalFlag(customerId)); + if (executing == null || !executing) { + synchronized (statsCalLock) { + Boolean executing2 = (Boolean) redisUtils.get(RedisKeys.getCustomerStatsCalFlag(customerId)); + if (executing2 != null && executing2) { + log.error(String.format("该客户正在执行计算,请勿重复提交计算请求。", customerId)); + return new Result().ok(false); + } + singleThreadPool.execute(() -> { + formDTO.setCustomerId(customerId); + long start = System.currentTimeMillis(); + Boolean aBoolean = indexCalculateService.indexCalculate(formDTO); + if (aBoolean) { + log.error("全部指标计算完成,总耗时:{}秒", (System.currentTimeMillis() - start) / 1000); + } + redisUtils.set(RedisKeys.getCustomerStatsCalFlag(customerId), false); + }); + redisUtils.set(RedisKeys.getCustomerStatsCalFlag(customerId), true); } - }); + } else { + log.error(String.format("该客户正在执行计算,请勿重复提交计算请求。", customerId)); + return new Result().ok(false); + } + + return new Result().ok(true); }