码迷,mamicode.com
首页 > 其他好文 > 详细

Zookeeper 重连机制

时间:2018-11-05 23:38:25      阅读:188      评论:0      收藏:0      [点我收藏+]

标签:public   get   pre   event   ati   机制   nts   time   dev   

Zookeeper 重连机制

public class ZKConnectSessionWatcher implements Watcher {


    public final static String zkServerPath = "10.10.10.10:2181";

    public final  static  int timeout = 5000;

    public static void main(String[] args) throws  Exception {
        ZooKeeper zk = new ZooKeeper(zkServerPath, timeout, new ZKConnectSessionWatcher());
        long sessionId = zk.getSessionId();
        byte[] sessionPassword = zk.getSessionPasswd();

        log("客户端开始连接Zookeeper服务器...");
        log("连接状态:" + zk.getState());

        new Thread().sleep(1000);
        log("连接状态:" + zk.getState());

        log("开始会话重连。。。");

        ZooKeeper zkSession = new ZooKeeper(zkServerPath, timeout, new ZKConnectSessionWatcher(), sessionId, sessionPassword);
        log("重新连接zkSession:" + zkSession.getState());
        new Thread().sleep(1000);
        log("重新连接zkSession:" + zkSession.getState());
    }

    public void process(WatchedEvent event) {
        System.out.println("接收到watch通知:" + event.toString());
    }

    private static void log(String msg){
        System.out.println(msg);
    }

  

Zookeeper 重连机制

标签:public   get   pre   event   ati   机制   nts   time   dev   

原文地址:https://www.cnblogs.com/linlf03/p/9912201.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!