From 03d6ad289baddf8feed64ffca5f1b13828bea710 Mon Sep 17 00:00:00 2001
From: 648540858 <648540858@qq.com>
Date: 星期四, 01 九月 2022 14:50:28 +0800
Subject: [PATCH] Merge branch 'wvp-28181-2.0'

---
 src/main/java/com/genersoft/iot/vmp/media/zlm/ZlmHttpHookSubscribe.java |   55 +++++++++++++++++++++++++++++++++++++++++--------------
 1 files changed, 41 insertions(+), 14 deletions(-)

diff --git a/src/main/java/com/genersoft/iot/vmp/media/zlm/ZLMHttpHookSubscribe.java b/src/main/java/com/genersoft/iot/vmp/media/zlm/ZlmHttpHookSubscribe.java
similarity index 69%
rename from src/main/java/com/genersoft/iot/vmp/media/zlm/ZLMHttpHookSubscribe.java
rename to src/main/java/com/genersoft/iot/vmp/media/zlm/ZlmHttpHookSubscribe.java
index 57b6d81..823bdab 100644
--- a/src/main/java/com/genersoft/iot/vmp/media/zlm/ZLMHttpHookSubscribe.java
+++ b/src/main/java/com/genersoft/iot/vmp/media/zlm/ZlmHttpHookSubscribe.java
@@ -4,6 +4,9 @@
 import com.genersoft.iot.vmp.media.zlm.dto.HookType;
 import com.genersoft.iot.vmp.media.zlm.dto.IHookSubscribe;
 import com.genersoft.iot.vmp.media.zlm.dto.MediaServerItem;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.scheduling.annotation.Scheduled;
 import org.springframework.stereotype.Component;
 import org.springframework.util.CollectionUtils;
 
@@ -13,21 +16,22 @@
 import java.util.concurrent.TimeUnit;
 
 /**
- * @description:閽堝 ZLMediaServer鐨刪ook浜嬩欢璁㈤槄
- * @author: pan
- * @date:   2020骞�12鏈�2鏃� 21:17:32
+ * ZLMediaServer鐨刪ook浜嬩欢璁㈤槄
+ * @author lin
  */
 @Component
-public class ZLMHttpHookSubscribe {
+public class ZlmHttpHookSubscribe {
+
+    private final static Logger logger = LoggerFactory.getLogger(ZlmHttpHookSubscribe.class);
 
     @FunctionalInterface
     public interface Event{
         void response(MediaServerItem mediaServerItem, JSONObject response);
     }
 
-    private Map<HookType, Map<IHookSubscribe, ZLMHttpHookSubscribe.Event>> allSubscribes = new ConcurrentHashMap<>();
+    private Map<HookType, Map<IHookSubscribe, ZlmHttpHookSubscribe.Event>> allSubscribes = new ConcurrentHashMap<>();
 
-    public void addSubscribe(IHookSubscribe hookSubscribe, ZLMHttpHookSubscribe.Event event) {
+    public void addSubscribe(IHookSubscribe hookSubscribe, ZlmHttpHookSubscribe.Event event) {
         if (hookSubscribe.getExpires() == null) {
             // 榛樿5鍒嗛挓杩囨湡
             Instant expiresInstant = Instant.now().plusSeconds(TimeUnit.MINUTES.toSeconds(5));
@@ -36,8 +40,8 @@
         allSubscribes.computeIfAbsent(hookSubscribe.getHookType(), k -> new ConcurrentHashMap<>()).put(hookSubscribe, event);
     }
 
-    public ZLMHttpHookSubscribe.Event sendNotify(HookType type, JSONObject hookResponse) {
-        ZLMHttpHookSubscribe.Event event= null;
+    public ZlmHttpHookSubscribe.Event sendNotify(HookType type, JSONObject hookResponse) {
+        ZlmHttpHookSubscribe.Event event= null;
         Map<IHookSubscribe, Event> eventMap = allSubscribes.get(type);
         if (eventMap == null) {
             return null;
@@ -69,8 +73,8 @@
 
         Set<Map.Entry<IHookSubscribe, Event>> entries = eventMap.entrySet();
         if (entries.size() > 0) {
-            List<Map.Entry<IHookSubscribe, ZLMHttpHookSubscribe.Event>> entriesToRemove = new ArrayList<>();
-            for (Map.Entry<IHookSubscribe, ZLMHttpHookSubscribe.Event> entry : entries) {
+            List<Map.Entry<IHookSubscribe, ZlmHttpHookSubscribe.Event>> entriesToRemove = new ArrayList<>();
+            for (Map.Entry<IHookSubscribe, ZlmHttpHookSubscribe.Event> entry : entries) {
                 JSONObject content = entry.getKey().getContent();
                 if (content == null || content.size() == 0) {
                     entriesToRemove.add(entry);
@@ -87,13 +91,13 @@
                         result = result && content.getString(s).equals(hookSubscribe.getContent().getString(s));
                     }
                 }
-                if (null != result && result){
+                if (result){
                     entriesToRemove.add(entry);
                 }
             }
 
             if (!CollectionUtils.isEmpty(entriesToRemove)) {
-                for (Map.Entry<IHookSubscribe, ZLMHttpHookSubscribe.Event> entry : entriesToRemove) {
+                for (Map.Entry<IHookSubscribe, ZlmHttpHookSubscribe.Event> entry : entriesToRemove) {
                     entries.remove(entry);
                 }
             }
@@ -106,12 +110,12 @@
      * @param type
      * @return
      */
-    public List<ZLMHttpHookSubscribe.Event> getSubscribes(HookType type) {
+    public List<ZlmHttpHookSubscribe.Event> getSubscribes(HookType type) {
         Map<IHookSubscribe, Event> eventMap = allSubscribes.get(type);
         if (eventMap == null) {
             return null;
         }
-        List<ZLMHttpHookSubscribe.Event> result = new ArrayList<>();
+        List<ZlmHttpHookSubscribe.Event> result = new ArrayList<>();
         for (IHookSubscribe key : eventMap.keySet()) {
             result.add(eventMap.get(key));
         }
@@ -127,5 +131,28 @@
         return result;
     }
 
+    /**
+     * 瀵硅闃呮暟鎹繘琛岃繃鏈熸竻鐞�
+     */
+    @Scheduled(cron="0 0/5 * * * ?")   //姣�5鍒嗛挓鎵ц涓�娆�
+    public void execute(){
 
+        logger.info("[hook璁㈤槄] 娓呯悊");
+
+        Instant instant = Instant.now().minusMillis(TimeUnit.MINUTES.toMillis(5));
+        int total = 0;
+        for (HookType hookType : allSubscribes.keySet()) {
+            Map<IHookSubscribe, Event> hookSubscribeEventMap = allSubscribes.get(hookType);
+            if (hookSubscribeEventMap.size() > 0) {
+                for (IHookSubscribe hookSubscribe : hookSubscribeEventMap.keySet()) {
+                    if (hookSubscribe.getExpires().isBefore(instant)) {
+                        // 杩囨湡鐨�
+                        hookSubscribeEventMap.remove(hookSubscribe);
+                        total ++;
+                    }
+                }
+            }
+        }
+        logger.info("[hook璁㈤槄] 娓呯悊缁撴潫锛屽叡娓呯悊{}鏉¤繃鏈熸暟鎹�", total);
+    }
 }

--
Gitblit v1.8.0