Examples of ZooKeeper


Examples of org.apache.zookeeper.ZooKeeper

    public static void doAction(int client) throws Exception {
        String host1 = "192.168.1.201:2181";
        String host2 = "192.168.1.201:2182";
        String host3 = "192.168.1.201:2183";

        ZooKeeper zk = null;
        switch (client) {
        case 1:
            zk = connection(host1);
            initQueue(zk);
            joinQueue(zk, 1);
View Full Code Here

Examples of org.apache.zookeeper.ZooKeeper

        }
    }

    // 创建一个与服务器的连接
    public static ZooKeeper connection(String host) throws IOException {
        ZooKeeper zk = new ZooKeeper(host, 60000, new Watcher() {
            // 监控所有被触发的事件
            public void process(WatchedEvent event) {
                if (event.getType() == Event.EventType.NodeCreated && event.getPath().equals("/queue/start")) {
                    System.out.println("Queue has Completed.Finish testing!!!");
                }
View Full Code Here

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

Examples of org.apache.zookeeper.ZooKeeper

  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

Examples of org.apache.zookeeper.ZooKeeper

     */
    @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

Examples of org.apache.zookeeper.ZooKeeper

    "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

Examples of org.apache.zookeeper.ZooKeeper

    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

Examples of org.apache.zookeeper.ZooKeeper

        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

Examples of org.apache.zookeeper.ZooKeeper

     */
    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

Examples of org.apache.zookeeper.ZooKeeper

                {
                    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
TOP
Copyright © 2018 www.massapi.com. 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.