Package org.apache.zookeeper

Examples of org.apache.zookeeper.ZooKeeper


public class BasicDemo1 {

    public static void main(String[] args) throws IOException, KeeperException, InterruptedException {
        // 创建一个与服务器的连接
        ZooKeeper zk = new ZooKeeper("192.168.1.201:2181", 60000, new Watcher() {
            // 监控所有被触发的事件
            public void process(WatchedEvent event) {
                System.out.println("EVENT:" + event.getType());
            }
        });

        // 查看根节点
        System.out.println("ls / => " + zk.getChildren("/", true));

        // 创建一个目录节点
        if (zk.exists("/node", true) == null) {
            zk.create("/node", "conan".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
            System.out.println("create /node conan");
            // 查看/node节点数据
            System.out.println("get /node => " + new String(zk.getData("/node", false, null)));
            // 查看根节点
            System.out.println("ls / => " + zk.getChildren("/", true));
        }

        // 创建一个子目录节点
        if (zk.exists("/node/sub1", true) == null) {
            zk.create("/node/sub1", "sub1".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
            System.out.println("create /node/sub1 sub1");
            // 查看node节点
            System.out.println("ls /node => " + zk.getChildren("/node", true));
        }

        // 修改节点数据
        if (zk.exists("/node", true) != null) {
            zk.setData("/node", "changed".getBytes(), -1);
            // 查看/node节点数据
            System.out.println("get /node => " + new String(zk.getData("/node", false, null)));
        }

        // 删除节点
        if (zk.exists("/node/sub1", true) != null) {
            zk.delete("/node/sub1", -1);
            zk.delete("/node", -1);
            // 查看根节点
            System.out.println("ls / => " + zk.getChildren("/", true));
        }

        // 关闭连接
        zk.close();
    }
View Full Code Here


  private ServerWatcher modelWatcher = new ServerWatcher();

  public Server(int port) throws TTransportException, IOException,
      InterruptedException, KeeperException {
    zk = new ZooKeeper("localhost", 2181, new Watcher() {
      @Override
      public void process(WatchedEvent watchedEvent) {
        // ignore
      }
    });
View Full Code Here

     */
    @Override
    public void process(WatchedEvent watchedEvent) {
      if (zk == null) {
        try {
          zk = new ZooKeeper("localhost", 2181, null);
        } catch (IOException e) {
          zk = null;
          return;
        }
      }
View Full Code Here

    "talk.politics.mideast",
    "talk.politics.misc",
    "talk.religion.misc");

  public static void main(String[] args) throws TException, IOException, InterruptedException, KeeperException {
    ZooKeeper zk = new ZooKeeper("localhost", 2181, new Watcher() {
      @Override
      public void process(WatchedEvent watchedEvent) {
        // ignore
      }
    });
    List<String> servers = zk.getChildren(Server.ZK_CURRENT_SERVERS, false, null);
    if (servers.size() == 0) {
      throw new IllegalStateException("No servers to query");
    }
    String hostname = servers.get(new Random().nextInt(servers.size()));
    System.out.printf("host = %s\n", hostname);
View Full Code Here

    protected ZookeeperSession(final String connectString, final int sessionTimeout) throws IOException
    {
        this.connectString = connectString;
        this.sessionTimeout = sessionTimeout;
        this.zkref = new AtomicReference<ZooKeeper>();
        final ZooKeeper newZk = makeZooKeeperClient(connectString, sessionTimeout);
        if (newZk != null) setNewZookeeper(newZk);
    }
View Full Code Here

        if (logger.isTraceEnabled()) logger.trace("Disrupting Zookeeper session by closing the session.");
        if (zkref != null)
        {
            try
            {
                final ZooKeeper cur = zkref.get();
                cur.close();
            } catch (final Throwable th)
            {
                logger.error("Failed disrupting ZookeeperSession", th);
            }
        }
View Full Code Here

     */
    protected ZooKeeper makeZooKeeperClient(final String connectString, final int sessionTimeout) throws IOException
    {
        if (logger.isTraceEnabled()) logger.trace("creating new ZooKeeper client connection from scratch.");

        return new ZooKeeper(connectString, sessionTimeout, new ZkWatcher());
    }
View Full Code Here

                {
                    registeredWatchers.add(wp);
                }
            }

            final ZooKeeper cur = zkref.get();
            try
            {
                return callee.call(cur, path, wp, userdata);
            } catch (final KeeperException.NodeExistsException e)
            {
View Full Code Here

                    public void run()
                    {
                        if (logger.isTraceEnabled())
                        logger.trace("Executing ZooKeeper client reset.");

                        ZooKeeper newZk = null;
                        try
                        {
                            boolean forceRebuild = false;
                            if (failedInstance.getState().isAlive())
                            {
View Full Code Here

    {
        if (logger.isTraceEnabled()) logger.trace("reestablished connection to " + connectString);

        if (isRunning)
        {
            final ZooKeeper last = zkref.getAndSet(newZk);
            if (last != null && last != newZk)
            {
                try {
                    last.close();
                } catch (final Throwable th) {}
            }
        }
        else
        {
View Full Code Here

TOP

Related Classes of org.apache.zookeeper.ZooKeeper

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.