Search in sources :

Example 56 with WatchedEvent

use of org.apache.zookeeper.WatchedEvent in project curator by Netflix.

the class CuratorZKClientBridge method connect.

@Override
public void connect(final Watcher watcher) {
    if (watcher != null) {
        CuratorListener localListener = new CuratorListener() {

            @Override
            public void eventReceived(CuratorFramework client, CuratorEvent event) throws Exception {
                if (event.getWatchedEvent() != null) {
                    watcher.process(event.getWatchedEvent());
                }
            }
        };
        curator.getCuratorListenable().addListener(localListener);
        listener.set(localListener);
        try {
            BackgroundCallback callback = new BackgroundCallback() {

                @Override
                public void processResult(CuratorFramework client, CuratorEvent event) throws Exception {
                    WatchedEvent fakeEvent = new WatchedEvent(Watcher.Event.EventType.None, curator.getZookeeperClient().isConnected() ? Watcher.Event.KeeperState.SyncConnected : Watcher.Event.KeeperState.Disconnected, null);
                    watcher.process(fakeEvent);
                }
            };
            curator.checkExists().inBackground(callback).forPath("/foo");
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
Also used : WatchedEvent(org.apache.zookeeper.WatchedEvent) CuratorFramework(org.apache.curator.framework.CuratorFramework) CuratorListener(org.apache.curator.framework.api.CuratorListener) CuratorEvent(org.apache.curator.framework.api.CuratorEvent) BackgroundCallback(org.apache.curator.framework.api.BackgroundCallback) KeeperException(org.apache.zookeeper.KeeperException)

Example 57 with WatchedEvent

use of org.apache.zookeeper.WatchedEvent in project yyl_example by Relucent.

the class Test1 method main.

public static void main(String[] args) throws Exception {
    ZooKeeper zk = new ZooKeeper("localhost:2181", 1000, new Watcher() {

        // 监控所有被触发的事件
        public void process(WatchedEvent event) {
            System.out.println("已经触发了" + event.getType() + "事件!");
        }
    });
    // 创建一个目录节点
    zk.create("/testRootPath", "testRootData".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
    // 创建一个子目录节点
    zk.create("/testRootPath/testChildPathOne", "testChildDataOne".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
    System.out.println(new String(zk.getData("/testRootPath", false, null)));
    // 取出子目录节点列表
    System.out.println(zk.getChildren("/testRootPath", true));
    // 修改子目录节点数据
    zk.setData("/testRootPath/testChildPathOne", "modifyChildDataOne".getBytes(), -1);
    System.out.println("目录节点状态:[" + zk.exists("/testRootPath", true) + "]");
    // 创建另外一个子目录节点
    zk.create("/testRootPath/testChildPathTwo", "testChildDataTwo".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
    System.out.println(new String(zk.getData("/testRootPath/testChildPathTwo", true, null)));
    // 删除子目录节点
    zk.delete("/testRootPath/testChildPathTwo", -1);
    zk.delete("/testRootPath/testChildPathOne", -1);
    // 删除父目录节点
    zk.delete("/testRootPath", -1);
    // 关闭连接
    zk.close();
}
Also used : WatchedEvent(org.apache.zookeeper.WatchedEvent) ZooKeeper(org.apache.zookeeper.ZooKeeper) Watcher(org.apache.zookeeper.Watcher)

Example 58 with WatchedEvent

use of org.apache.zookeeper.WatchedEvent in project yyl_example by Relucent.

the class Test3 method main.

public static void main(String[] args) {
    ZooKeeper zk = null;
    try {
        zk = new ZooKeeper("localhost:2181", 1000, null);
        zk.exists("/test", new Watcher() {

            @Override
            public void process(WatchedEvent e) {
                System.out.println(e);
            }
        });
        Thread.sleep(60 * 1000);
    } catch (Exception e) {
        e.printStackTrace();
        try {
            if (zk != null) {
                zk.close();
            }
        } catch (Exception e1) {
        }
    }
}
Also used : WatchedEvent(org.apache.zookeeper.WatchedEvent) ZooKeeper(org.apache.zookeeper.ZooKeeper) Watcher(org.apache.zookeeper.Watcher)

Example 59 with WatchedEvent

use of org.apache.zookeeper.WatchedEvent in project distributedlog by twitter.

the class LocalDLMEmulator method connectZooKeeper.

public static ZooKeeper connectZooKeeper(String zkHost, int zkPort, int zkTimeoutSec) throws IOException, KeeperException, InterruptedException {
    final CountDownLatch latch = new CountDownLatch(1);
    final String zkHostPort = zkHost + ":" + zkPort;
    ZooKeeper zkc = new ZooKeeper(zkHostPort, zkTimeoutSec * 1000, new Watcher() {

        public void process(WatchedEvent event) {
            if (event.getState() == Event.KeeperState.SyncConnected) {
                latch.countDown();
            }
        }
    });
    if (!latch.await(zkTimeoutSec, TimeUnit.SECONDS)) {
        throw new IOException("Zookeeper took too long to connect");
    }
    return zkc;
}
Also used : WatchedEvent(org.apache.zookeeper.WatchedEvent) ZooKeeper(org.apache.zookeeper.ZooKeeper) Watcher(org.apache.zookeeper.Watcher) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 60 with WatchedEvent

use of org.apache.zookeeper.WatchedEvent in project distributedlog by twitter.

the class ZooKeeperClient method registerExpirationHandler.

/**
 * Clients that need to re-establish state after session expiration can register an
 * {@code onExpired} command to execute.
 *
 * @param onExpired the {@code Command} to register
 * @return the new {@link Watcher} which can later be passed to {@link #unregister} for
 *         removal.
 */
public Watcher registerExpirationHandler(final ZooKeeperSessionExpireNotifier onExpired) {
    Watcher watcher = new Watcher() {

        @Override
        public void process(WatchedEvent event) {
            if (event.getType() == EventType.None && event.getState() == KeeperState.Expired) {
                try {
                    onExpired.notifySessionExpired();
                } catch (Exception exc) {
                // do nothing
                }
            }
        }
    };
    register(watcher);
    return watcher;
}
Also used : WatchedEvent(org.apache.zookeeper.WatchedEvent) Watcher(org.apache.zookeeper.Watcher) TimeoutException(java.util.concurrent.TimeoutException) KeeperException(org.apache.zookeeper.KeeperException) IOException(java.io.IOException)

Aggregations

WatchedEvent (org.apache.zookeeper.WatchedEvent)194 Watcher (org.apache.zookeeper.Watcher)123 ZooKeeper (org.apache.zookeeper.ZooKeeper)67 CountDownLatch (java.util.concurrent.CountDownLatch)64 KeeperException (org.apache.zookeeper.KeeperException)56 Test (org.junit.Test)38 Stat (org.apache.zookeeper.data.Stat)35 IOException (java.io.IOException)31 Test (org.testng.annotations.Test)27 Test (org.junit.jupiter.api.Test)18 CuratorFramework (org.apache.curator.framework.CuratorFramework)14 AsyncCallback (org.apache.zookeeper.AsyncCallback)14 List (java.util.List)10 KeeperState (org.apache.zookeeper.Watcher.Event.KeeperState)10 Set (java.util.Set)7 TimeoutException (java.util.concurrent.TimeoutException)7 HashSet (java.util.HashSet)6 CompletableFuture (java.util.concurrent.CompletableFuture)6 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)6 RetryOneTime (org.apache.curator.retry.RetryOneTime)6