Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to implement event listening Watcher in zookeeper

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

Editor to share with you how to achieve event monitoring Watcher in zookeeper, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

Watcher is the event listening mechanism of zookeeper. Today let's take a look at what the code of the Watcher class contains.

Watcher

Watcher is an interface that defines process methods and requires subclass implementation. It represents the method that must be implemented when implementing the Watcher interface, that is, defining the event to be processed, and WatchedEvent representing the observed event.

Abstract public void process (WatchedEvent event); inner class 1, Event interface

Represents the state represented by the event, which contains two internal enumeration classes, KeeperState and EventType.

KeeperState

KeeperState is an enumerated class that defines the various states that the Zookeeper is in when the event occurs, and it also defines a method fromInt that returns the corresponding state from the integer value.

@ 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 * / / unknown status The server no longer generates this state @ Deprecated Unknown (- 1), / * * The client is in the disconnected state-it is not connected * to any server in the ensemble. * / / disconnect Disconnected (0), / * * Unused, this state is never generated by the server * / is not synchronized and is no longer used The server will not generate this state @ 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). * / / synchronous connection status SyncConnected (3), / * Auth failed state * / / Authentication failure status 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 rbino servers. * / / read-only connection status 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 authentication passed status 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 status 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)} * / / disable Closed (7); / / represents the integer value of the state private final int intValue / / Integer representation of value / / for sending over wire KeeperState (int intValue) {this.intValue = intValue;} public int getIntValue () {return intValue } / / construct the corresponding state 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 from the integer 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 is an enumeration class that defines the type of event (such as creating a node, deleting a node, and so on), as well as a method fromInt that returns the corresponding event type from an integer value.

@ InterfaceAudience.Public public enum EventType {/ / No None (- 1), / / Node creation NodeCreated (1), / / Node deletion NodeDeleted (2), / / Node data change NodeDataChanged (3), / / Child Node change NodeChildrenChanged (4) / / snooping removes DataWatchRemoved (5), / / Child snooping removes 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 } / / construct the corresponding event 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 from the integer 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, enumerated class WatcherType

Listener type enumeration

@ InterfaceAudience.Public public enum WatcherType {/ / sublistener Children (1), / / data listener Data (2), / / any Any (3); / / Integer representation of value private final int intValue; private WatcherType (int intValue) {this.intValue = intValue } public int getIntValue () {return intValue;} / / integer-to-type conversion 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;}

The WatchedEvent class contains three attributes that represent the state of the Zookeeper at the time of the event, the event type, and the node path involved in the event.

These are all the contents of the article "how to implement event snooping Watcher in zookeeper". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report