Browse Source

解决上一版本清空缓存时造成bug的问题修复。

Jay Lee 6 năm trước cách đây
mục cha
commit
bb6194cc86

+ 99 - 90
collect/src/main/java/com/saga/thread/common/CheckThread.java

@@ -152,124 +152,133 @@ public class CheckThread extends Thread {
 
 	private void virtual_project_status_up_Check() {
 		// TODO Auto-generated method stub
-		for (Entry<String, List<String>> ps : Constant.ps_ProjectList.entrySet()) {
-			String project = ps.getKey();
-			List<String> l = ps.getValue();
-			String status = this.gatherStatus(l);
-			try {
-				if ("conn".equalsIgnoreCase(status)) {
-					HBaseUtil.Insert_ps_virtual_project_status(Constant.agent, Constant.BuildingDB.get(project),
-							project, status, Constant.format(new Date()), "");
-				}
-
-				if (
-				// 首次
-				!this.projectStatus.containsKey(project) || null == this.projectStatus.get(project) ||
-				// 已有记录
-						!status.equalsIgnoreCase(this.projectStatus.get(project))) {
-
-					HBaseUtil.Insert_ps_virtual_project_log(Constant.agent, Constant.BuildingDB.get(project), project,
-							Constant.format(new Date()), status);
-					if ("disconn".equalsIgnoreCase(status)) {
+		synchronized (Constant.ps_ProjectList) {
+			for (Entry<String, List<String>> ps : Constant.ps_ProjectList.entrySet()) {
+				String project = ps.getKey();
+				List<String> l = ps.getValue();
+				String status = this.gatherStatus(l);
+				try {
+					if ("conn".equalsIgnoreCase(status)) {
 						HBaseUtil.Insert_ps_virtual_project_status(Constant.agent, Constant.BuildingDB.get(project),
 								project, status, Constant.format(new Date()), "");
 					}
+
+					if (
+					// 首次
+					!this.projectStatus.containsKey(project) || null == this.projectStatus.get(project) ||
+					// 已有记录
+							!status.equalsIgnoreCase(this.projectStatus.get(project))) {
+
+						HBaseUtil.Insert_ps_virtual_project_log(Constant.agent, Constant.BuildingDB.get(project),
+								project, Constant.format(new Date()), status);
+						if ("disconn".equalsIgnoreCase(status)) {
+							HBaseUtil.Insert_ps_virtual_project_status(Constant.agent, Constant.BuildingDB.get(project),
+									project, status, Constant.format(new Date()), "");
+						}
+					}
+				} catch (Exception e) {
+					// TODO Auto-generated catch block
+					e.printStackTrace();
 				}
-			} catch (Exception e) {
-				// TODO Auto-generated catch block
-				e.printStackTrace();
+				this.projectStatus.put(project, status);
 			}
-			this.projectStatus.put(project, status);
 		}
+
 	}
 
 	private void virtual_meter_status_up_Check() {
 		// TODO Auto-generated method stub
-		for (Entry<String, List<String>> ms : Constant.ps_MeterList.entrySet()) {
-			String pm = ms.getKey();
-			List<String> l = ms.getValue();
-			String status = this.gatherStatus(l);
-			String[] ml = pm.split("-");
-			String project = ml[0];
-			String meter = ml[1];
-			try {
-				if ("conn".equalsIgnoreCase(status)) {
-					HBaseUtil.Insert_ps_virtual_meter_status(Constant.agent, Constant.BuildingDB.get(project), project,
-							meter, "up", status, Constant.format(new Date()), "");
-				}
-
-				if (
-				// 首次
-				!this.meterStatus.containsKey(pm) || null == this.meterStatus.get(pm) ||
-				// 已有记录
-						!status.equalsIgnoreCase(this.meterStatus.get(pm))) {
-
-					HBaseUtil.Insert_ps_virtual_meter_log(Constant.agent, Constant.BuildingDB.get(project), project,
-							meter, "up", Constant.format(new Date()), status);
-					if ("disconn".equalsIgnoreCase(status)) {
+		synchronized (Constant.ps_MeterList) {
+			for (Entry<String, List<String>> ms : Constant.ps_MeterList.entrySet()) {
+				String pm = ms.getKey();
+				List<String> l = ms.getValue();
+				String status = this.gatherStatus(l);
+				String[] ml = pm.split("-");
+				String project = ml[0];
+				String meter = ml[1];
+				try {
+					if ("conn".equalsIgnoreCase(status)) {
 						HBaseUtil.Insert_ps_virtual_meter_status(Constant.agent, Constant.BuildingDB.get(project),
 								project, meter, "up", status, Constant.format(new Date()), "");
 					}
+
+					if (
+					// 首次
+					!this.meterStatus.containsKey(pm) || null == this.meterStatus.get(pm) ||
+					// 已有记录
+							!status.equalsIgnoreCase(this.meterStatus.get(pm))) {
+
+						HBaseUtil.Insert_ps_virtual_meter_log(Constant.agent, Constant.BuildingDB.get(project), project,
+								meter, "up", Constant.format(new Date()), status);
+						if ("disconn".equalsIgnoreCase(status)) {
+							HBaseUtil.Insert_ps_virtual_meter_status(Constant.agent, Constant.BuildingDB.get(project),
+									project, meter, "up", status, Constant.format(new Date()), "");
+						}
+					}
+				} catch (Exception e) {
+					// TODO Auto-generated catch block
+					e.printStackTrace();
 				}
-			} catch (Exception e) {
-				// TODO Auto-generated catch block
-				e.printStackTrace();
+				this.meterStatus.put(pm, status);
 			}
-			this.meterStatus.put(pm, status);
 		}
 	}
 
 	private void physical_status_address_Check() {
 		// TODO Auto-generated method stub
-		for (Entry<String, List<String>> ps : Constant.ps_PhysicalList.entrySet()) {
-			String physical = ps.getKey();
-			String[] pl = physical.split(";");
-			String project = pl[0];
-			String address_1 = pl.length < 2 ? "" : pl[1];
-			if ("".equalsIgnoreCase(address_1)) {
-				continue;
-			}
-			String address_2 = pl.length < 3 ? "" : pl[2];
-			String address_3 = pl.length < 4 ? "" : pl[3];
-			String address_4 = pl.length < 5 ? "" : pl[4];
-			String status = "disconn";
-			if (!physical.endsWith(";;;")) {
-				List<String> l = ps.getValue();
-				status = this.gatherStatus(l);
-			} else {
-				if (Constant.ps_PhysicalStatus.get(physical) != null) {
-					if (this.realCheck - Constant.ps_PhysicalStatus.get(physical) < Constant.Millisecond.MINUTE_1) {
-						status = "conn";
-					}
-				} else {
-					status = "disconn";
+		synchronized (Constant.ps_PhysicalList) {
+			for (Entry<String, List<String>> ps : Constant.ps_PhysicalList.entrySet()) {
+				String physical = ps.getKey();
+				String[] pl = physical.split(";");
+				String project = pl[0];
+				String address_1 = pl.length < 2 ? "" : pl[1];
+				if ("".equalsIgnoreCase(address_1)) {
+					continue;
 				}
-			}
-			try {
-				if ("conn".equalsIgnoreCase(status)) {
-					HBaseUtil.Insert_ps_physical_status(Constant.agent, Constant.BuildingDB.get(project), project,
-							address_1, address_2, address_3, address_4, status, Constant.format(new Date()), "");
+				String address_2 = pl.length < 3 ? "" : pl[2];
+				String address_3 = pl.length < 4 ? "" : pl[3];
+				String address_4 = pl.length < 5 ? "" : pl[4];
+				String status = "disconn";
+				if (!physical.endsWith(";;;")) {
+					List<String> l = ps.getValue();
+					status = this.gatherStatus(l);
+				} else {
+					if (Constant.ps_PhysicalStatus.get(physical) != null) {
+						if (this.realCheck - Constant.ps_PhysicalStatus.get(physical) < Constant.Millisecond.MINUTE_1) {
+							status = "conn";
+						}
+					} else {
+						status = "disconn";
+					}
 				}
-
-				if (
-				// 首次
-				!this.physicalStatus.containsKey(physical) || null == this.physicalStatus.get(physical) ||
-				// 已有记录
-						!status.equalsIgnoreCase(this.physicalStatus.get(physical))) {
-
-					HBaseUtil.Insert_ps_physical_log(Constant.agent, Constant.BuildingDB.get(project), project,
-							address_1, address_2, address_3, address_4, Constant.format(new Date()), status);
-					if ("disconn".equalsIgnoreCase(status)) {
+				try {
+					if ("conn".equalsIgnoreCase(status)) {
 						HBaseUtil.Insert_ps_physical_status(Constant.agent, Constant.BuildingDB.get(project), project,
 								address_1, address_2, address_3, address_4, status, Constant.format(new Date()), "");
 					}
+
+					if (
+					// 首次
+					!this.physicalStatus.containsKey(physical) || null == this.physicalStatus.get(physical) ||
+					// 已有记录
+							!status.equalsIgnoreCase(this.physicalStatus.get(physical))) {
+
+						HBaseUtil.Insert_ps_physical_log(Constant.agent, Constant.BuildingDB.get(project), project,
+								address_1, address_2, address_3, address_4, Constant.format(new Date()), status);
+						if ("disconn".equalsIgnoreCase(status)) {
+							HBaseUtil.Insert_ps_physical_status(Constant.agent, Constant.BuildingDB.get(project),
+									project, address_1, address_2, address_3, address_4, status,
+									Constant.format(new Date()), "");
+						}
+					}
+				} catch (Exception e) {
+					// TODO Auto-generated catch block
+					e.printStackTrace();
 				}
-			} catch (Exception e) {
-				// TODO Auto-generated catch block
-				e.printStackTrace();
+				this.physicalStatus.put(physical, status);
 			}
-			this.physicalStatus.put(physical, status);
 		}
+
 	}
 
 	private String gatherStatus(List<String> l) {

+ 40 - 29
collect/src/main/java/com/saga/util/HBaseUtil.java

@@ -62,40 +62,51 @@ public class HBaseUtil {
 			String key1 = project + ";" + address_1 + ";" + "" + ";" + "" + ";" + "";
 
 			String value = project + "-" + meter + "." + funcid;
-			if (!Constant.ps_PhysicalList.containsKey(key4)) {
-				Constant.ps_PhysicalList.put(key4, new ArrayList<String>());
-			}
-			if (!Constant.ps_PhysicalList.containsKey(key3)) {
-				Constant.ps_PhysicalList.put(key3, new ArrayList<String>());
-			}
-			if (!Constant.ps_PhysicalList.containsKey(key2)) {
-				Constant.ps_PhysicalList.put(key2, new ArrayList<String>());
-			}
-			if (!Constant.ps_PhysicalList.containsKey(key1)) {
-				Constant.ps_PhysicalList.put(key1, new ArrayList<String>());
+			synchronized (Constant.ps_PhysicalList) {
+				if (!Constant.ps_PhysicalList.containsKey(key4)) {
+					Constant.ps_PhysicalList.put(key4, new ArrayList<String>());
+				}
+				if (!Constant.ps_PhysicalList.containsKey(key3)) {
+					Constant.ps_PhysicalList.put(key3, new ArrayList<String>());
+				}
+				if (!Constant.ps_PhysicalList.containsKey(key2)) {
+					Constant.ps_PhysicalList.put(key2, new ArrayList<String>());
+				}
+				if (!Constant.ps_PhysicalList.containsKey(key1)) {
+					Constant.ps_PhysicalList.put(key1, new ArrayList<String>());
+				}
+				if (!Constant.ps_PhysicalList.get(key4).contains(value)) {
+					Constant.ps_PhysicalList.get(key4).add(value);
+				}
+				if (!Constant.ps_PhysicalList.get(key3).contains(value)) {
+					Constant.ps_PhysicalList.get(key3).add(value);
+				}
+				if (!Constant.ps_PhysicalList.get(key2).contains(value)) {
+					Constant.ps_PhysicalList.get(key2).add(value);
+				}
+				if (!Constant.ps_PhysicalList.get(key1).contains(value)) {
+					Constant.ps_PhysicalList.get(key1).add(value);
+				}
 			}
 
-			if (!Constant.ps_MeterList.containsKey(project + "-" + meter)) {
-				Constant.ps_MeterList.put(project + "-" + meter, new ArrayList<String>());
-			}
-			if (!Constant.ps_ProjectList.containsKey(project)) {
-				Constant.ps_ProjectList.put(project, new ArrayList<String>());
-			}
-			if (!Constant.ps_PhysicalList.get(key4).contains(value)) {
-				Constant.ps_PhysicalList.get(key4).add(value);
-			}
-			if (!Constant.ps_PhysicalList.get(key3).contains(value)) {
-				Constant.ps_PhysicalList.get(key3).add(value);
-			}
-			if (!Constant.ps_PhysicalList.get(key2).contains(value)) {
-				Constant.ps_PhysicalList.get(key2).add(value);
+			synchronized (Constant.ps_MeterList) {
+				if (!Constant.ps_MeterList.containsKey(project + "-" + meter)) {
+					Constant.ps_MeterList.put(project + "-" + meter, new ArrayList<String>());
+				}
+				if (!Constant.ps_MeterList.get(project + "-" + meter).contains(value)) {
+					Constant.ps_MeterList.get(project + "-" + meter).add(value);
+				}
 			}
-			if (!Constant.ps_PhysicalList.get(key1).contains(value)) {
-				Constant.ps_PhysicalList.get(key1).add(value);
+			synchronized (Constant.ps_ProjectList) {
+
+				if (!Constant.ps_ProjectList.containsKey(project)) {
+					Constant.ps_ProjectList.put(project, new ArrayList<String>());
+				}
+				if (!Constant.ps_ProjectList.get(project).contains(value)) {
+					Constant.ps_ProjectList.get(project).add(value);
+				}
 			}
 
-			Constant.ps_MeterList.get(project + "-" + meter).add(value);
-			Constant.ps_ProjectList.get(project).add(value);
 			checkStatus(project, meter, funcid, address_1, address_2, address_3, address_4);
 		}
 		LogUtil.info("Reload Project Point List:" + project + "  dy_pointlist End...");