Example usage for org.apache.zookeeper Watcher Watcher

List of usage examples for org.apache.zookeeper Watcher Watcher

Introduction

In this page you can find the example usage for org.apache.zookeeper Watcher Watcher.

Prototype

Watcher

Source Link

Usage

From source file:com.linkedin.helix.AppTest.java

License:Apache License

@Test(enabled = false)
private static void testChroot() throws Exception {
    Watcher watcher = new Watcher() {
        @Override// w  w  w .j  a  v a  2  s.  c o m
        public void process(WatchedEvent event) {
            System.out.println("Event:" + event);
        }
    };
    ZooKeeper zk = new ZooKeeper("localhost:2181/foo", 6000, watcher);
    // uncommenting this line will not cause infinite connect/disconnect
    // zk.create("/", new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);

    zk.exists("/", true);
    System.out.println("Stop the server and restart it when you see this message");
    Thread.currentThread().join();
}

From source file:com.linkedin.helix.TestZkClientWrapper.java

License:Apache License

@Test()
void testSessioExpire() {
    IZkStateListener listener = new IZkStateListener() {

        @Override//from   w ww  . j  av  a2 s. c  om
        public void handleStateChanged(KeeperState state) throws Exception {
            System.out.println("In Old connection New state " + state);
        }

        @Override
        public void handleNewSession() throws Exception {
            System.out.println("In Old connection New session");
        }
    };
    _zkClient.subscribeStateChanges(listener);
    ZkConnection connection = ((ZkConnection) _zkClient.getConnection());
    ZooKeeper zookeeper = connection.getZookeeper();
    System.out.println("old sessionId= " + zookeeper.getSessionId());
    try {
        Watcher watcher = new Watcher() {
            @Override
            public void process(WatchedEvent event) {
                System.out.println("In New connection In process event:" + event);
            }
        };
        ZooKeeper newZookeeper = new ZooKeeper(connection.getServers(), zookeeper.getSessionTimeout(), watcher,
                zookeeper.getSessionId(), zookeeper.getSessionPasswd());
        Thread.sleep(3000);
        System.out.println("New sessionId= " + newZookeeper.getSessionId());
        Thread.sleep(3000);
        newZookeeper.close();
        Thread.sleep(10000);
        connection = ((ZkConnection) _zkClient.getConnection());
        zookeeper = connection.getZookeeper();
        System.out.println("After session expiry sessionId= " + zookeeper.getSessionId());
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

From source file:com.linkedin.helix.ZkUnitTestBase.java

License:Apache License

protected void simulateSessionExpiry(ZkConnection zkConnection) throws IOException, InterruptedException {
    ZooKeeper oldZookeeper = zkConnection.getZookeeper();
    LOG.info("Old sessionId = " + oldZookeeper.getSessionId());

    Watcher watcher = new Watcher() {
        @Override//  w w  w  .  jav a 2s .  c om
        public void process(WatchedEvent event) {
            LOG.info("In New connection, process event:" + event);
        }
    };

    ZooKeeper newZookeeper = new ZooKeeper(zkConnection.getServers(), oldZookeeper.getSessionTimeout(), watcher,
            oldZookeeper.getSessionId(), oldZookeeper.getSessionPasswd());
    LOG.info("New sessionId = " + newZookeeper.getSessionId());
    // Thread.sleep(3000);
    newZookeeper.close();
    Thread.sleep(10000);
    oldZookeeper = zkConnection.getZookeeper();
    LOG.info("After session expiry sessionId = " + oldZookeeper.getSessionId());
}

From source file:com.linkedin.helix.ZkUnitTestBase.java

License:Apache License

protected void simulateSessionExpiry(ZkClient zkClient) throws IOException, InterruptedException {
    IZkStateListener listener = new IZkStateListener() {
        @Override//from   w  w w  .  j ava2s . c o m
        public void handleStateChanged(KeeperState state) throws Exception {
            LOG.info("In Old connection, state changed:" + state);
        }

        @Override
        public void handleNewSession() throws Exception {
            LOG.info("In Old connection, new session");
        }
    };
    zkClient.subscribeStateChanges(listener);
    ZkConnection connection = ((ZkConnection) zkClient.getConnection());
    ZooKeeper oldZookeeper = connection.getZookeeper();
    LOG.info("Old sessionId = " + oldZookeeper.getSessionId());

    Watcher watcher = new Watcher() {
        @Override
        public void process(WatchedEvent event) {
            LOG.info("In New connection, process event:" + event);
        }
    };

    ZooKeeper newZookeeper = new ZooKeeper(connection.getServers(), oldZookeeper.getSessionTimeout(), watcher,
            oldZookeeper.getSessionId(), oldZookeeper.getSessionPasswd());
    LOG.info("New sessionId = " + newZookeeper.getSessionId());
    // Thread.sleep(3000);
    newZookeeper.close();
    Thread.sleep(10000);
    connection = (ZkConnection) zkClient.getConnection();
    oldZookeeper = connection.getZookeeper();
    LOG.info("After session expiry sessionId = " + oldZookeeper.getSessionId());
}

From source file:com.liveramp.hank.test.ZkTestCase.java

License:Apache License

@Before
public final void setUpZk() throws Exception {

    setupZkServer();/*from   w ww.ja  v a  2s. com*/

    final Object lock = new Object();
    final AtomicBoolean connected = new AtomicBoolean(false);

    zk = new ZooKeeperPlus("127.0.0.1:" + zkClientPort, 1000000, new Watcher() {
        @Override
        public void process(WatchedEvent event) {
            switch (event.getType()) {
            case None:
                if (event.getState() == KeeperState.SyncConnected) {
                    connected.set(true);
                    synchronized (lock) {
                        lock.notifyAll();
                    }
                }
            }
            LOG.debug(event.toString());
        }
    });

    synchronized (lock) {
        lock.wait(2000);
    }
    if (!connected.get()) {
        fail("timed out waiting for the zk client connection to come online!");
    }
    LOG.debug("session timeout: " + zk.getSessionTimeout());

    zk.deleteNodeRecursively(zkRoot);
    WaitUntil.orDie(() -> {
        try {
            return zk.exists(zkRoot, false) == null;
        } catch (KeeperException | InterruptedException e) {
            throw new RuntimeException(e);
        }
    });
    createNodeRecursively(zkRoot);
}

From source file:com.martin.zkedit.utils.ZKConnHelper.java

License:Open Source License

public ZooKeeper connect(String hosts, int sessionTimeout) throws IOException, InterruptedException {
    final CountDownLatch connectedSignal = new CountDownLatch(1);
    ZooKeeper zk = new ZooKeeper(hosts, sessionTimeout, new Watcher() {
        @Override/*  w w w .ja v a 2  s .  com*/
        public void process(WatchedEvent event) {
            if (event.getState() == Watcher.Event.KeeperState.SyncConnected) {
                connectedSignal.countDown();
            }
        }
    });
    connectedSignal.await();
    return zk;
}

From source file:com.navercorp.client.Client.java

License:Apache License

public Client(String connectionString, int timeoutms) throws IOException, InterruptedException {
    final CountDownLatch sync = new CountDownLatch(1);
    zk = new ZooKeeper(connectionString, timeoutms, new Watcher() {
        public void process(WatchedEvent event) {
            if (event.getType() == Event.EventType.None
                    && event.getState() == Event.KeeperState.SyncConnected) {
                sync.countDown();/*w  ww. ja  va 2s  .  c  om*/
            }
        }
    });

    if (sync.await(timeoutms, TimeUnit.MILLISECONDS) == false) {
        zk.close();
        throw new IOException("Connection to zookeeper timed out. connectionString: " + connectionString);
    }
}

From source file:com.navercorp.pinpoint.common.server.cluster.zookeeper.CuratorZookeeperClientTest.java

License:Apache License

private ZooKeeper createZookeeper() throws IOException {
    ZooKeeper zooKeeper = new ZooKeeper(ts.getConnectString(), 3000, new Watcher() {
        @Override// w  w w . j a  va 2  s.c  om
        public void process(WatchedEvent watchedEvent) {
            LOGGER.info("ZooKeeper process:{}", watchedEvent);
        }
    });

    return zooKeeper;
}

From source file:com.nearinfinity.blur.manager.clusterstatus.ZookeeperClusterStatus.java

License:Apache License

public ZookeeperClusterStatus(String connectionStr) throws IOException {
    this(new ZooKeeper(connectionStr, 30000, new Watcher() {
        @Override/*from   w  w w . j  a v  a 2  s .co  m*/
        public void process(WatchedEvent event) {

        }
    }));
}

From source file:com.nearinfinity.blur.manager.indexserver.BlurServerShutDown.java

License:Apache License

public void register(final BlurShutdown shutdown, ZooKeeper zooKeeper) {
    this.shutdown = shutdown;
    this.zooKeeper = zooKeeper;
    zooKeeper.register(new Watcher() {
        @Override//www .ja v a2  s.  c  o m
        public void process(WatchedEvent event) {
            KeeperState state = event.getState();
            if (state == KeeperState.Expired) {
                LOG.fatal("Zookeeper session has [" + state + "] server process shutting down.");
                shutdown.shutdown();
            }
        }
    });
}