码迷,mamicode.com
首页 > 其他好文 > 详细

模仿ZooKeeperMain编写的zookeeper客户端

时间:2016-06-19 22:45:36      阅读:168      评论:0      收藏:0      [点我收藏+]

标签:

public class App {
        private static final int SESSION_TIME   = 2000;
        protected ZooKeeper zk;
        protected String host = "";

        protected void connectToZK(String newHost) throws InterruptedException, IOException {
          if (zk != null && zk.getState().isAlive()) {
            zk.close();
          }
          host = newHost;
          zk = new ZooKeeper(host, SESSION_TIME, new MyWatcher());
        }
    
        public static void printMessage(String msg) {
          System.out.println(msg);
        }
    
      private class MyWatcher implements Watcher {
          public void process(WatchedEvent event) {
              App.printMessage(event.toString());
          }
      }
}

public class AppTest {
  public static void main(String[] args) throws Exception {
      App app = new App();
    app.connectToZK("192.168.1.92:2181");
    //添加一个scheme为ip的权限控制
    ArrayList<ACL> acl = new ArrayList<ACL>(Collections.singletonList(new ACL(Perms.ALL, new Id("ip","192.168.1.92"))));
    app.zk.create("/test", "test".getBytes(), acl, CreateMode.PERSISTENT);
       }
}

模仿ZooKeeperMain编写的zookeeper客户端

标签:

原文地址:http://www.cnblogs.com/wanshoushou/p/5598854.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!