Browse Source

Merge pull request #426 from J1031/bugfix/map_modification_exception

修复ConcurrentModificationException
pull/446/head
648540858 3 years ago
committed by GitHub
parent
commit
d42329a021
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 19
      src/main/java/com/genersoft/iot/vmp/media/zlm/ZLMHttpHookSubscribe.java

19
src/main/java/com/genersoft/iot/vmp/media/zlm/ZLMHttpHookSubscribe.java

@ -3,6 +3,7 @@ package com.genersoft.iot.vmp.media.zlm;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
import com.genersoft.iot.vmp.media.zlm.dto.MediaServerItem; import com.genersoft.iot.vmp.media.zlm.dto.MediaServerItem;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import java.util.*; import java.util.*;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
@ -39,12 +40,8 @@ public class ZLMHttpHookSubscribe {
private Map<HookType, Map<JSONObject, ZLMHttpHookSubscribe.Event>> allSubscribes = new ConcurrentHashMap<>(); private Map<HookType, Map<JSONObject, ZLMHttpHookSubscribe.Event>> allSubscribes = new ConcurrentHashMap<>();
public void addSubscribe(HookType type, JSONObject hookResponse, ZLMHttpHookSubscribe.Event event) { public void addSubscribe(HookType type, JSONObject hookResponse, ZLMHttpHookSubscribe.Event event) {
Map<JSONObject, Event> eventMap = allSubscribes.get(type); allSubscribes.computeIfAbsent(type, k -> new ConcurrentHashMap<>())
if (eventMap == null) { .put(hookResponse, event);
eventMap = new HashMap<JSONObject, Event>();
allSubscribes.put(type,eventMap);
}
eventMap.put(hookResponse, event);
} }
public ZLMHttpHookSubscribe.Event getSubscribe(HookType type, JSONObject hookResponse) { public ZLMHttpHookSubscribe.Event getSubscribe(HookType type, JSONObject hookResponse) {
@ -81,7 +78,8 @@ public class ZLMHttpHookSubscribe {
Set<Map.Entry<JSONObject, Event>> entries = eventMap.entrySet(); Set<Map.Entry<JSONObject, Event>> entries = eventMap.entrySet();
if (entries.size() > 0) { if (entries.size() > 0) {
for (Map.Entry<JSONObject, Event> entry : entries) { List<Map.Entry<JSONObject, ZLMHttpHookSubscribe.Event>> entriesToRemove = new ArrayList<>();
for (Map.Entry<JSONObject, ZLMHttpHookSubscribe.Event> entry : entries) {
JSONObject key = entry.getKey(); JSONObject key = entry.getKey();
Boolean result = null; Boolean result = null;
for (String s : key.keySet()) { for (String s : key.keySet()) {
@ -93,9 +91,16 @@ public class ZLMHttpHookSubscribe {
} }
} }
if (null != result && result){ if (null != result && result){
entriesToRemove.add(entry);
}
}
if (!CollectionUtils.isEmpty(entriesToRemove)) {
for (Map.Entry<JSONObject, ZLMHttpHookSubscribe.Event> entry : entriesToRemove) {
entries.remove(entry); entries.remove(entry);
} }
} }
} }
} }

Loading…
Cancel
Save