zookeeperAPI的使用(1)创建会话

zookeeper 创建连接 (zookeeper API四种创建连接)

/**
 * zookeeper 创建连接有四种方式
 * 
 * @author Administrator
 *
 */

/*
 * 参数介绍 1. connectString zookeeper服务器列表, 
 *  a. host:port(如:192.168.1:2181)
 *  b.host:port/path(如:192.168.1:2181/zk) 之后所有操作都会基于这个路径
 * 
 * 2. sessionTimeout 回话超时时间(毫秒)
 * 
 * 3. watcher 事件通知处理(可以为null,表明不需要watcher时间通知)
 * 
 * 4. canBeReadOnly 用于标识当前回话是否"readl-only"模式(即当zookeeper发生故障时,依然能够提供读服务)
 * 
 * 5. sessionId和 sessionPasswd 代表回话ID和回话秘钥,这两个参数唯一确定一个回话,同时客户端使用这两个参数
 * 可以实现客户端回话服用,从而达到恢复会话的效果。
 * 
 */

public class Create implements Watcher {

    CountDownLatch CountDownLatch = new CountDownLatch(1);

    /**
     * 1. ZooKeeper.ZooKeeper(String connectString, int sessionTimeout, Watcher
     * watcher)
     * 
     * @throws IOException
     * @throws InterruptedException
     */
    @Test
    public void testCreate1() throws IOException, InterruptedException {
        ZooKeeper zk = new ZooKeeper(Connection.CONNECTION, 5000, new Create());
        System.out.println(zk);
        CountDownLatch.await();
    }

    /**
     * 2. ZooKeeper(String connectString, int sessionTimeout, Watcher watcher,
     * boolean canBeReadOnly) watcher)
     * 
     * @throws IOException
     * @throws InterruptedException
     */
    @Test
    public void testCreate2() throws IOException, InterruptedException {
        ZooKeeper zk = new ZooKeeper(Connection.CONNECTION, 5000, new Create(), true);
        System.out.println(zk);
        CountDownLatch.await();
    }

    /**
     * 3. ZooKeeper(String connectString, int sessionTimeout, Watcher watcher, long
     * sessionId, byte[] sessionPasswd)
     * 
     * @throws IOException
     * @throws InterruptedException
     */
    @Test
    public void testCreate3() throws IOException, InterruptedException {
        ZooKeeper zk = new ZooKeeper(Connection.CONNECTION, 5000, new Create(), 111l, "root".getBytes());
        System.out.println(zk);
        CountDownLatch.await();
    }

    /**
     * 4. ZooKeeper(String connectString, int sessionTimeout, Watcher watcher, long
     * sessionId, byte[] sessionPasswd, boolean canBeReadOnly)
     * 
     * @throws IOException
     * @throws InterruptedException
     */
    @Test
    public void testCreate4() throws IOException, InterruptedException {
        ZooKeeper zk = new ZooKeeper(Connection.CONNECTION, 5000, new Create(), 111l, "root".getBytes(), true);
        System.out.println(zk);
        CountDownLatch.await();

    }

    @Override
    public void process(WatchedEvent event) {
        System.out.println("processing.............");
        System.out.println(event);
        CountDownLatch.countDown();
    }

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值