JAvaリンクzookeeper

4926 ワード

サーバー通信やプラットフォーム間通信で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();    
        } 
      }
}