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+" 次监听到!"); } };
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· winform 绘制太阳,地球,月球 运作规律
· 上周热点回顾(3.3-3.9)