Commit d42329a0219a5b523acfa93b3a94fa17db91d910

Authored by 648540858
Committed by GitHub
2 parents 5ed44c3e d883de7f

Merge pull request #426 from J1031/bugfix/map_modification_exception

修复ConcurrentModificationException
src/main/java/com/genersoft/iot/vmp/media/zlm/ZLMHttpHookSubscribe.java
@@ -3,6 +3,7 @@ package com.genersoft.iot.vmp.media.zlm; @@ -3,6 +3,7 @@ package com.genersoft.iot.vmp.media.zlm;
3 import com.alibaba.fastjson.JSONObject; 3 import com.alibaba.fastjson.JSONObject;
4 import com.genersoft.iot.vmp.media.zlm.dto.MediaServerItem; 4 import com.genersoft.iot.vmp.media.zlm.dto.MediaServerItem;
5 import org.springframework.stereotype.Component; 5 import org.springframework.stereotype.Component;
  6 +import org.springframework.util.CollectionUtils;
6 7
7 import java.util.*; 8 import java.util.*;
8 import java.util.concurrent.ConcurrentHashMap; 9 import java.util.concurrent.ConcurrentHashMap;
@@ -39,12 +40,8 @@ public class ZLMHttpHookSubscribe { @@ -39,12 +40,8 @@ public class ZLMHttpHookSubscribe {
39 private Map<HookType, Map<JSONObject, ZLMHttpHookSubscribe.Event>> allSubscribes = new ConcurrentHashMap<>(); 40 private Map<HookType, Map<JSONObject, ZLMHttpHookSubscribe.Event>> allSubscribes = new ConcurrentHashMap<>();
40 41
41 public void addSubscribe(HookType type, JSONObject hookResponse, ZLMHttpHookSubscribe.Event event) { 42 public void addSubscribe(HookType type, JSONObject hookResponse, ZLMHttpHookSubscribe.Event event) {
42 - Map<JSONObject, Event> eventMap = allSubscribes.get(type);  
43 - if (eventMap == null) {  
44 - eventMap = new HashMap<JSONObject, Event>();  
45 - allSubscribes.put(type,eventMap);  
46 - }  
47 - eventMap.put(hookResponse, event); 43 + allSubscribes.computeIfAbsent(type, k -> new ConcurrentHashMap<>())
  44 + .put(hookResponse, event);
48 } 45 }
49 46
50 public ZLMHttpHookSubscribe.Event getSubscribe(HookType type, JSONObject hookResponse) { 47 public ZLMHttpHookSubscribe.Event getSubscribe(HookType type, JSONObject hookResponse) {
@@ -81,7 +78,8 @@ public class ZLMHttpHookSubscribe { @@ -81,7 +78,8 @@ public class ZLMHttpHookSubscribe {
81 78
82 Set<Map.Entry<JSONObject, Event>> entries = eventMap.entrySet(); 79 Set<Map.Entry<JSONObject, Event>> entries = eventMap.entrySet();
83 if (entries.size() > 0) { 80 if (entries.size() > 0) {
84 - for (Map.Entry<JSONObject, Event> entry : entries) { 81 + List<Map.Entry<JSONObject, ZLMHttpHookSubscribe.Event>> entriesToRemove = new ArrayList<>();
  82 + for (Map.Entry<JSONObject, ZLMHttpHookSubscribe.Event> entry : entries) {
85 JSONObject key = entry.getKey(); 83 JSONObject key = entry.getKey();
86 Boolean result = null; 84 Boolean result = null;
87 for (String s : key.keySet()) { 85 for (String s : key.keySet()) {
@@ -93,9 +91,16 @@ public class ZLMHttpHookSubscribe { @@ -93,9 +91,16 @@ public class ZLMHttpHookSubscribe {
93 } 91 }
94 } 92 }
95 if (null != result && result){ 93 if (null != result && result){
  94 + entriesToRemove.add(entry);
  95 + }
  96 + }
  97 +
  98 + if (!CollectionUtils.isEmpty(entriesToRemove)) {
  99 + for (Map.Entry<JSONObject, ZLMHttpHookSubscribe.Event> entry : entriesToRemove) {
96 entries.remove(entry); 100 entries.remove(entry);
97 } 101 }
98 } 102 }
  103 +
99 } 104 }
100 } 105 }
101 106