Commit d4f6ec39b7e0421757a6b9d1a68b1c4610ea2e8c

Authored by 648540858
1 parent e1c8183a

优化CivilCode缓存

src/main/java/com/genersoft/iot/vmp/conf/CivilCodeFileConf.java
1 package com.genersoft.iot.vmp.conf; 1 package com.genersoft.iot.vmp.conf;
2 2
3 import com.genersoft.iot.vmp.common.CivilCodePo; 3 import com.genersoft.iot.vmp.common.CivilCodePo;
4 -import org.ehcache.impl.internal.concurrent.ConcurrentHashMap; 4 +import com.genersoft.iot.vmp.utils.CivilCodeUtil;
5 import org.slf4j.Logger; 5 import org.slf4j.Logger;
6 import org.slf4j.LoggerFactory; 6 import org.slf4j.LoggerFactory;
7 import org.springframework.beans.factory.annotation.Autowired; 7 import org.springframework.beans.factory.annotation.Autowired;
@@ -17,7 +17,8 @@ import java.io.File; @@ -17,7 +17,8 @@ import java.io.File;
17 import java.io.InputStream; 17 import java.io.InputStream;
18 import java.io.InputStreamReader; 18 import java.io.InputStreamReader;
19 import java.nio.file.Files; 19 import java.nio.file.Files;
20 -import java.util.Map; 20 +import java.util.ArrayList;
  21 +import java.util.List;
21 22
22 /** 23 /**
23 * 启动时读取行政区划表 24 * 启动时读取行政区划表
@@ -28,8 +29,6 @@ public class CivilCodeFileConf implements CommandLineRunner { @@ -28,8 +29,6 @@ public class CivilCodeFileConf implements CommandLineRunner {
28 29
29 private final static Logger logger = LoggerFactory.getLogger(CivilCodeFileConf.class); 30 private final static Logger logger = LoggerFactory.getLogger(CivilCodeFileConf.class);
30 31
31 - private final Map<String, CivilCodePo> civilCodeMap= new ConcurrentHashMap<>();  
32 -  
33 @Autowired 32 @Autowired
34 @Lazy 33 @Lazy
35 private UserSetting userSetting; 34 private UserSetting userSetting;
@@ -62,6 +61,7 @@ public class CivilCodeFileConf implements CommandLineRunner { @@ -62,6 +61,7 @@ public class CivilCodeFileConf implements CommandLineRunner {
62 BufferedReader inputStreamReader = new BufferedReader(new InputStreamReader(inputStream)); 61 BufferedReader inputStreamReader = new BufferedReader(new InputStreamReader(inputStream));
63 int index = -1; 62 int index = -1;
64 String line; 63 String line;
  64 + List<CivilCodePo> civilCodePoList = new ArrayList<>();
65 while ((line = inputStreamReader.readLine()) != null) { 65 while ((line = inputStreamReader.readLine()) != null) {
66 index ++; 66 index ++;
67 if (index == 0) { 67 if (index == 0) {
@@ -69,36 +69,15 @@ public class CivilCodeFileConf implements CommandLineRunner { @@ -69,36 +69,15 @@ public class CivilCodeFileConf implements CommandLineRunner {
69 } 69 }
70 String[] infoArray = line.split(","); 70 String[] infoArray = line.split(",");
71 CivilCodePo civilCodePo = CivilCodePo.getInstance(infoArray); 71 CivilCodePo civilCodePo = CivilCodePo.getInstance(infoArray);
72 - civilCodeMap.put(civilCodePo.getCode(), civilCodePo); 72 + civilCodePoList.add(civilCodePo);
73 } 73 }
  74 + CivilCodeUtil.INSTANCE.add(civilCodePoList);
74 inputStreamReader.close(); 75 inputStreamReader.close();
75 inputStream.close(); 76 inputStream.close();
76 - if (civilCodeMap.size() == 0) { 77 + if (civilCodePoList.isEmpty()) {
77 logger.warn("[行政区划] 文件内容为空,可能造成目录刷新结果不完整"); 78 logger.warn("[行政区划] 文件内容为空,可能造成目录刷新结果不完整");
78 }else { 79 }else {
79 - logger.info("[行政区划] 加载成功,共加载数据{}条", civilCodeMap.size());  
80 - }  
81 - }  
82 -  
83 - public CivilCodePo getParentCode(String code) {  
84 - if (code.length() > 8) {  
85 - return null;  
86 - }  
87 - if (code.length() == 8) {  
88 - String parentCode = code.substring(0, 6);  
89 - return civilCodeMap.get(parentCode);  
90 - }else {  
91 - CivilCodePo civilCodePo = civilCodeMap.get(code);  
92 - if (civilCodePo == null){  
93 - return null;  
94 - }  
95 - String parentCode = civilCodePo.getParentCode();  
96 - if (parentCode == null) {  
97 - return null;  
98 - }  
99 - return civilCodeMap.get(parentCode); 80 + logger.info("[行政区划] 加载成功,共加载数据{}条", civilCodePoList.size());
100 } 81 }
101 -  
102 } 82 }
103 -  
104 } 83 }
src/main/java/com/genersoft/iot/vmp/gb28181/transmit/event/request/impl/NotifyRequestForCatalogProcessor.java
@@ -108,7 +108,7 @@ public class NotifyRequestForCatalogProcessor extends SIPRequestProcessorParent @@ -108,7 +108,7 @@ public class NotifyRequestForCatalogProcessor extends SIPRequestProcessorParent
108 }else { 108 }else {
109 event = eventElement.getText().toUpperCase(); 109 event = eventElement.getText().toUpperCase();
110 } 110 }
111 - DeviceChannel channel = XmlUtil.channelContentHandler(itemDevice, device, event, civilCodeFileConf); 111 + DeviceChannel channel = XmlUtil.channelContentHandler(itemDevice, device, event);
112 if (channel == null) { 112 if (channel == null) {
113 logger.info("[收到目录订阅]:但是解析失败 {}", new String(evt.getRequest().getRawContent())); 113 logger.info("[收到目录订阅]:但是解析失败 {}", new String(evt.getRequest().getRawContent()));
114 continue; 114 continue;
src/main/java/com/genersoft/iot/vmp/gb28181/transmit/event/request/impl/message/response/cmd/CatalogResponseMessageHandler.java
1 package com.genersoft.iot.vmp.gb28181.transmit.event.request.impl.message.response.cmd; 1 package com.genersoft.iot.vmp.gb28181.transmit.event.request.impl.message.response.cmd;
2 2
3 -import com.genersoft.iot.vmp.conf.CivilCodeFileConf;  
4 import com.genersoft.iot.vmp.conf.SipConfig; 3 import com.genersoft.iot.vmp.conf.SipConfig;
5 import com.genersoft.iot.vmp.gb28181.bean.*; 4 import com.genersoft.iot.vmp.gb28181.bean.*;
6 import com.genersoft.iot.vmp.gb28181.session.CatalogDataCatch; 5 import com.genersoft.iot.vmp.gb28181.session.CatalogDataCatch;
@@ -58,9 +57,6 @@ public class CatalogResponseMessageHandler extends SIPRequestProcessorParent imp @@ -58,9 +57,6 @@ public class CatalogResponseMessageHandler extends SIPRequestProcessorParent imp
58 private ThreadPoolTaskExecutor taskExecutor; 57 private ThreadPoolTaskExecutor taskExecutor;
59 58
60 @Autowired 59 @Autowired
61 - private CivilCodeFileConf civilCodeFileConf;  
62 -  
63 - @Autowired  
64 private SipConfig sipConfig; 60 private SipConfig sipConfig;
65 private AtomicBoolean processing = new AtomicBoolean(false); 61 private AtomicBoolean processing = new AtomicBoolean(false);
66 62
@@ -118,7 +114,7 @@ public class CatalogResponseMessageHandler extends SIPRequestProcessorParent imp @@ -118,7 +114,7 @@ public class CatalogResponseMessageHandler extends SIPRequestProcessorParent imp
118 if (channelDeviceElement == null) { 114 if (channelDeviceElement == null) {
119 continue; 115 continue;
120 } 116 }
121 - DeviceChannel channel = XmlUtil.channelContentHandler(itemDevice, device, null, civilCodeFileConf); 117 + DeviceChannel channel = XmlUtil.channelContentHandler(itemDevice, device, null);
122 if (channel == null) { 118 if (channel == null) {
123 logger.info("[收到目录订阅]:但是解析失败 {}", new String(evt.getRequest().getRawContent())); 119 logger.info("[收到目录订阅]:但是解析失败 {}", new String(evt.getRequest().getRawContent()));
124 continue; 120 continue;
src/main/java/com/genersoft/iot/vmp/gb28181/utils/XmlUtil.java
@@ -3,10 +3,10 @@ package com.genersoft.iot.vmp.gb28181.utils; @@ -3,10 +3,10 @@ package com.genersoft.iot.vmp.gb28181.utils;
3 import com.alibaba.fastjson2.JSONArray; 3 import com.alibaba.fastjson2.JSONArray;
4 import com.alibaba.fastjson2.JSONObject; 4 import com.alibaba.fastjson2.JSONObject;
5 import com.genersoft.iot.vmp.common.CivilCodePo; 5 import com.genersoft.iot.vmp.common.CivilCodePo;
6 -import com.genersoft.iot.vmp.conf.CivilCodeFileConf;  
7 import com.genersoft.iot.vmp.gb28181.bean.Device; 6 import com.genersoft.iot.vmp.gb28181.bean.Device;
8 import com.genersoft.iot.vmp.gb28181.bean.DeviceChannel; 7 import com.genersoft.iot.vmp.gb28181.bean.DeviceChannel;
9 import com.genersoft.iot.vmp.gb28181.event.subscribe.catalog.CatalogEvent; 8 import com.genersoft.iot.vmp.gb28181.event.subscribe.catalog.CatalogEvent;
  9 +import com.genersoft.iot.vmp.utils.CivilCodeUtil;
10 import com.genersoft.iot.vmp.utils.DateUtil; 10 import com.genersoft.iot.vmp.utils.DateUtil;
11 import org.apache.commons.lang3.StringUtils; 11 import org.apache.commons.lang3.StringUtils;
12 import org.apache.commons.lang3.math.NumberUtils; 12 import org.apache.commons.lang3.math.NumberUtils;
@@ -240,7 +240,7 @@ public class XmlUtil { @@ -240,7 +240,7 @@ public class XmlUtil {
240 CivilCode, BusinessGroup,VirtualOrganization,Other 240 CivilCode, BusinessGroup,VirtualOrganization,Other
241 } 241 }
242 242
243 - public static DeviceChannel channelContentHandler(Element itemDevice, Device device, String event, CivilCodeFileConf civilCodeFileConf){ 243 + public static DeviceChannel channelContentHandler(Element itemDevice, Device device, String event){
244 DeviceChannel deviceChannel = new DeviceChannel(); 244 DeviceChannel deviceChannel = new DeviceChannel();
245 deviceChannel.setDeviceId(device.getDeviceId()); 245 deviceChannel.setDeviceId(device.getDeviceId());
246 Element channdelIdElement = itemDevice.element("DeviceID"); 246 Element channdelIdElement = itemDevice.element("DeviceID");
@@ -267,7 +267,7 @@ public class XmlUtil { @@ -267,7 +267,7 @@ public class XmlUtil {
267 } 267 }
268 if(channelId.length() <= 8) { 268 if(channelId.length() <= 8) {
269 deviceChannel.setHasAudio(false); 269 deviceChannel.setHasAudio(false);
270 - CivilCodePo parentCode = civilCodeFileConf.getParentCode(channelId); 270 + CivilCodePo parentCode = CivilCodeUtil.INSTANCE.getParentCode(channelId);
271 if (parentCode != null) { 271 if (parentCode != null) {
272 deviceChannel.setParentId(parentCode.getCode()); 272 deviceChannel.setParentId(parentCode.getCode());
273 deviceChannel.setCivilCode(parentCode.getCode()); 273 deviceChannel.setCivilCode(parentCode.getCode());
src/main/java/com/genersoft/iot/vmp/utils/CivilCodeUtil.java 0 → 100644
  1 +package com.genersoft.iot.vmp.utils;
  2 +
  3 +import com.genersoft.iot.vmp.common.CivilCodePo;
  4 +import org.slf4j.Logger;
  5 +import org.slf4j.LoggerFactory;
  6 +
  7 +import java.util.List;
  8 +import java.util.Map;
  9 +import java.util.concurrent.ConcurrentHashMap;
  10 +
  11 +public enum CivilCodeUtil {
  12 +
  13 + INSTANCE;
  14 + private final static Logger log = LoggerFactory.getLogger(CivilCodeUtil.class);
  15 +
  16 + // 用与消息的缓存
  17 + private final Map<String, CivilCodePo> civilCodeMap = new ConcurrentHashMap<>();
  18 +
  19 + CivilCodeUtil() {
  20 + }
  21 +
  22 + public void add(List<CivilCodePo> civilCodePoList) {
  23 + if (!civilCodePoList.isEmpty()) {
  24 + for (CivilCodePo civilCodePo : civilCodePoList) {
  25 + civilCodeMap.put(civilCodePo.getCode(), civilCodePo);
  26 + }
  27 + }
  28 + }
  29 +
  30 + public CivilCodePo getParentCode(String code) {
  31 + if (code.length() > 8) {
  32 + return null;
  33 + }
  34 + if (code.length() == 8) {
  35 + String parentCode = code.substring(0, 6);
  36 + return civilCodeMap.get(parentCode);
  37 + }else {
  38 + CivilCodePo civilCodePo = civilCodeMap.get(code);
  39 + if (civilCodePo == null){
  40 + return null;
  41 + }
  42 + String parentCode = civilCodePo.getParentCode();
  43 + if (parentCode == null) {
  44 + return null;
  45 + }
  46 + return civilCodeMap.get(parentCode);
  47 + }
  48 +
  49 + }
  50 +}