zeekeeper重连的代码

zeekeeper重连的代码
  1. private void buildZK() {  
  2.         System.out.println("Build zk client");  
  3.         try {  
  4.             zk = new ZooKeeper(zookeeperConnectionString, 10000, this);  
  5.             Stat s = zk.exists(rootPath, false);  
  6.             if (s == null) {  
  7.                 zk.create(rootPath, new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);  
  8.                 zk.create(rootPath + "/ELECTION", new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);  
  9.             }  
  10.             String value = zk.create(rootPath + "/ELECTION/n_", hostAddress, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL_SEQUENTIAL);  
  11.         } catch (Exception e) {  
  12.             e.printStackTrace();  
  13.             System.err.println("Error connect to zoo keeper");  
  14.         }  
  15.     }  
  16.   
  17.   
  18.     public void process(WatchedEvent event) {  
  19.         System.out.println(event);  
  20.         if (event.getState() == Event.KeeperState.Disconnected || event.getState() == Event.KeeperState.Expired) {  
  21.             System.out.println("Zookeeper connection timeout.");  
  22.             buildZK();  
  23.         }  
  24.   
  25.     }  
  26.  

你可能感兴趣的:(zeekeeper重连的代码)