Commit 5f096921985f0a79abe426396ceada42c841b03c

Authored by 648540858
1 parent f6c48588

添加统一回调管理

src/main/java/com/genersoft/iot/vmp/gb28181/session/CommonSessionManager.java 0 → 100755
  1 +package com.genersoft.iot.vmp.gb28181.session;
  2 +
  3 +import com.genersoft.iot.vmp.common.CommonCallback;
  4 +import org.springframework.scheduling.annotation.Scheduled;
  5 +import org.springframework.stereotype.Component;
  6 +
  7 +import java.util.Calendar;
  8 +import java.util.Map;
  9 +import java.util.concurrent.ConcurrentHashMap;
  10 +
  11 +/**
  12 + * 通用回调管理
  13 + */
  14 +@Component
  15 +public class CommonSessionManager {
  16 +
  17 + public static Map<String, CommonSession> callbackMap = new ConcurrentHashMap<>();
  18 +
  19 + /**
  20 + * 存储回调相关的信息
  21 + */
  22 + class CommonSession{
  23 + public String session;
  24 + public long createTime;
  25 + public int timeout;
  26 +
  27 + public CommonCallback<Object> callback;
  28 + public CommonCallback<String> timeoutCallback;
  29 + }
  30 +
  31 + /**
  32 + * 添加回调
  33 + * @param sessionId 唯一标识
  34 + * @param callback 回调
  35 + * @param timeout 超时时间, 单位分钟
  36 + */
  37 + public void add(String sessionId, CommonCallback<Object> callback, CommonCallback<String> timeoutCallback,
  38 + Integer timeout) {
  39 + CommonSession commonSession = new CommonSession();
  40 + commonSession.session = sessionId;
  41 + commonSession.callback = callback;
  42 + commonSession.createTime = System.currentTimeMillis();
  43 + if (timeoutCallback != null) {
  44 + commonSession.timeoutCallback = timeoutCallback;
  45 + }
  46 + if (timeout != null) {
  47 + commonSession.timeout = timeout;
  48 + }
  49 + callbackMap.put(sessionId, commonSession);
  50 + }
  51 +
  52 + public void add(String sessionId, CommonCallback<Object> callback) {
  53 + add(sessionId, callback, null, 1);
  54 + }
  55 +
  56 + public CommonCallback<Object> get(String sessionId, boolean destroy) {
  57 + CommonSession commonSession = callbackMap.get(sessionId);
  58 + if (destroy) {
  59 + callbackMap.remove(sessionId);
  60 + }
  61 + return commonSession.callback;
  62 + }
  63 +
  64 + public CommonCallback<Object> get(String sessionId) {
  65 + return get(sessionId, false);
  66 + }
  67 +
  68 + public void delete(String sessionID) {
  69 + callbackMap.remove(sessionID);
  70 + }
  71 +
  72 + @Scheduled(fixedRate= 60) //每分钟执行一次
  73 + public void execute(){
  74 + Calendar cal = Calendar.getInstance();
  75 + cal.add(Calendar.MINUTE, -1);
  76 + for (String session : callbackMap.keySet()) {
  77 + if (callbackMap.get(session).createTime < cal.getTimeInMillis()) {
  78 + // 超时
  79 + if (callbackMap.get(session).timeoutCallback != null) {
  80 + callbackMap.get(session).timeoutCallback.run("timeout");
  81 + }
  82 + callbackMap.remove(session);
  83 + }
  84 + }
  85 + }
  86 +}
... ...
src/main/java/com/genersoft/iot/vmp/gb28181/transmit/event/request/impl/NotifyRequestProcessor.java
... ... @@ -6,7 +6,6 @@ import com.genersoft.iot.vmp.conf.SipConfig;
6 6 import com.genersoft.iot.vmp.conf.UserSetting;
7 7 import com.genersoft.iot.vmp.gb28181.bean.*;
8 8 import com.genersoft.iot.vmp.gb28181.event.EventPublisher;
9   -import com.genersoft.iot.vmp.gb28181.event.subscribe.catalog.CatalogEvent;
10 9 import com.genersoft.iot.vmp.gb28181.transmit.SIPProcessorObserver;
11 10 import com.genersoft.iot.vmp.gb28181.transmit.callback.DeferredResultHolder;
12 11 import com.genersoft.iot.vmp.gb28181.transmit.cmd.impl.SIPCommander;
... ... @@ -38,7 +37,6 @@ import javax.sip.SipException;
38 37 import javax.sip.header.FromHeader;
39 38 import javax.sip.message.Response;
40 39 import java.text.ParseException;
41   -import java.util.Iterator;
42 40 import java.util.List;
43 41 import java.util.concurrent.ConcurrentLinkedQueue;
44 42  
... ... @@ -222,7 +220,6 @@ public class NotifyRequestProcessor extends SIPRequestProcessorParent implements
222 220 mobilePosition.getLongitude(), mobilePosition.getLatitude());
223 221 mobilePosition.setReportSource("Mobile Position");
224 222  
225   -
226 223 // 更新device channel 的经纬度
227 224 DeviceChannel deviceChannel = new DeviceChannel();
228 225 deviceChannel.setDeviceId(device.getDeviceId());
... ... @@ -242,6 +239,8 @@ public class NotifyRequestProcessor extends SIPRequestProcessorParent implements
242 239 }
243 240  
244 241 storager.updateChannelPosition(deviceChannel);
  242 + // 向关联了该通道并且开启移动位置订阅的上级平台发送移动位置订阅消息
  243 +
245 244  
246 245 // 发送redis消息。 通知位置信息的变化
247 246 JSONObject jsonObject = new JSONObject();
... ...