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{   
      /**
       * 
       *<b> function:</b> 创建持久态的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); 
      } 
      /**
       * 
       *<b> function:</b> 获取节点信息
       *@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<String> 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();    
        } 
      }
}





  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值