From d4f6ec39b7e0421757a6b9d1a68b1c4610ea2e8c Mon Sep 17 00:00:00 2001
From: 648540858 <648540858@qq.com>
Date: 星期四, 14 三月 2024 14:54:20 +0800
Subject: [PATCH] 优化CivilCode缓存
---
src/main/java/com/genersoft/iot/vmp/conf/CivilCodeFileConf.java | 37 ++++--------------
src/main/java/com/genersoft/iot/vmp/gb28181/transmit/event/request/impl/NotifyRequestForCatalogProcessor.java | 2
src/main/java/com/genersoft/iot/vmp/gb28181/utils/XmlUtil.java | 6 +-
src/main/java/com/genersoft/iot/vmp/gb28181/transmit/event/request/impl/message/response/cmd/CatalogResponseMessageHandler.java | 6 --
src/main/java/com/genersoft/iot/vmp/utils/CivilCodeUtil.java | 50 +++++++++++++++++++++++++
5 files changed, 63 insertions(+), 38 deletions(-)
diff --git a/src/main/java/com/genersoft/iot/vmp/conf/CivilCodeFileConf.java b/src/main/java/com/genersoft/iot/vmp/conf/CivilCodeFileConf.java
index 20b6eef..8449ebb 100644
--- a/src/main/java/com/genersoft/iot/vmp/conf/CivilCodeFileConf.java
+++ b/src/main/java/com/genersoft/iot/vmp/conf/CivilCodeFileConf.java
@@ -1,7 +1,7 @@
package com.genersoft.iot.vmp.conf;
import com.genersoft.iot.vmp.common.CivilCodePo;
-import org.ehcache.impl.internal.concurrent.ConcurrentHashMap;
+import com.genersoft.iot.vmp.utils.CivilCodeUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
@@ -17,7 +17,8 @@
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.file.Files;
-import java.util.Map;
+import java.util.ArrayList;
+import java.util.List;
/**
* 鍚姩鏃惰鍙栬鏀垮尯鍒掕〃
@@ -27,8 +28,6 @@
public class CivilCodeFileConf implements CommandLineRunner {
private final static Logger logger = LoggerFactory.getLogger(CivilCodeFileConf.class);
-
- private final Map<String, CivilCodePo> civilCodeMap= new ConcurrentHashMap<>();
@Autowired
@Lazy
@@ -62,6 +61,7 @@
BufferedReader inputStreamReader = new BufferedReader(new InputStreamReader(inputStream));
int index = -1;
String line;
+ List<CivilCodePo> civilCodePoList = new ArrayList<>();
while ((line = inputStreamReader.readLine()) != null) {
index ++;
if (index == 0) {
@@ -69,36 +69,15 @@
}
String[] infoArray = line.split(",");
CivilCodePo civilCodePo = CivilCodePo.getInstance(infoArray);
- civilCodeMap.put(civilCodePo.getCode(), civilCodePo);
+ civilCodePoList.add(civilCodePo);
}
+ CivilCodeUtil.INSTANCE.add(civilCodePoList);
inputStreamReader.close();
inputStream.close();
- if (civilCodeMap.size() == 0) {
+ if (civilCodePoList.isEmpty()) {
logger.warn("[琛屾斂鍖哄垝] 鏂囦欢鍐呭涓虹┖锛屽彲鑳介�犳垚鐩綍鍒锋柊缁撴灉涓嶅畬鏁�");
}else {
- logger.info("[琛屾斂鍖哄垝] 鍔犺浇鎴愬姛锛屽叡鍔犺浇鏁版嵁{}鏉�", civilCodeMap.size());
+ logger.info("[琛屾斂鍖哄垝] 鍔犺浇鎴愬姛锛屽叡鍔犺浇鏁版嵁{}鏉�", civilCodePoList.size());
}
}
-
- public CivilCodePo getParentCode(String code) {
- if (code.length() > 8) {
- return null;
- }
- if (code.length() == 8) {
- String parentCode = code.substring(0, 6);
- return civilCodeMap.get(parentCode);
- }else {
- CivilCodePo civilCodePo = civilCodeMap.get(code);
- if (civilCodePo == null){
- return null;
- }
- String parentCode = civilCodePo.getParentCode();
- if (parentCode == null) {
- return null;
- }
- return civilCodeMap.get(parentCode);
- }
-
- }
-
}
diff --git a/src/main/java/com/genersoft/iot/vmp/gb28181/transmit/event/request/impl/NotifyRequestForCatalogProcessor.java b/src/main/java/com/genersoft/iot/vmp/gb28181/transmit/event/request/impl/NotifyRequestForCatalogProcessor.java
index 4ea7667..cd97786 100755
--- a/src/main/java/com/genersoft/iot/vmp/gb28181/transmit/event/request/impl/NotifyRequestForCatalogProcessor.java
+++ b/src/main/java/com/genersoft/iot/vmp/gb28181/transmit/event/request/impl/NotifyRequestForCatalogProcessor.java
@@ -108,7 +108,7 @@
}else {
event = eventElement.getText().toUpperCase();
}
- DeviceChannel channel = XmlUtil.channelContentHandler(itemDevice, device, event, civilCodeFileConf);
+ DeviceChannel channel = XmlUtil.channelContentHandler(itemDevice, device, event);
if (channel == null) {
logger.info("[鏀跺埌鐩綍璁㈤槄]锛氫絾鏄В鏋愬け璐� {}", new String(evt.getRequest().getRawContent()));
continue;
diff --git a/src/main/java/com/genersoft/iot/vmp/gb28181/transmit/event/request/impl/message/response/cmd/CatalogResponseMessageHandler.java b/src/main/java/com/genersoft/iot/vmp/gb28181/transmit/event/request/impl/message/response/cmd/CatalogResponseMessageHandler.java
index 19dde71..c16d7f5 100755
--- a/src/main/java/com/genersoft/iot/vmp/gb28181/transmit/event/request/impl/message/response/cmd/CatalogResponseMessageHandler.java
+++ b/src/main/java/com/genersoft/iot/vmp/gb28181/transmit/event/request/impl/message/response/cmd/CatalogResponseMessageHandler.java
@@ -1,6 +1,5 @@
package com.genersoft.iot.vmp.gb28181.transmit.event.request.impl.message.response.cmd;
-import com.genersoft.iot.vmp.conf.CivilCodeFileConf;
import com.genersoft.iot.vmp.conf.SipConfig;
import com.genersoft.iot.vmp.gb28181.bean.*;
import com.genersoft.iot.vmp.gb28181.session.CatalogDataCatch;
@@ -56,9 +55,6 @@
@Qualifier("taskExecutor")
@Autowired
private ThreadPoolTaskExecutor taskExecutor;
-
- @Autowired
- private CivilCodeFileConf civilCodeFileConf;
@Autowired
private SipConfig sipConfig;
@@ -118,7 +114,7 @@
if (channelDeviceElement == null) {
continue;
}
- DeviceChannel channel = XmlUtil.channelContentHandler(itemDevice, device, null, civilCodeFileConf);
+ DeviceChannel channel = XmlUtil.channelContentHandler(itemDevice, device, null);
if (channel == null) {
logger.info("[鏀跺埌鐩綍璁㈤槄]锛氫絾鏄В鏋愬け璐� {}", new String(evt.getRequest().getRawContent()));
continue;
diff --git a/src/main/java/com/genersoft/iot/vmp/gb28181/utils/XmlUtil.java b/src/main/java/com/genersoft/iot/vmp/gb28181/utils/XmlUtil.java
index 41e0570..9de1ef2 100644
--- a/src/main/java/com/genersoft/iot/vmp/gb28181/utils/XmlUtil.java
+++ b/src/main/java/com/genersoft/iot/vmp/gb28181/utils/XmlUtil.java
@@ -3,10 +3,10 @@
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.genersoft.iot.vmp.common.CivilCodePo;
-import com.genersoft.iot.vmp.conf.CivilCodeFileConf;
import com.genersoft.iot.vmp.gb28181.bean.Device;
import com.genersoft.iot.vmp.gb28181.bean.DeviceChannel;
import com.genersoft.iot.vmp.gb28181.event.subscribe.catalog.CatalogEvent;
+import com.genersoft.iot.vmp.utils.CivilCodeUtil;
import com.genersoft.iot.vmp.utils.DateUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;
@@ -240,7 +240,7 @@
CivilCode, BusinessGroup,VirtualOrganization,Other
}
- public static DeviceChannel channelContentHandler(Element itemDevice, Device device, String event, CivilCodeFileConf civilCodeFileConf){
+ public static DeviceChannel channelContentHandler(Element itemDevice, Device device, String event){
DeviceChannel deviceChannel = new DeviceChannel();
deviceChannel.setDeviceId(device.getDeviceId());
Element channdelIdElement = itemDevice.element("DeviceID");
@@ -267,7 +267,7 @@
}
if(channelId.length() <= 8) {
deviceChannel.setHasAudio(false);
- CivilCodePo parentCode = civilCodeFileConf.getParentCode(channelId);
+ CivilCodePo parentCode = CivilCodeUtil.INSTANCE.getParentCode(channelId);
if (parentCode != null) {
deviceChannel.setParentId(parentCode.getCode());
deviceChannel.setCivilCode(parentCode.getCode());
diff --git a/src/main/java/com/genersoft/iot/vmp/utils/CivilCodeUtil.java b/src/main/java/com/genersoft/iot/vmp/utils/CivilCodeUtil.java
new file mode 100644
index 0000000..ba23ab2
--- /dev/null
+++ b/src/main/java/com/genersoft/iot/vmp/utils/CivilCodeUtil.java
@@ -0,0 +1,50 @@
+package com.genersoft.iot.vmp.utils;
+
+import com.genersoft.iot.vmp.common.CivilCodePo;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+
+public enum CivilCodeUtil {
+
+ INSTANCE;
+ private final static Logger log = LoggerFactory.getLogger(CivilCodeUtil.class);
+
+ // 鐢ㄤ笌娑堟伅鐨勭紦瀛�
+ private final Map<String, CivilCodePo> civilCodeMap = new ConcurrentHashMap<>();
+
+ CivilCodeUtil() {
+ }
+
+ public void add(List<CivilCodePo> civilCodePoList) {
+ if (!civilCodePoList.isEmpty()) {
+ for (CivilCodePo civilCodePo : civilCodePoList) {
+ civilCodeMap.put(civilCodePo.getCode(), civilCodePo);
+ }
+ }
+ }
+
+ public CivilCodePo getParentCode(String code) {
+ if (code.length() > 8) {
+ return null;
+ }
+ if (code.length() == 8) {
+ String parentCode = code.substring(0, 6);
+ return civilCodeMap.get(parentCode);
+ }else {
+ CivilCodePo civilCodePo = civilCodeMap.get(code);
+ if (civilCodePo == null){
+ return null;
+ }
+ String parentCode = civilCodePo.getParentCode();
+ if (parentCode == null) {
+ return null;
+ }
+ return civilCodeMap.get(parentCode);
+ }
+
+ }
+}
--
Gitblit v1.8.0