use of org.apache.zookeeper.WatchedEvent in project zookeeper by apache.
the class DataTree method setWatches.
public void setWatches(long relativeZxid, List<String> dataWatches, List<String> existWatches, List<String> childWatches, List<String> persistentWatches, List<String> persistentRecursiveWatches, Watcher watcher) {
for (String path : dataWatches) {
DataNode node = getNode(path);
WatchedEvent e = null;
if (node == null) {
watcher.process(new WatchedEvent(EventType.NodeDeleted, KeeperState.SyncConnected, path));
} else if (node.stat.getMzxid() > relativeZxid) {
watcher.process(new WatchedEvent(EventType.NodeDataChanged, KeeperState.SyncConnected, path));
} else {
this.dataWatches.addWatch(path, watcher);
}
}
for (String path : existWatches) {
DataNode node = getNode(path);
if (node != null) {
watcher.process(new WatchedEvent(EventType.NodeCreated, KeeperState.SyncConnected, path));
} else {
this.dataWatches.addWatch(path, watcher);
}
}
for (String path : childWatches) {
DataNode node = getNode(path);
if (node == null) {
watcher.process(new WatchedEvent(EventType.NodeDeleted, KeeperState.SyncConnected, path));
} else if (node.stat.getPzxid() > relativeZxid) {
watcher.process(new WatchedEvent(EventType.NodeChildrenChanged, KeeperState.SyncConnected, path));
} else {
this.childWatches.addWatch(path, watcher);
}
}
for (String path : persistentWatches) {
this.childWatches.addWatch(path, watcher, WatcherMode.PERSISTENT);
this.dataWatches.addWatch(path, watcher, WatcherMode.PERSISTENT);
}
for (String path : persistentRecursiveWatches) {
this.childWatches.addWatch(path, watcher, WatcherMode.PERSISTENT_RECURSIVE);
this.dataWatches.addWatch(path, watcher, WatcherMode.PERSISTENT_RECURSIVE);
}
}
use of org.apache.zookeeper.WatchedEvent in project zookeeper by apache.
the class PersistentWatcherTest method assertEvent.
private void assertEvent(BlockingQueue<WatchedEvent> events, Watcher.Event.EventType eventType, String path) throws InterruptedException {
WatchedEvent event = events.poll(5, TimeUnit.SECONDS);
assertNotNull(event);
assertEquals(eventType, event.getType());
assertEquals(path, event.getPath());
}
use of org.apache.zookeeper.WatchedEvent in project zookeeper by apache.
the class PersistentWatcherTest method testBasicAsync.
@Test
public void testBasicAsync() throws IOException, InterruptedException, KeeperException {
CountdownWatcher watcher = new CountdownWatcher() {
@Override
public synchronized void process(WatchedEvent event) {
super.process(event);
events.add(event);
}
};
try (ZooKeeper zk = createClient(watcher, hostPort)) {
final CountDownLatch latch = new CountDownLatch(1);
AsyncCallback.VoidCallback cb = (rc, path, ctx) -> {
if (rc == KeeperException.Code.OK.intValue()) {
latch.countDown();
}
};
zk.addWatch("/a/b", persistentWatcher, PERSISTENT, cb, null);
assertTrue(latch.await(5, TimeUnit.SECONDS));
// clear any events added during client connection
events.clear();
internalTestBasic(zk);
}
}
use of org.apache.zookeeper.WatchedEvent in project zookeeper by apache.
the class PersistentWatcherTest method testDefaultWatcher.
@Test
public void testDefaultWatcher() throws IOException, InterruptedException, KeeperException {
CountdownWatcher watcher = new CountdownWatcher() {
@Override
public synchronized void process(WatchedEvent event) {
super.process(event);
events.add(event);
}
};
try (ZooKeeper zk = createClient(watcher, hostPort)) {
zk.addWatch("/a/b", PERSISTENT);
// clear any events added during client connection
events.clear();
internalTestBasic(zk);
}
}
use of org.apache.zookeeper.WatchedEvent in project zookeeper by apache.
the class WatchedEventTest method testCreatingWatchedEventFromInvalidWrapper.
@Test
public void testCreatingWatchedEventFromInvalidWrapper() {
try {
WatcherEvent wep = new WatcherEvent(-2342, -252352, "foo");
new WatchedEvent(wep);
fail("Was able to create WatchedEvent from bad wrapper");
} catch (RuntimeException re) {
// we're good
}
}
Aggregations