java链接zookeeper

服务器通信以及跨平台通信中会遇到zookeeper这个东西,至于什么是zookeeper,google it!

为了测试这块的东西,必须要懂zookeeper工作原理,因此,本人整理了一下zookeeper的一般用法便于理解,以及更好的测试~~


package com.kiven.test;

import java.io.IOException;
import java.util.concurrent.CountDownLatch;

import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.Watcher.Event.KeeperState;

public class Demo implements Watcher {
      
    //缓存时间 
    private static final int SESSION_TIME   = 2000;    
    protected ZooKeeper zooKeeper; 
    protected CountDownLatch countDownLatch= new CountDownLatch(1);
 
    public void connect(String hosts) throws IOException, InterruptedException{    
      zooKeeper = new ZooKeeper(hosts, SESSION_TIME, this);    
        countDownLatch.await();
    }
    /* (non-Javadoc)
    * @see org.apache.zookeeper.Watcher#process(org.apache.zookeeper.WatchedEvent)
    */ 
    public void process(WatchedEvent event) { 
      // TODO Auto-generated method stub 
      if(event.getState()==KeeperState. SyncConnected){ 
             countDownLatch.countDown(); 
        } 
    }
    public void close() throws InterruptedException{
      zooKeeper.close();    
    }
}
=================================================================================================================================

package com.kiven.test;

import java.util.List;

import org.apache.log4j.PropertyConfigurator;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.ZooDefs.Ids;

public class MyTest extends Demo{   
      /**
       * 
       * function: 创建持久态的znode,比支持多层创建.比如在创建/parent/child的情况下,无/parent.无法通过
       *@author cuiran
       *@createDate 2013 -01 -16 15:08:38
       *@param path
       *@param data
       *@throws KeeperException
       *@throws InterruptedException
       */ 
      public void create(String path, byte[] data) throws KeeperException, InterruptedException{ 
          /**
           * 此处采用的是CreateMode是PERSISTENT   表示The znode will not be automatically deleted upon client's disconnect.
           * EPHEMERAL 表示The znode will be deleted upon the client's disconnect.
           */  
          this. zooKeeper.create(path, data, Ids. OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); 
      } 
      /**
       * 
       * function: 获取节点信息
       *@author cuiran
       *@createDate 2013 -01 -16 15:17:22
       *@param path
       *@throws KeeperException
       *@throws InterruptedException
       */ 
      public void getChild(String path) throws KeeperException, InterruptedException{    
          try{ 
              List list= this. zooKeeper.getChildren(path, false); 
              if(list.isEmpty()){ 
                  System. out.println(path+ "中没有节点" );
              } else{ 
                  System. out.println(path+ "中存在节点" ); 
                  for(String child:list){
                        System. out.println( "节点为:"+child);
                  } 
              } 
          } catch (KeeperException.NoNodeException e) { 
              // TODO: handle exception 
               throw e;
      
          } 
      } 
       
      public byte[] getData(String path) throws KeeperException, InterruptedException {    
          return  this. zooKeeper.getData(path, false, null);    
      }   
      
      public static void main(String args[]){
            PropertyConfigurator. configure("log4j.properties");
             try {    
                  MyTest zkoperator = new MyTest();    
            zkoperator.connect( "172.16.3.9"); 
             
//          byte[] data = new byte[]{'a','b','c','d'};    
                
//          zkoperator.create("/root",null);    
//          System.out.println(Arrays.toString(zkoperator.getData("/root")));    
//              
//          zkoperator.create("/root/child1",data);    
//          System.out.println(Arrays.toString(zkoperator.getData("/root/child1")));    
//              
//          zkoperator.create("/root/child2",data);    
//          System.out.println(Arrays.toString(zkoperator.getData("/root/child2")));    
                
            String zktest= "ZooKeeper的Java API测试" ; 
            zkoperator.create( "/test", zktest.getBytes());
            zkoperator.create( "/test/test1", zktest.getBytes());
            System. out.println( "获取设置的信息:" +new String(zkoperator.getData("/test" ))); 
             
            System. out.println( "节点孩子信息:" );
            zkoperator.getChild( "/test");    
                
            zkoperator.close();    
        } catch (Exception e) {    
            e.printStackTrace();    
        } 
      }
}





你可能感兴趣的:(java链接zookeeper)