Browse Source

插入指标计算结果数据到大屏去掉事务,添加分布式锁

dev_shibei_match
jianjun 5 years ago
parent
commit
95b592c657
  1. 8
      epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/redis/RedisKeys.java
  2. 26
      epmet-module/data-statistical/data-statistical-server/src/main/java/com/epmet/service/evaluationindex/indexcoll/impl/FactIndexCollectServiceImpl.java

8
epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/redis/RedisKeys.java

@ -330,9 +330,17 @@ public class RedisKeys {
/**
* 获取计算标记的key前缀
*
* @return
*/
public static String getCustomerStatsCalKeyPrefix() {
return rootPrefix.concat("stats:calflag");
}
/**
* 插入大屏指标数据分布式锁 key
*/
public static String getScreenIndexDataLockKey() {
return rootPrefix.concat("stats:indexcal:lock");
}
}

26
epmet-module/data-statistical/data-statistical-server/src/main/java/com/epmet/service/evaluationindex/indexcoll/impl/FactIndexCollectServiceImpl.java

@ -3,6 +3,7 @@ package com.epmet.service.evaluationindex.indexcoll.impl;
import com.epmet.commons.dynamic.datasource.annotation.DataSource;
import com.epmet.commons.tools.constant.NumConstant;
import com.epmet.commons.tools.constant.StrConstant;
import com.epmet.commons.tools.redis.RedisKeys;
import com.epmet.commons.tools.utils.ConvertUtils;
import com.epmet.constant.DataSourceConstant;
import com.epmet.constant.OrgTypeConstant;
@ -27,6 +28,8 @@ import com.epmet.service.evaluationindex.indexcoll.FactIndexCollectService;
import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@ -38,6 +41,7 @@ import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;
/**
@ -84,6 +88,8 @@ public class FactIndexCollectServiceImpl implements FactIndexCollectService {
private ScreenCustomerDeptDao screenCustomerDeptDao;
@Autowired
private ScreenCustomerAgencyDao screenCustomerAgencyDao;
@Autowired
private RedissonClient redissonClient;
@Override
@Transactional(rollbackFor = Exception.class)
@ -199,11 +205,21 @@ public class FactIndexCollectServiceImpl implements FactIndexCollectService {
}
@Override
@Transactional(rollbackFor = Exception.class)
public void insertScreenIndexDataMonthlyAndYearly(String monthId, String customerId) {
if (NumConstant.SIX != monthId.length()) {
throw new RuntimeException("入参monthId格式不正确:monthId =" + monthId);
}
RLock lock = redissonClient.getLock(RedisKeys.getScreenIndexDataLockKey());
try {
AtomicInteger tryTimes = new AtomicInteger(0);
boolean lockFlag = false;
do {
lockFlag = lock.tryLock(1, 10, TimeUnit.MINUTES);
} while (!lockFlag && tryTimes.addAndGet(1) < 3);
if (!lockFlag) {
log.error("插入大屏指数方法获取锁失败");
return;
}
allParentIds.cleanUp();
// 根据网格类型,删除 指数-指数数据(每月数值)表中 匹配的数据
this.batchDelIndexDataMonthly(customerId, monthId, OrgTypeConstant.GRID);
@ -227,6 +243,14 @@ public class FactIndexCollectServiceImpl implements FactIndexCollectService {
// 根据年,汇总今年各项得到,计算平均值后 插入年表 screen_index_data_yearly
this.insertIndexDataYear(monthId, customerId);
allParentIds.invalidateAll();
} catch (InterruptedException inter) {
log.error("插入计算指标数据异常", inter);
} finally {
//判断是否是当前线程 持有锁
if (lock.isHeldByCurrentThread()) {
lock.unlock();
}
}
}
/**

Loading…
Cancel
Save