소스 검색

增加lock 逻辑

gaoyu 3 년 전
부모
커밋
93c3274f4c
2개의 변경된 파일66개의 추가작업 그리고 28개의 파일을 삭제
  1. 64 27
      src/main/java/com/persagy/cache/AlarmRedisCache.java
  2. 2 1
      src/main/java/com/persagy/service/impl/AlarmHandleServiceImpl.java

+ 64 - 27
src/main/java/com/persagy/cache/AlarmRedisCache.java

@@ -2,6 +2,7 @@ package com.persagy.cache;
 
 import java.util.Objects;
 import java.util.Optional;
+import java.util.concurrent.TimeUnit;
 
 import javax.annotation.Resource;
 
@@ -79,31 +80,67 @@ public class AlarmRedisCache implements RedisCache {
 		return alarmCondition;
 	}
 
-    public ObjConditionInfo getAlarmConditionsByObjId(String meterId) {
-        try {
-            String str = alarmRedisTemplate.opsForHash().get(RedisConstant.METER_ID, meterId).toString();
-            if (StringUtils.isNotBlank(str)) {
-                JSONObject object = JSONObject.parseObject(str);
-                ObjConditionInfo objConditionInfo = JSON.toJavaObject(object, ObjConditionInfo.class);
-                return objConditionInfo;
-            }
-        } catch (Exception e) {
-            log.warn("从redis获取设备的报警条件信息失败" + e);
-        }
-        return null;
-    }
-
-    public AlarmCondition getAlarmCondition(String conditionId) {
-        try {
-            String str = alarmRedisTemplate.opsForHash().get(RedisConstant.ALARMCONDITION_ID, conditionId).toString();
-            if (StringUtils.isNotBlank(str)) {
-                JSONObject object = JSONObject.parseObject(str);
-                AlarmCondition alarmCondition = JSON.toJavaObject(object, AlarmCondition.class);
-                return alarmCondition;
-            }
-        } catch (Exception e) {
-            log.warn("从redis获取报警条件失败" + e);
-        }
-        return null;
-    }
+	public ObjConditionInfo getAlarmConditionsByObjId(String meterId) {
+		try {
+			String str = alarmRedisTemplate.opsForHash().get(RedisConstant.METER_ID, meterId).toString();
+			if (StringUtils.isNotBlank(str)) {
+				JSONObject object = JSONObject.parseObject(str);
+				ObjConditionInfo objConditionInfo = JSON.toJavaObject(object, ObjConditionInfo.class);
+				return objConditionInfo;
+			}
+		} catch (Exception e) {
+			log.warn("从redis获取设备的报警条件信息失败" + e);
+		}
+		return null;
+	}
+
+	public AlarmCondition getAlarmCondition(String conditionId) {
+		try {
+			String str = alarmRedisTemplate.opsForHash().get(RedisConstant.ALARMCONDITION_ID, conditionId).toString();
+			if (StringUtils.isNotBlank(str)) {
+				JSONObject object = JSONObject.parseObject(str);
+				AlarmCondition alarmCondition = JSON.toJavaObject(object, AlarmCondition.class);
+				return alarmCondition;
+			}
+		} catch (Exception e) {
+			log.warn("从redis获取报警条件失败" + e);
+		}
+		return null;
+	}
+
+	/**
+	 * 释放锁
+	 * 
+	 * @param key
+	 */
+	public void releaseLock(String key) {
+		alarmRedisTemplate.delete(key);
+	}
+
+	/**
+	 * 加锁
+	 * 
+	 * @param key
+	 * @param value
+	 * @param timeout 过期时间
+	 */
+	public boolean lock(String key, String value, Integer timeout) {
+		boolean bool;
+		while (true) {
+			Boolean b = alarmRedisTemplate.opsForValue().setIfAbsent(key, value, timeout, TimeUnit.SECONDS);
+			if (b) {
+				bool = b;
+				break;
+			} else {
+				try {
+					Thread.sleep(100);
+				} catch (InterruptedException e) {
+					Thread.currentThread().interrupt();
+				}
+			}
+		}
+		return bool;
+
+	}
+	
 }

+ 2 - 1
src/main/java/com/persagy/service/impl/AlarmHandleServiceImpl.java

@@ -417,6 +417,7 @@ public class AlarmHandleServiceImpl implements AlarmHandleService {
 			log.debug("报警恢复持续时间大于设定时间:[{}]>[{}]", alarmSuspendLastTime, uphold);
 			ZktAlarmRecordDO alarmRecordDO = getZktAlarmRecordDOWhenAlarmSuspend(defineId, dataTime, paramMap);
 			String alarmId = alarmRecordDO.getAlarmId();
+			alarmRedisCache.lock(alarmId, alarmId, 10);
 			// 发送报警恢复消息
 			sendAlarmRecoverMessage(defineId, alarmSuspendStartTime, paramMap, alarmId);
 			// 报警恢复后,从缓存中移除报警id
@@ -428,7 +429,7 @@ public class AlarmHandleServiceImpl implements AlarmHandleService {
 		}
 		// 更新报警定义缓存
 		alarmRedisCache.setAlarmState(defineId, alarmConditionState);
-
+		alarmRedisCache.releaseLock(alarmDefine.getObjId() + "-" + alarmDefine.getItemCode());
 	}
 
 	/**