Commit bc0319b3f338412aa18f73bd749057e9ea3a7125
1 parent
b6fa459b
将device信息写入redis以提高sip处理速度
Showing
12 changed files
with
111 additions
and
10 deletions
src/main/java/com/genersoft/iot/vmp/gb28181/transmit/event/request/impl/InviteRequestProcessor.java
| ... | ... | @@ -318,7 +318,7 @@ public class InviteRequestProcessor extends SIPRequestProcessorParent implements |
| 318 | 318 | |
| 319 | 319 | } else { |
| 320 | 320 | // 非上级平台请求,查询是否设备请求(通常为接收语音广播的设备) |
| 321 | - Device device = storager.queryVideoDevice(requesterId); | |
| 321 | + Device device = redisCatchStorage.getDevice(requesterId); | |
| 322 | 322 | if (device != null) { |
| 323 | 323 | logger.info("收到设备" + requesterId + "的语音广播Invite请求"); |
| 324 | 324 | responseAck(evt, Response.TRYING); | ... | ... |
src/main/java/com/genersoft/iot/vmp/gb28181/transmit/event/request/impl/NotifyRequestProcessor.java
| ... | ... | @@ -112,7 +112,7 @@ public class NotifyRequestProcessor extends SIPRequestProcessorParent implements |
| 112 | 112 | MobilePosition mobilePosition = new MobilePosition(); |
| 113 | 113 | Element deviceIdElement = rootElement.element("DeviceID"); |
| 114 | 114 | String deviceId = deviceIdElement.getTextTrim().toString(); |
| 115 | - Device device = storager.queryVideoDevice(deviceId); | |
| 115 | + Device device = redisCatchStorage.getDevice(deviceId); | |
| 116 | 116 | if (device != null) { |
| 117 | 117 | if (!StringUtils.isEmpty(device.getName())) { |
| 118 | 118 | mobilePosition.setDeviceName(device.getName()); |
| ... | ... | @@ -168,7 +168,7 @@ public class NotifyRequestProcessor extends SIPRequestProcessorParent implements |
| 168 | 168 | Element deviceIdElement = rootElement.element("DeviceID"); |
| 169 | 169 | String deviceId = deviceIdElement.getText().toString(); |
| 170 | 170 | |
| 171 | - Device device = storager.queryVideoDevice(deviceId); | |
| 171 | + Device device = redisCatchStorage.getDevice(deviceId); | |
| 172 | 172 | if (device == null) { |
| 173 | 173 | return; |
| 174 | 174 | } |
| ... | ... | @@ -235,7 +235,7 @@ public class NotifyRequestProcessor extends SIPRequestProcessorParent implements |
| 235 | 235 | String deviceId = SipUtils.getUserIdFromFromHeader(fromHeader); |
| 236 | 236 | |
| 237 | 237 | Element rootElement = getRootElement(evt); |
| 238 | - Device device = storager.queryVideoDevice(deviceId); | |
| 238 | + Device device = redisCatchStorage.getDevice(deviceId); | |
| 239 | 239 | if (device == null) { |
| 240 | 240 | return; |
| 241 | 241 | } | ... | ... |
src/main/java/com/genersoft/iot/vmp/gb28181/transmit/event/request/impl/RegisterRequestProcessor.java
| ... | ... | @@ -10,6 +10,7 @@ import com.genersoft.iot.vmp.gb28181.event.EventPublisher; |
| 10 | 10 | import com.genersoft.iot.vmp.gb28181.transmit.SIPProcessorObserver; |
| 11 | 11 | import com.genersoft.iot.vmp.gb28181.transmit.event.request.ISIPRequestProcessor; |
| 12 | 12 | import com.genersoft.iot.vmp.gb28181.transmit.event.request.SIPRequestProcessorParent; |
| 13 | +import com.genersoft.iot.vmp.storager.IRedisCatchStorage; | |
| 13 | 14 | import com.genersoft.iot.vmp.storager.IVideoManagerStorager; |
| 14 | 15 | import gov.nist.javax.sip.RequestEventExt; |
| 15 | 16 | import gov.nist.javax.sip.address.AddressImpl; |
| ... | ... | @@ -52,6 +53,9 @@ public class RegisterRequestProcessor extends SIPRequestProcessorParent implemen |
| 52 | 53 | private RegisterLogicHandler handler; |
| 53 | 54 | |
| 54 | 55 | @Autowired |
| 56 | + private IRedisCatchStorage redisCatchStorage; | |
| 57 | + | |
| 58 | + @Autowired | |
| 55 | 59 | private IVideoManagerStorager storager; |
| 56 | 60 | |
| 57 | 61 | @Autowired |
| ... | ... | @@ -86,7 +90,7 @@ public class RegisterRequestProcessor extends SIPRequestProcessorParent implemen |
| 86 | 90 | AddressImpl address = (AddressImpl) fromHeader.getAddress(); |
| 87 | 91 | SipUri uri = (SipUri) address.getURI(); |
| 88 | 92 | String deviceId = uri.getUser(); |
| 89 | - Device device = storager.queryVideoDevice(deviceId); | |
| 93 | + Device device = redisCatchStorage.getDevice(deviceId); | |
| 90 | 94 | AuthorizationHeader authorhead = (AuthorizationHeader) request.getHeader(AuthorizationHeader.NAME); |
| 91 | 95 | // 校验密码是否正确 |
| 92 | 96 | if (authorhead != null) { | ... | ... |
src/main/java/com/genersoft/iot/vmp/gb28181/transmit/event/request/impl/message/MessageRequestProcessor.java
| ... | ... | @@ -6,6 +6,7 @@ import com.genersoft.iot.vmp.gb28181.transmit.SIPProcessorObserver; |
| 6 | 6 | import com.genersoft.iot.vmp.gb28181.transmit.event.request.ISIPRequestProcessor; |
| 7 | 7 | import com.genersoft.iot.vmp.gb28181.transmit.event.request.SIPRequestProcessorParent; |
| 8 | 8 | import com.genersoft.iot.vmp.gb28181.utils.SipUtils; |
| 9 | +import com.genersoft.iot.vmp.storager.IRedisCatchStorage; | |
| 9 | 10 | import com.genersoft.iot.vmp.storager.IVideoManagerStorager; |
| 10 | 11 | import org.dom4j.DocumentException; |
| 11 | 12 | import org.dom4j.Element; |
| ... | ... | @@ -38,6 +39,9 @@ public class MessageRequestProcessor extends SIPRequestProcessorParent implement |
| 38 | 39 | @Autowired |
| 39 | 40 | private IVideoManagerStorager storage; |
| 40 | 41 | |
| 42 | + @Autowired | |
| 43 | + private IRedisCatchStorage redisCatchStorage; | |
| 44 | + | |
| 41 | 45 | @Override |
| 42 | 46 | public void afterPropertiesSet() throws Exception { |
| 43 | 47 | // 添加消息处理的订阅 |
| ... | ... | @@ -53,7 +57,7 @@ public class MessageRequestProcessor extends SIPRequestProcessorParent implement |
| 53 | 57 | logger.debug("接收到消息:" + evt.getRequest()); |
| 54 | 58 | String deviceId = SipUtils.getUserIdFromFromHeader(evt.getRequest()); |
| 55 | 59 | // 查询设备是否存在 |
| 56 | - Device device = storage.queryVideoDevice(deviceId); | |
| 60 | + Device device = redisCatchStorage.getDevice(deviceId); | |
| 57 | 61 | // 查询上级平台是否存在 |
| 58 | 62 | ParentPlatform parentPlatform = storage.queryParentPlatByServerGBId(deviceId); |
| 59 | 63 | try { | ... | ... |
src/main/java/com/genersoft/iot/vmp/media/zlm/ZLMHttpHookListener.java
| ... | ... | @@ -467,7 +467,7 @@ public class ZLMHttpHookListener { |
| 467 | 467 | if (s.length == 2) { |
| 468 | 468 | String deviceId = s[0]; |
| 469 | 469 | String channelId = s[1]; |
| 470 | - Device device = storager.queryVideoDevice(deviceId); | |
| 470 | + Device device = redisCatchStorage.getDevice(deviceId); | |
| 471 | 471 | if (device != null) { |
| 472 | 472 | UUID uuid = UUID.randomUUID(); |
| 473 | 473 | SSRCInfo ssrcInfo; | ... | ... |
src/main/java/com/genersoft/iot/vmp/service/IStreamPushService.java
src/main/java/com/genersoft/iot/vmp/service/impl/PlayServiceImpl.java
| ... | ... | @@ -96,7 +96,7 @@ public class PlayServiceImpl implements IPlayService { |
| 96 | 96 | resultHolder.invokeResult(msg); |
| 97 | 97 | return playResult; |
| 98 | 98 | } |
| 99 | - Device device = storager.queryVideoDevice(deviceId); | |
| 99 | + Device device = redisCatchStorage.getDevice(deviceId); | |
| 100 | 100 | StreamInfo streamInfo = redisCatchStorage.queryPlayByDevice(deviceId, channelId); |
| 101 | 101 | playResult.setDevice(device); |
| 102 | 102 | // 超时处理 | ... | ... |
src/main/java/com/genersoft/iot/vmp/service/impl/StreamPushServiceImpl.java
| ... | ... | @@ -255,4 +255,30 @@ public class StreamPushServiceImpl implements IStreamPushService { |
| 255 | 255 | public void clean() { |
| 256 | 256 | |
| 257 | 257 | } |
| 258 | + | |
| 259 | + @Override | |
| 260 | + public boolean saveToRandomGB() { | |
| 261 | + List<StreamPushItem> streamPushItems = streamPushMapper.selectAll(); | |
| 262 | + long gbId = 100001; | |
| 263 | + for (StreamPushItem streamPushItem : streamPushItems) { | |
| 264 | + streamPushItem.setStreamType("push"); | |
| 265 | + streamPushItem.setStatus(true); | |
| 266 | + streamPushItem.setGbId("34020000004111" + gbId); | |
| 267 | + gbId ++; | |
| 268 | + } | |
| 269 | + int limitCount = 30; | |
| 270 | + | |
| 271 | + if (streamPushItems.size() > limitCount) { | |
| 272 | + for (int i = 0; i < streamPushItems.size(); i += limitCount) { | |
| 273 | + int toIndex = i + limitCount; | |
| 274 | + if (i + limitCount > streamPushItems.size()) { | |
| 275 | + toIndex = streamPushItems.size(); | |
| 276 | + } | |
| 277 | + gbStreamMapper.batchAdd(streamPushItems.subList(i, toIndex)); | |
| 278 | + } | |
| 279 | + }else { | |
| 280 | + gbStreamMapper.batchAdd(streamPushItems); | |
| 281 | + } | |
| 282 | + return true; | |
| 283 | + } | |
| 258 | 284 | } | ... | ... |
src/main/java/com/genersoft/iot/vmp/storager/IRedisCatchStorage.java
| ... | ... | @@ -2,6 +2,7 @@ package com.genersoft.iot.vmp.storager; |
| 2 | 2 | |
| 3 | 3 | import com.alibaba.fastjson.JSONObject; |
| 4 | 4 | import com.genersoft.iot.vmp.common.StreamInfo; |
| 5 | +import com.genersoft.iot.vmp.gb28181.bean.Device; | |
| 5 | 6 | import com.genersoft.iot.vmp.gb28181.bean.ParentPlatform; |
| 6 | 7 | import com.genersoft.iot.vmp.gb28181.bean.ParentPlatformCatch; |
| 7 | 8 | import com.genersoft.iot.vmp.gb28181.bean.SendRtpItem; |
| ... | ... | @@ -169,4 +170,15 @@ public interface IRedisCatchStorage { |
| 169 | 170 | ThirdPartyGB queryMemberNoGBId(String queryKey); |
| 170 | 171 | |
| 171 | 172 | List<StreamInfo> getStreams(String mediaServerId, String pull); |
| 173 | + | |
| 174 | + /** | |
| 175 | + * 将device信息写入redis | |
| 176 | + * @param device | |
| 177 | + */ | |
| 178 | + void updateDevice(Device device); | |
| 179 | + | |
| 180 | + /** | |
| 181 | + * 获取Device | |
| 182 | + */ | |
| 183 | + Device getDevice(String deviceId); | |
| 172 | 184 | } | ... | ... |
src/main/java/com/genersoft/iot/vmp/storager/dao/GbStreamMapper.java
| ... | ... | @@ -2,6 +2,7 @@ package com.genersoft.iot.vmp.storager.dao; |
| 2 | 2 | |
| 3 | 3 | import com.genersoft.iot.vmp.gb28181.bean.GbStream; |
| 4 | 4 | import com.genersoft.iot.vmp.media.zlm.dto.StreamProxyItem; |
| 5 | +import com.genersoft.iot.vmp.media.zlm.dto.StreamPushItem; | |
| 5 | 6 | import org.apache.ibatis.annotations.*; |
| 6 | 7 | import org.springframework.stereotype.Repository; |
| 7 | 8 | |
| ... | ... | @@ -79,4 +80,17 @@ public interface GbStreamMapper { |
| 79 | 80 | "</foreach>" + |
| 80 | 81 | "</script>") |
| 81 | 82 | void batchDel(List<StreamProxyItem> streamProxyItemList); |
| 83 | + | |
| 84 | + @Insert("<script> " + | |
| 85 | + "insert into gb_stream " + | |
| 86 | + "(app, stream, gbId, name, " + | |
| 87 | + "longitude, latitude, streamType, mediaServerId, status)" + | |
| 88 | + "values " + | |
| 89 | + "<foreach collection='subList' index='index' item='item' separator=','> " + | |
| 90 | + "('${item.app}', '${item.stream}', '${item.gbId}', '${item.name}', " + | |
| 91 | + "'${item.longitude}', '${item.latitude}', '${item.streamType}', " + | |
| 92 | + "'${item.mediaServerId}', ${item.status}) "+ | |
| 93 | + "</foreach> " + | |
| 94 | + "</script>") | |
| 95 | + void batchAdd(List<StreamPushItem> subList); | |
| 82 | 96 | } | ... | ... |
src/main/java/com/genersoft/iot/vmp/storager/impl/RedisCatchStorageImpl.java
| ... | ... | @@ -377,4 +377,16 @@ public class RedisCatchStorageImpl implements IRedisCatchStorage { |
| 377 | 377 | } |
| 378 | 378 | return result; |
| 379 | 379 | } |
| 380 | + | |
| 381 | + @Override | |
| 382 | + public void updateDevice(Device device) { | |
| 383 | + String key = VideoManagerConstants.DEVICE_PREFIX + userSetup.getServerId() + "_" + device.getDeviceId(); | |
| 384 | + redis.set(key, device); | |
| 385 | + } | |
| 386 | + | |
| 387 | + @Override | |
| 388 | + public Device getDevice(String deviceId) { | |
| 389 | + String key = VideoManagerConstants.DEVICE_PREFIX + userSetup.getServerId() + "_" + deviceId; | |
| 390 | + return (Device)redis.get(key); | |
| 391 | + } | |
| 380 | 392 | } | ... | ... |
src/main/java/com/genersoft/iot/vmp/storager/impl/VideoManagerStoragerImpl.java
| ... | ... | @@ -110,6 +110,7 @@ public class VideoManagerStoragerImpl implements IVideoManagerStorager { |
| 110 | 110 | */ |
| 111 | 111 | @Override |
| 112 | 112 | public synchronized boolean create(Device device) { |
| 113 | + redisCatchStorage.updateDevice(device); | |
| 113 | 114 | return deviceMapper.add(device) > 0; |
| 114 | 115 | } |
| 115 | 116 | |
| ... | ... | @@ -128,11 +129,14 @@ public class VideoManagerStoragerImpl implements IVideoManagerStorager { |
| 128 | 129 | Device deviceByDeviceId = deviceMapper.getDeviceByDeviceId(device.getDeviceId()); |
| 129 | 130 | if (deviceByDeviceId == null) { |
| 130 | 131 | device.setCreateTime(now); |
| 132 | + redisCatchStorage.updateDevice(device); | |
| 131 | 133 | return deviceMapper.add(device) > 0; |
| 132 | 134 | }else { |
| 135 | + redisCatchStorage.updateDevice(device); | |
| 133 | 136 | return deviceMapper.update(device) > 0; |
| 134 | 137 | } |
| 135 | 138 | |
| 139 | + | |
| 136 | 140 | } |
| 137 | 141 | |
| 138 | 142 | @Override |
| ... | ... | @@ -185,11 +189,32 @@ public class VideoManagerStoragerImpl implements IVideoManagerStorager { |
| 185 | 189 | } |
| 186 | 190 | } |
| 187 | 191 | } |
| 192 | + int limitCount = 300; | |
| 188 | 193 | if (addChannels.size() > 0) { |
| 189 | - deviceChannelMapper.batchAdd(addChannels); | |
| 194 | + if (addChannels.size() > limitCount) { | |
| 195 | + for (int i = 0; i < addChannels.size(); i += limitCount) { | |
| 196 | + int toIndex = i + limitCount; | |
| 197 | + if (i + limitCount > addChannels.size()) { | |
| 198 | + toIndex = addChannels.size(); | |
| 199 | + } | |
| 200 | + deviceChannelMapper.batchAdd(addChannels.subList(i, toIndex)); | |
| 201 | + } | |
| 202 | + }else { | |
| 203 | + deviceChannelMapper.batchAdd(addChannels); | |
| 204 | + } | |
| 190 | 205 | } |
| 191 | 206 | if (updateChannels.size() > 0) { |
| 192 | - deviceChannelMapper.batchUpdate(updateChannels); | |
| 207 | + if (updateChannels.size() > limitCount) { | |
| 208 | + for (int i = 0; i < updateChannels.size(); i += limitCount) { | |
| 209 | + int toIndex = i + limitCount; | |
| 210 | + if (i + limitCount > updateChannels.size()) { | |
| 211 | + toIndex = updateChannels.size(); | |
| 212 | + } | |
| 213 | + deviceChannelMapper.batchAdd(updateChannels.subList(i, toIndex)); | |
| 214 | + } | |
| 215 | + }else { | |
| 216 | + deviceChannelMapper.batchUpdate(updateChannels); | |
| 217 | + } | |
| 193 | 218 | } |
| 194 | 219 | } |
| 195 | 220 | } |
| ... | ... | @@ -322,6 +347,7 @@ public class VideoManagerStoragerImpl implements IVideoManagerStorager { |
| 322 | 347 | } |
| 323 | 348 | device.setOnline(1); |
| 324 | 349 | logger.info("更新设备在线: " + deviceId); |
| 350 | + redisCatchStorage.updateDevice(device); | |
| 325 | 351 | return deviceMapper.update(device) > 0; |
| 326 | 352 | } |
| 327 | 353 | |
| ... | ... | @@ -337,6 +363,7 @@ public class VideoManagerStoragerImpl implements IVideoManagerStorager { |
| 337 | 363 | Device device = deviceMapper.getDeviceByDeviceId(deviceId); |
| 338 | 364 | if (device == null) return false; |
| 339 | 365 | device.setOnline(0); |
| 366 | + redisCatchStorage.updateDevice(device); | |
| 340 | 367 | return deviceMapper.update(device) > 0; |
| 341 | 368 | } |
| 342 | 369 | ... | ... |