mirror of
https://github.com/reiseburo/rx-curator
synced 2024-09-15 06:41:58 +00:00
Properly handle unsubscriptions and close our PathChildrenCache object out
This commit is contained in:
parent
a1c7147c5e
commit
10ada1646f
@ -6,6 +6,9 @@ import org.apache.curator.framework.recipes.cache.PathChildrenCacheEvent;
|
||||
import org.apache.curator.framework.recipes.cache.PathChildrenCacheListener;
|
||||
import rx.Observable;
|
||||
import rx.Subscriber;
|
||||
import rx.functions.Action0;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* PathChildren is an {@code Observable} which takes events from a {@code PathChildrenCache}
|
||||
@ -49,8 +52,7 @@ public class PathChildren {
|
||||
cache = new PathChildrenCache(curatorFramework, znodePath, true);
|
||||
try {
|
||||
cache.start(PathChildrenCache.StartMode.POST_INITIALIZED_EVENT);
|
||||
}
|
||||
catch (Exception ex) {
|
||||
} catch (Exception ex) {
|
||||
subscriber.onError(ex);
|
||||
}
|
||||
|
||||
@ -61,6 +63,19 @@ public class PathChildren {
|
||||
}
|
||||
});
|
||||
}
|
||||
}).doOnUnsubscribe(new Action0() {
|
||||
@Override
|
||||
public void call() {
|
||||
/* properly close out our cache object when our subscriber leaves */
|
||||
if (cache instanceof PathChildrenCache) {
|
||||
try {
|
||||
cache.close();
|
||||
} catch (IOException exception) {
|
||||
/* Swallow, we don't need this exception */
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
@ -58,12 +58,14 @@ class PathChildrenIntegrationSpec extends Specification {
|
||||
def "an initialization event should be received"() {
|
||||
given:
|
||||
boolean received = false
|
||||
Subscription subscription
|
||||
Observable<PathChildrenCacheEvent> observable = PathChildren.with(curator).watch('/')
|
||||
|
||||
when:
|
||||
observable.subscribe { PathChildrenCacheEvent ev ->
|
||||
subscription = observable.subscribe { PathChildrenCacheEvent ev ->
|
||||
if (ev.type == PathChildrenCacheEvent.Type.INITIALIZED) {
|
||||
received = true
|
||||
subscription.unsubscribe()
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user