Commit d4f6ec39b7e0421757a6b9d1a68b1c4610ea2e8c
1 parent
e1c8183a
优化CivilCode缓存
Showing
5 changed files
with
63 additions
and
38 deletions
src/main/java/com/genersoft/iot/vmp/conf/CivilCodeFileConf.java
| 1 | 1 | package com.genersoft.iot.vmp.conf; |
| 2 | 2 | |
| 3 | 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 | 5 | import org.slf4j.Logger; |
| 6 | 6 | import org.slf4j.LoggerFactory; |
| 7 | 7 | import org.springframework.beans.factory.annotation.Autowired; |
| ... | ... | @@ -17,7 +17,8 @@ import java.io.File; |
| 17 | 17 | import java.io.InputStream; |
| 18 | 18 | import java.io.InputStreamReader; |
| 19 | 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 | 29 | |
| 29 | 30 | private final static Logger logger = LoggerFactory.getLogger(CivilCodeFileConf.class); |
| 30 | 31 | |
| 31 | - private final Map<String, CivilCodePo> civilCodeMap= new ConcurrentHashMap<>(); | |
| 32 | - | |
| 33 | 32 | @Autowired |
| 34 | 33 | @Lazy |
| 35 | 34 | private UserSetting userSetting; |
| ... | ... | @@ -62,6 +61,7 @@ public class CivilCodeFileConf implements CommandLineRunner { |
| 62 | 61 | BufferedReader inputStreamReader = new BufferedReader(new InputStreamReader(inputStream)); |
| 63 | 62 | int index = -1; |
| 64 | 63 | String line; |
| 64 | + List<CivilCodePo> civilCodePoList = new ArrayList<>(); | |
| 65 | 65 | while ((line = inputStreamReader.readLine()) != null) { |
| 66 | 66 | index ++; |
| 67 | 67 | if (index == 0) { |
| ... | ... | @@ -69,36 +69,15 @@ public class CivilCodeFileConf implements CommandLineRunner { |
| 69 | 69 | } |
| 70 | 70 | String[] infoArray = line.split(","); |
| 71 | 71 | CivilCodePo civilCodePo = CivilCodePo.getInstance(infoArray); |
| 72 | - civilCodeMap.put(civilCodePo.getCode(), civilCodePo); | |
| 72 | + civilCodePoList.add(civilCodePo); | |
| 73 | 73 | } |
| 74 | + CivilCodeUtil.INSTANCE.add(civilCodePoList); | |
| 74 | 75 | inputStreamReader.close(); |
| 75 | 76 | inputStream.close(); |
| 76 | - if (civilCodeMap.size() == 0) { | |
| 77 | + if (civilCodePoList.isEmpty()) { | |
| 77 | 78 | logger.warn("[行政区划] 文件内容为空,可能造成目录刷新结果不完整"); |
| 78 | 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 | 108 | }else { |
| 109 | 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 | 112 | if (channel == null) { |
| 113 | 113 | logger.info("[收到目录订阅]:但是解析失败 {}", new String(evt.getRequest().getRawContent())); |
| 114 | 114 | continue; | ... | ... |
src/main/java/com/genersoft/iot/vmp/gb28181/transmit/event/request/impl/message/response/cmd/CatalogResponseMessageHandler.java
| 1 | 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 | 3 | import com.genersoft.iot.vmp.conf.SipConfig; |
| 5 | 4 | import com.genersoft.iot.vmp.gb28181.bean.*; |
| 6 | 5 | import com.genersoft.iot.vmp.gb28181.session.CatalogDataCatch; |
| ... | ... | @@ -58,9 +57,6 @@ public class CatalogResponseMessageHandler extends SIPRequestProcessorParent imp |
| 58 | 57 | private ThreadPoolTaskExecutor taskExecutor; |
| 59 | 58 | |
| 60 | 59 | @Autowired |
| 61 | - private CivilCodeFileConf civilCodeFileConf; | |
| 62 | - | |
| 63 | - @Autowired | |
| 64 | 60 | private SipConfig sipConfig; |
| 65 | 61 | private AtomicBoolean processing = new AtomicBoolean(false); |
| 66 | 62 | |
| ... | ... | @@ -118,7 +114,7 @@ public class CatalogResponseMessageHandler extends SIPRequestProcessorParent imp |
| 118 | 114 | if (channelDeviceElement == null) { |
| 119 | 115 | continue; |
| 120 | 116 | } |
| 121 | - DeviceChannel channel = XmlUtil.channelContentHandler(itemDevice, device, null, civilCodeFileConf); | |
| 117 | + DeviceChannel channel = XmlUtil.channelContentHandler(itemDevice, device, null); | |
| 122 | 118 | if (channel == null) { |
| 123 | 119 | logger.info("[收到目录订阅]:但是解析失败 {}", new String(evt.getRequest().getRawContent())); |
| 124 | 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 | 3 | import com.alibaba.fastjson2.JSONArray; |
| 4 | 4 | import com.alibaba.fastjson2.JSONObject; |
| 5 | 5 | import com.genersoft.iot.vmp.common.CivilCodePo; |
| 6 | -import com.genersoft.iot.vmp.conf.CivilCodeFileConf; | |
| 7 | 6 | import com.genersoft.iot.vmp.gb28181.bean.Device; |
| 8 | 7 | import com.genersoft.iot.vmp.gb28181.bean.DeviceChannel; |
| 9 | 8 | import com.genersoft.iot.vmp.gb28181.event.subscribe.catalog.CatalogEvent; |
| 9 | +import com.genersoft.iot.vmp.utils.CivilCodeUtil; | |
| 10 | 10 | import com.genersoft.iot.vmp.utils.DateUtil; |
| 11 | 11 | import org.apache.commons.lang3.StringUtils; |
| 12 | 12 | import org.apache.commons.lang3.math.NumberUtils; |
| ... | ... | @@ -240,7 +240,7 @@ public class XmlUtil { |
| 240 | 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 | 244 | DeviceChannel deviceChannel = new DeviceChannel(); |
| 245 | 245 | deviceChannel.setDeviceId(device.getDeviceId()); |
| 246 | 246 | Element channdelIdElement = itemDevice.element("DeviceID"); |
| ... | ... | @@ -267,7 +267,7 @@ public class XmlUtil { |
| 267 | 267 | } |
| 268 | 268 | if(channelId.length() <= 8) { |
| 269 | 269 | deviceChannel.setHasAudio(false); |
| 270 | - CivilCodePo parentCode = civilCodeFileConf.getParentCode(channelId); | |
| 270 | + CivilCodePo parentCode = CivilCodeUtil.INSTANCE.getParentCode(channelId); | |
| 271 | 271 | if (parentCode != null) { |
| 272 | 272 | deviceChannel.setParentId(parentCode.getCode()); |
| 273 | 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 | +} | ... | ... |