Skip to content

Commit

Permalink
Merge pull request #43 from lmorda/master
Browse files Browse the repository at this point in the history
Issue #40 - Unsubscribing a list of subscriptions causes ConcurrentModificationException
  • Loading branch information
NaikSoftware authored Apr 26, 2017
2 parents 14c468b + 9f7a618 commit 2045f63
Showing 1 changed file with 11 additions and 7 deletions.
18 changes: 11 additions & 7 deletions lib/src/main/java/ua/naiksoftware/stomp/client/StompClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
Expand All @@ -15,7 +16,6 @@
import rx.Subscriber;
import rx.Subscription;
import rx.observables.ConnectableObservable;
import rx.schedulers.Schedulers;
import ua.naiksoftware.stomp.ConnectionProvider;
import ua.naiksoftware.stomp.LifecycleEvent;
import ua.naiksoftware.stomp.StompHeader;
Expand Down Expand Up @@ -171,14 +171,18 @@ public Observable<StompMessage> topic(String destinationPath, List<StompHeader>
subscribersSet.add(subscriber);

}).doOnUnsubscribe(() -> {
for (String dest : mSubscribers.keySet()) {
Set<Subscriber<? super StompMessage>> set = mSubscribers.get(dest);
for (Subscriber<? super StompMessage> subscriber : set) {
Iterator<String> mapIterator = mSubscribers.keySet().iterator();
while (mapIterator.hasNext()) {
String destinationUrl = mapIterator.next();
Set<Subscriber<? super StompMessage>> set = mSubscribers.get(destinationUrl);
Iterator<Subscriber<? super StompMessage>> setIterator = set.iterator();
while (setIterator.hasNext()) {
Subscriber<? super StompMessage> subscriber = setIterator.next();
if (subscriber.isUnsubscribed()) {
set.remove(subscriber);
setIterator.remove();
if (set.size() < 1) {
mSubscribers.remove(dest);
unsubscribePath(dest).subscribe();
mapIterator.remove();
unsubscribePath(destinationUrl).subscribe();
}
}
}
Expand Down

0 comments on commit 2045f63

Please sign in to comment.