[java] Synchronizing access to listener list to avoid concurrent modification exceptions.
@@ -111,12 +111,16 @@ public Connection(HttpClient client, String url) {
Objects.requireNonNull(event);
Objects.requireNonNull(handler);
+ synchronized (eventCallbacks) {
eventCallbacks.put(event, handler);
}
+ }
public void clearListeners() {
+ synchronized (eventCallbacks) {
eventCallbacks.clear();
}
+ }
@Override
public void close() {
@@ -159,6 +163,7 @@ public void onText(CharSequence data) {
} else if (raw.get("method") instanceof String && raw.get("params") instanceof Map) {
LOG.fine("Seen: " + raw);
+ synchronized (eventCallbacks) {
// TODO: Also only decode once.
eventCallbacks.keySet().stream()
.filter(event -> raw.get("method").equals(event.getMethod()))
@@ -194,6 +199,7 @@ public void onText(CharSequence data) {
}
}
});
+ }
} else {
LOG.warning("Unhandled type: " + data);
}
