From 6acf9620606ca706c2b0b1b7cc4eaf7fa9fc7824 Mon Sep 17 00:00:00 2001 From: wxz Date: Fri, 11 Sep 2020 15:29:45 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E7=BB=88=E6=AD=A2=E8=AE=A1?= =?UTF-8?q?=E7=AE=97=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../dto/indexcal/CalculateCommonFormDTO.java | 6 +++++ .../controller/IndexCalculateController.java | 26 ++++++++++++++++++- 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/epmet-module/data-statistical/data-statistical-client/src/main/java/com/epmet/dto/indexcal/CalculateCommonFormDTO.java b/epmet-module/data-statistical/data-statistical-client/src/main/java/com/epmet/dto/indexcal/CalculateCommonFormDTO.java index 187f69c960..cfdc9f2ec9 100644 --- a/epmet-module/data-statistical/data-statistical-client/src/main/java/com/epmet/dto/indexcal/CalculateCommonFormDTO.java +++ b/epmet-module/data-statistical/data-statistical-client/src/main/java/com/epmet/dto/indexcal/CalculateCommonFormDTO.java @@ -1,7 +1,9 @@ package com.epmet.dto.indexcal; +import com.epmet.commons.tools.validator.group.CustomerClientShowGroup; import lombok.Data; +import javax.validation.constraints.NotBlank; import java.io.Serializable; /** @@ -13,6 +15,9 @@ import java.io.Serializable; @Data public class CalculateCommonFormDTO implements Serializable { private static final long serialVersionUID = -5689788391963427717L; + + public interface CancelCalculateGroup extends CustomerClientShowGroup {} + /** * 月份id: yyyyMM */ @@ -21,6 +26,7 @@ public class CalculateCommonFormDTO implements Serializable { /** * 客户id */ + @NotBlank(message = "客户id不能为空", groups = { CancelCalculateGroup.class }) private String customerId; public CalculateCommonFormDTO() { 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 0acdf28890..596a1f17ad 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 @@ -7,6 +7,7 @@ import com.epmet.commons.tools.redis.RedisUtils; import com.epmet.commons.tools.utils.DateUtils; import com.epmet.commons.tools.utils.HttpClientManager; import com.epmet.commons.tools.utils.Result; +import com.epmet.commons.tools.validator.ValidatorUtils; import com.epmet.dto.indexcal.CalculateCommonFormDTO; import com.epmet.service.evaluationindex.indexcal.CpcIndexCalculateService; import com.epmet.service.evaluationindex.indexcal.IndexCalculateService; @@ -21,6 +22,8 @@ import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import java.util.Date; +import java.util.HashMap; +import java.util.Map; import java.util.concurrent.*; /** @@ -52,6 +55,8 @@ public class IndexCalculateController { // 计算同步锁 private Object statsCalLock = new Object(); + private Map futureMap = new HashMap<>(); + /** * 按照客户计算所有指标(按照月份) * @@ -72,7 +77,7 @@ public class IndexCalculateController { log.error(String.format("该客户正在执行计算,请勿重复提交计算请求。", customerId)); return new Result().ok(false); } - singleThreadPool.execute(() -> { + Future future = singleThreadPool.submit(() -> { formDTO.setCustomerId(customerId); long start = System.currentTimeMillis(); Boolean aBoolean = indexCalculateService.indexCalculate(formDTO); @@ -80,7 +85,9 @@ public class IndexCalculateController { log.error("客户Id:{},全部指标计算完成,总耗时:{}秒", customerId, (System.currentTimeMillis() - start) / 1000); } redisUtils.set(RedisKeys.getCustomerStatsCalFlag(customerId), false); + futureMap.remove(customerId); }); + futureMap.put(customerId, future); redisUtils.set(RedisKeys.getCustomerStatsCalFlag(customerId), true); } } else { @@ -92,6 +99,23 @@ public class IndexCalculateController { return new Result().ok(true); } + /** + * 终止计算 + * @param form + * @return + */ + @PostMapping("stopcalculate") + public Result stopcalculate(@RequestBody CalculateCommonFormDTO form) { + ValidatorUtils.validateEntity(form); + + String customerId = form.getCustomerId(); + Future future = this.futureMap.get(customerId); + if (future != null && !future.isCancelled()) { + future.cancel(true); + } + return new Result(); + } + @PostMapping("reAll") public Result calculateAll(@RequestBody CalculateCommonFormDTO formDTO) { long start = System.currentTimeMillis();