session 和 watch ,监听目标结点的父节点,已达到重复监听的目的

3.6. 会话(Session)

Session 可以看作是 ZooKeeper 服务器与客户端的之间的一个 TCP 长连接,通过这个连接,客户端能够通过心跳检测与服务器保持有效的会话,也能够向 ZooKeeper 服务器发送请求并接受响应,同时还能够通过该连接接收来自服务器的 Watcher 事件通知。

Session 有一个属性叫做:sessionTimeout ,sessionTimeout 代表会话的超时时间。当由于服务器压力太大、网络故障或是客户端主动断开连接等各种原因导致客户端连接断开时,只要在sessionTimeout规定的时间内能够重新连接上集群中任意一台服务器,那么之前创建的会话仍然有效。

另外,在为客户端创建会话之前,服务端首先会为每个客户端都分配一个 sessionID。由于 sessionID是 ZooKeeper 会话的一个重要标识,许多与会话相关的运行机制都是基于这个 sessionID 的,因此,无论是哪台服务器为客户端分配的 sessionID,都务必保证全局唯一。

 

 

实现watcher接口

public interface Watcher {
    void process(WatchedEvent var1);

    public interface Event {
        public static enum EventType {
            None(-1),
            NodeCreated(1),
            NodeDeleted(2),
            NodeDataChanged(3),
            NodeChildrenChanged(4);

            private final int intValue;

            private EventType(int intValue) {
                this.intValue = intValue;
            }

            public int getIntValue() {
                return this.intValue;
            }

            public static Watcher.Event.EventType fromInt(int intValue) {
                switch(intValue) {
                case -1:
                    return None;
                case 0:
                default:
                    throw new RuntimeException("Invalid integer value for conversion to EventType");
                case 1:
                    return NodeCreated;
                case 2:
                    return NodeDeleted;
                case 3:
                    return NodeDataChanged;
                case 4:
                    return NodeChildrenChanged;
                }
            }
        }

        public static enum KeeperState {
            /** @deprecated */
            @Deprecated
            Unknown(-1),
            Disconnected(0),
            /** @deprecated */
            @Deprecated
            NoSyncConnected(1),
            SyncConnected(3),
            AuthFailed(4),
            ConnectedReadOnly(5),
            SaslAuthenticated(6),
            Expired(-112);

            private final int intValue;

            private KeeperState(int intValue) {
                this.intValue = intValue;
            }

            public int getIntValue() {
                return this.intValue;
            }

            public static Watcher.Event.KeeperState fromInt(int intValue) {
                switch(intValue) {
                case -112:
                    return Expired;
                case -1:
                    return Unknown;
                case 0:
                    return Disconnected;
                case 1:
                    return NoSyncConnected;
                case 3:
                    return SyncConnected;
                case 4:
                    return AuthFailed;
                case 5:
                    return ConnectedReadOnly;
                case 6:
                    return SaslAuthenticated;
                default:
                    throw new RuntimeException("Invalid integer value for conversion to KeeperState");
                }
            }
        }
    }
}
zk = new ZooKeeper(address, sessionTimeout, new Watcher() {
                // 监控所有被触发的事件
                public void process(WatchedEvent event) {
                    if (event.getType() == null || "".equals(event.getType())) {
                        return;
                    }
                    System.out.println("已经触发了" + event.getType() + "事件!");
                }
            });

 

public int watchCount = 0;  //记录监听次数

    ZooKeeper zkClient = null;

    String createNodeName = "/apiTest10000000007"; //记录创建的节点名称

    public static void main(String[] args) {

    }
    /**定义watch对象*/
    private Watcher watcher = new Watcher() {

        @Override
        public void process(WatchedEvent watchedEvent) {
            System.out.println("获得监听事件,path:" + watchedEvent.getPath() + ";state:" + watchedEvent.getState() + ";type:" + watchedEvent.getType());

            //循环重复监听
            try {
                zkClient.exists(watchedEvent.getPath(), true);
            } catch (KeeperException e) {
                e.printStackTrace();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            watchCount++;
            System.out.println("第 "+watchCount+" 次监听到!");
        }
    };

 

posted on 2020-10-25 14:31  潮流教父孙笑川  阅读(67)  评论(0)    收藏  举报

导航