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   潮流教父孙笑川  阅读(61)  评论(0编辑  收藏  举报

(评论功能已被禁用)
编辑推荐:
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· winform 绘制太阳,地球,月球 运作规律
· 上周热点回顾(3.3-3.9)

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示