Browse Source

Merge branch 'wxz_threadpool' of http://git.elinkit.com.cn:7070/r/epmet-cloud into develop

dev
wangxianzhang 4 years ago
parent
commit
09075d3a4b
  1. 55
      epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/feign/ResultDataResolver.java
  2. 62
      epmet-module/data-statistical/data-statistical-server/src/main/java/com/epmet/service/evaluationindex/extract/dataToIndex/impl/IndexOriginExtractServiceImpl.java
  3. 14
      epmet-module/data-statistical/data-statistical-server/src/main/java/com/epmet/service/evaluationindex/extract/toscreen/impl/ScreenExtractServiceImpl.java

55
epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/feign/ResultDataResolver.java

@ -1,9 +1,12 @@
package com.epmet.commons.tools.feign;
import com.epmet.commons.tools.exception.EpmetErrorCode;
import com.epmet.commons.tools.exception.EpmetException;
import com.epmet.commons.tools.exception.RenException;
import com.epmet.commons.tools.utils.Result;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Feign请求结果解析器
@ -12,30 +15,30 @@ public interface ResultDataResolver {
/**
* @Description 获取Result种的data如果失败(返回result为null或者result.success为false)那么返回null
* @return
* @return data数据
* @author wxz
* @date 2021.06.07 22:45
*/
//default <R> R tryGetResultData(Result<R> result, String targetServiceName) {
// Logger logger = LoggerFactory.getLogger(ResultDataResolver.class);
// if (result == null) {
// logger.error("调用{}服务发生错误,返回Result为null", targetServiceName);
// return null;
// }
// if (!result.success()) {
// logger.error("调用{}服务发生错误,错误信息:{}", targetServiceName, result.getInternalMsg());
// return null;
// }
// return result.getData();
//}
default <R> R getResultDataOrReturnNull(Result<R> result, String targetServiceName) {
Logger logger = LoggerFactory.getLogger(ResultDataResolver.class);
if (result == null) {
logger.error("调用{}服务发生错误,返回Result为null", targetServiceName);
return null;
}
if (!result.success()) {
logger.error("调用{}服务发生错误,内部信息:{},错误信息:{}", targetServiceName, result.getInternalMsg(), result.getMsg());
return null;
}
return result.getData();
}
/**
* @Description
* @return
* @Description 解析Result中的结果如果请求上游服务返回的结果不成功则抛出异常
* @return data数据
* @param targetServiceName 目标service名称
* @param errorCode 错误码可以为空为空则使用上游服务抛出的错误码
* @param errorInternalMsg 内部错误信息可以为空为空则使用上游服务抛出的异常信息
* @param showMsg 展示给前端程序的错误信息可以为空为空则根据errorCode给定错误msg信息
* @param errorCode 错误码可以为空为空则使用8000
* @param errorInternalMsg 内部错误信息可以为空为空则internalMsg=""
* @param showMsg 展示给前端程序的错误信息可以为空为空则showMsg=""
* @author wxz
* @date 2021.06.07 22:45
*/
@ -43,11 +46,25 @@ public interface ResultDataResolver {
if (result == null) {
throw new RenException(EpmetErrorCode.SERVER_ERROR.getCode(), "调用{}服务发生错误,返回Result为null", targetServiceName);
}
if (!result.success()) {
// 考虑到:上游服务抛出的异常代码和错误消息并不一定适用于当前服务,并且上有服务的错误消息弹出之后可能给用户造成困扰,
// 因此,当前服务抛出异常的时候,不再继承上游服务返回的错误码和错误消息
/*if (!result.success()) {
Integer finalErrorCode = errorCode == null ? result.getCode() : errorCode;
String finalErrorInternalMsg = StringUtils.isBlank(errorInternalMsg) ? result.getInternalMsg() : errorInternalMsg;
throw new RenException(finalErrorCode, finalErrorInternalMsg, showMsg, RenException.MessageMode.CODE_INTERNAL_EXTERNAL);
}*/
if (!result.success()) {
// 如果不通过参数指定code,则默认使用8000服务器开小差
Integer finalErrorCode = errorCode == null ? EpmetErrorCode.SERVER_ERROR.getCode() : errorCode;
String finalErrorInternalMsg = StringUtils.isBlank(errorInternalMsg) ? "" : errorInternalMsg;
String finalShowMsg = StringUtils.isBlank(showMsg) ? "" : showMsg;
throw new EpmetException(finalErrorCode, finalErrorInternalMsg, finalShowMsg);
}
return result.getData();
}

62
epmet-module/data-statistical/data-statistical-server/src/main/java/com/epmet/service/evaluationindex/extract/dataToIndex/impl/IndexOriginExtractServiceImpl.java

@ -4,11 +4,14 @@ import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson.JSON;
import com.epmet.commons.tools.constant.CustomerIdConstant;
import com.epmet.commons.tools.constant.NumConstant;
import com.epmet.commons.tools.enums.EnvEnum;
import com.epmet.commons.tools.utils.HttpClientManager;
import com.epmet.commons.tools.validator.ValidatorUtils;
import com.epmet.dto.extract.form.ExtractIndexFormDTO;
import com.epmet.dto.indexcal.CalculateCommonFormDTO;
import com.epmet.service.evaluationindex.extract.dataToIndex.*;
import com.epmet.service.evaluationindex.indexcal.IndexCalculateService;
import com.epmet.service.stats.DimCustomerService;
import com.google.common.util.concurrent.ThreadFactoryBuilder;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
@ -18,7 +21,8 @@ import org.springframework.util.CollectionUtils;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.*;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
/**
* desc指标原始数据抽取服务实现类
@ -26,12 +30,13 @@ import java.util.concurrent.*;
@Slf4j
@Service
public class IndexOriginExtractServiceImpl implements IndexOriginExtractService {
ThreadFactory namedThreadFactory = new ThreadFactoryBuilder()
.setNameFormat("indexOriginExtract-pool-%d").build();
/*ThreadFactory namedThreadFactory = new ThreadFactoryBuilder()
.setNameFormat("indexOriginExtractAndCal-pool-%d").build();
ExecutorService threadPool = new ThreadPoolExecutor(1, 1,
10L, TimeUnit.MINUTES,
new LinkedBlockingQueue<>(500), namedThreadFactory, new ThreadPoolExecutor.CallerRunsPolicy());
new LinkedBlockingQueue<>(500), namedThreadFactory, new ThreadPoolExecutor.CallerRunsPolicy());*/
@Autowired
private ExecutorService executorService;
@Autowired
private CalCpcIndexService calCpcIndexService;
@Autowired
@ -46,6 +51,9 @@ public class IndexOriginExtractServiceImpl implements IndexOriginExtractService
private IndexCollDistrictService indexCollDistrictService;
@Autowired
private IndexCollDistrictDepartmentService indexCollDistrictDepartmentService;
@Autowired
private IndexCalculateService indexCalculateService;
/**
* desc从统计库对象抽取指标数据
*
@ -91,11 +99,11 @@ public class IndexOriginExtractServiceImpl implements IndexOriginExtractService
private void submitJob(ExtractIndexFormDTO param) {
CountDownLatch countDownLatch = new CountDownLatch(2);
CountDownLatch countDownLatch = new CountDownLatch(NumConstant.SIX);
long start = System.currentTimeMillis();
final String customerId = param.getCustomerId();
final String monthId = param.getMonthId();
threadPool.submit(() -> {
executorService.submit(() -> {
try {
long startCpc = System.currentTimeMillis();
calCpcIndexService.calCpcPartyAbility(customerId, monthId);
@ -106,7 +114,7 @@ public class IndexOriginExtractServiceImpl implements IndexOriginExtractService
countDownLatch.countDown();
}
});
threadPool.submit(() -> {
executorService.submit(() -> {
try {
long startGridGovern = System.currentTimeMillis();
calGridIndexService.calGridIndexGovernAbility(customerId, monthId);
@ -128,37 +136,40 @@ public class IndexOriginExtractServiceImpl implements IndexOriginExtractService
} catch (Exception e) {
log.error("抽取【网格服务能力数据】发生异常,参数:" + JSON.toJSONString(param), e);
}
countDownLatch.countDown();
});
executorService.submit(() -> {
try {
//dimAgency
long startCommunity = System.currentTimeMillis();
indexCollCommunityService.saveCommunityAbility(customerId, monthId);
log.error("社区相关-三大能力执行完毕======总耗时:{}ms,customerId:{}", System.currentTimeMillis() - startCommunity, param.getCustomerId());
} catch (Exception e) {
log.error("抽取【社区治理能力-社区党建能力-服务能力】发生异常,参数:" + JSON.toJSONString(param), e);
}
countDownLatch.countDown();
});
executorService.submit(() -> {
try {
//dimAgency
long startStreet = System.currentTimeMillis();
indexCollStreetService.saveStreetAbility(customerId, monthId);
log.error("街道相关-三大能力执行完毕======总耗时:{}ms,customerId:{}", System.currentTimeMillis() - startStreet, param.getCustomerId());
} catch (Exception e) {
log.error("抽取【街道治理能力-街道党建能力-服务能力】发生异常,参数:" + JSON.toJSONString(param), e);
}
countDownLatch.countDown();
});
executorService.submit(() -> {
try {
//dimAgency
long startDept = System.currentTimeMillis();
indexCollDistrictDepartmentService.saveDepartmentAbility(customerId, monthId);
log.error("区直部门相关-治理能力执行完毕======总耗时:{}ms,customerId:{}", System.currentTimeMillis() - startDept, param.getCustomerId());
} catch (Exception e) {
log.error("抽取【区直部门治理能力】发生异常,参数:" + JSON.toJSONString(param), e);
}
countDownLatch.countDown();
});
executorService.submit(() -> {
try {
//dimAgency
long startDistrict = System.currentTimeMillis();
indexCollDistrictService.saveDistrictAbility(customerId, monthId);
log.error("全区相关-三大能力执行完毕======总耗时:{}ms,customerId:{}", System.currentTimeMillis() - startDistrict, param.getCustomerId());
@ -169,10 +180,23 @@ public class IndexOriginExtractServiceImpl implements IndexOriginExtractService
});
try {
//等前面都执行完 再执行计算指标得分
countDownLatch.await();
executorService.submit(() -> {
long startT = System.currentTimeMillis();
CalculateCommonFormDTO formDTO1 = new CalculateCommonFormDTO();
try {
formDTO1.setMonthId(monthId);
formDTO1.setCustomerId(customerId);
Boolean aBoolean = indexCalculateService.indexCalculate(formDTO1);
HttpClientManager.getInstance().sendAlarmMsg(EnvEnum.getCurrentEnv().getName() + "客户Id:" + customerId + ";monthId:" + monthId + ",calculateAll全部指标计算完成,是否成功:" + aBoolean + ",总耗时:" + (System.currentTimeMillis() - startT) / 1000 + "秒");
} catch (Exception e) {
log.error("extractMonthly 计算分数异常,参数:{}", JSON.toJSONString(formDTO1));
}
});
} catch (InterruptedException e) {
log.error("indexOriginExtractAll countDownLatch exception", e);
}
log.error("indexOriginExtractAll执行完毕======总耗时:{}ms,customerId:{}",System.currentTimeMillis()-start, param.getCustomerId());
log.info("indexOriginExtractAll及指标计算执行完毕======总耗时:{}ms,customerId:{}", System.currentTimeMillis() - start, param.getCustomerId());
}
}

14
epmet-module/data-statistical/data-statistical-server/src/main/java/com/epmet/service/evaluationindex/extract/toscreen/impl/ScreenExtractServiceImpl.java

@ -474,20 +474,6 @@ public class ScreenExtractServiceImpl implements ScreenExtractService {
log.info("===== extractMonthly method end not contains shi bei:{}======", customerId);
return;
}
//此方法保持在最后即可 计算指标分数 todo 优化 手动创建线程池 控制任务数量
ExecutorService pool = Executors.newSingleThreadExecutor();
pool.submit(() -> {
long start = System.currentTimeMillis();
CalculateCommonFormDTO formDTO1 = new CalculateCommonFormDTO();
try {
formDTO1.setMonthId(monthId);
formDTO1.setCustomerId(customerId);
Boolean aBoolean = indexCalculateService.indexCalculate(formDTO1);
HttpClientManager.getInstance().sendAlarmMsg(EnvEnum.getCurrentEnv().getName() + "客户Id:" + formDTO.getCustomerId() + ";monthId:" + formDTO1.getMonthId() + ",calculateAll全部指标计算完成,是否成功:" + aBoolean + ",总耗时:" + (System.currentTimeMillis() - start) / 1000 + "秒");
} catch (Exception e) {
log.error("extractMonthly 计算分数异常,参数:{}", JSON.toJSONString(formDTO1));
}
});
log.info("===== extractMonthly method end ======");
}
}

Loading…
Cancel
Save