zookeeper中如何实现事件监听Watcher
发表于:2025-11-08 作者:千家信息网编辑
千家信息网最后更新 2025年11月08日,小编给大家分享一下zookeeper中如何实现事件监听Watcher,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!Watc
千家信息网最后更新 2025年11月08日zookeeper中如何实现事件监听Watcher
小编给大家分享一下zookeeper中如何实现事件监听Watcher,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!
Watcher是zookeeper的事件监听机制,今天我们来看看Watcher类的代码都包含了什么内容?
Watcher
Watcher是一个接口,定义了process方法,需要子类实现。其代表了实现Watcher接口时必须实现的的方法,即定义进行处理,WatchedEvent表示观察的事件。
abstract public void process(WatchedEvent event);
内部类
1、Event接口
表示事件代表的状态,其包含了KeeperState和EventType两个内部枚举类。
KeeperState
KeeperState是一个枚举类,其定义了在事件发生时Zookeeper所处的各种状态,其还定义了一个从整型值返回对应状态的方法fromInt。
@InterfaceAudience.Public public interface Event { /** * Enumeration of states the ZooKeeper may be at the event */ @InterfaceAudience.Public public enum KeeperState { /** Unused, this state is never generated by the server */ //未知状态,服务器不再产生此状态 @Deprecated Unknown (-1), /** The client is in the disconnected state - it is not connected * to any server in the ensemble. */ //断开 Disconnected (0), /** Unused, this state is never generated by the server */ //未同步连接,不再使用,服务器不会产生此状态 @Deprecated NoSyncConnected (1), /** The client is in the connected state - it is connected * to a server in the ensemble (one of the servers specified * in the host connection parameter during ZooKeeper client * creation). */ //同步连接状态 SyncConnected (3), /** * Auth failed state */ //认证失败状态 AuthFailed (4), /** * The client is connected to a read-only server, that is the * server which is not currently connected to the majority. * The only operations allowed after receiving this state is * read operations. * This state is generated for read-only clients only since * read/write clients aren't allowed to connect to r/o servers. */ //只读连接状态 ConnectedReadOnly (5), /** * SaslAuthenticated: used to notify clients that they are SASL-authenticated, * so that they can perform Zookeeper actions with their SASL-authorized permissions. */ //SASL认证通过状态 SaslAuthenticated(6), /** The serving cluster has expired this session. The ZooKeeper * client connection (the session) is no longer valid. You must * create a new client connection (instantiate a new ZooKeeper * instance) if you with to access the ensemble. */ //过期状态 Expired (-112), /** * The client has been closed. This state is never generated by * the server, but is generated locally when a client calls * {@link ZooKeeper#close()} or {@link ZooKeeper#close(int)} */ //关闭 Closed (7); //代表状态的整型值 private final int intValue; // Integer representation of value // for sending over wire KeeperState(int intValue) { this.intValue = intValue; } public int getIntValue() { return intValue; } //从整型构造相应的状态 public static KeeperState fromInt(int intValue) { switch(intValue) { case -1: return KeeperState.Unknown; case 0: return KeeperState.Disconnected; case 1: return KeeperState.NoSyncConnected; case 3: return KeeperState.SyncConnected; case 4: return KeeperState.AuthFailed; case 5: return KeeperState.ConnectedReadOnly; case 6: return KeeperState.SaslAuthenticated; case -112: return KeeperState.Expired; case 7: return KeeperState.Closed; default: throw new RuntimeException("Invalid integer value for conversion to KeeperState"); } } }EventType
EventType是一个枚举类,其定义了事件的类型(如创建节点、删除节点等事件),同时,其还定义了一个从整型值返回对应事件类型的方法fromInt。
@InterfaceAudience.Public public enum EventType { //无 None (-1), //结点创建 NodeCreated (1), //结点删除 NodeDeleted (2), //结点数据变化 NodeDataChanged (3), //子结点变化 NodeChildrenChanged (4), //监听移除 DataWatchRemoved (5), //子结点监听移除 ChildWatchRemoved (6); private final int intValue; // Integer representation of value // for sending over wire EventType(int intValue) { this.intValue = intValue; } public int getIntValue() { return intValue; } //从整型构造相应的事件 public static EventType fromInt(int intValue) { switch(intValue) { case -1: return EventType.None; case 1: return EventType.NodeCreated; case 2: return EventType.NodeDeleted; case 3: return EventType.NodeDataChanged; case 4: return EventType.NodeChildrenChanged; case 5: return EventType.DataWatchRemoved; case 6: return EventType.ChildWatchRemoved; default: throw new RuntimeException("Invalid integer value for conversion to EventType"); } } }2、枚举类WatcherType
监听器类型枚举
@InterfaceAudience.Public public enum WatcherType { //子监听器 Children(1), //数据监听 Data(2), //任意 Any(3); // Integer representation of value private final int intValue; private WatcherType(int intValue) { this.intValue = intValue; } public int getIntValue() { return intValue; } //整数到类型的转换 public static WatcherType fromInt(int intValue) { switch (intValue) { case 1: return WatcherType.Children; case 2: return WatcherType.Data; case 3: return WatcherType.Any; default: throw new RuntimeException( "Invalid integer value for conversion to WatcherType"); } } }WatchedEvent
/** * Create a WatchedEvent with specified type, state and path */ public WatchedEvent(EventType eventType, KeeperState keeperState, String path) { this.keeperState = keeperState; this.eventType = eventType; this.path = path; }WatchedEvent类包含了三个属性,分别代表事件发生时Zookeeper的状态、事件类型和发生事件所涉及的节点路径。
以上是"zookeeper中如何实现事件监听Watcher"这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注行业资讯频道!
状态
事件
监听
类型
结点
代表
方法
内容
接口
篇文章
节点
数据
服务器
监听器
生时
变化
同步
服务
认证
三个
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
区块链数据库怎么样
阿里云2t流量服务器
手机提醒服务器异常
深圳优加互联网科技有限公司
网络安全法国家实施
中国最快的网络技术
服务器共享硬盘.
救赎ol服务器
牛牛豆科技互联网公司
漳州数据库安全审计
穿越火线总是出现服务器连接
电脑版网易哪些服务器好玩
金蝶保存数据库
c 处理高并发数据库
数据库查看表内容的命令是
php删除数据库表的数据
河北品牌服务器回收报价
榆林市第四届网络安全周启动
小程序云开发没有数据库吗
MSW服务器
福建pdu服务器专用电源厂家
gdb数据库下载数据
新罗区晨轩鹏网络技术工作室
显示某个数据库中所有表用 命令
暗影格斗3服务器显示响应时间长
当前主流数据库系统采用
h61主板最高支持服务器cpu
中信银行软件开发规范
国家网络安全保密法
签字背书软件开发