接口中有枚举时

本文详细解析了ZooKeeper中Watcher接口的设计原理及其内部枚举类型KeeperState和EventType的定义,帮助读者理解ZooKeeper客户端状态变化及事件处理机制。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

读zookeeper的Watcher接口:


public interface Watcher {

/**
* This interface defines the possible states an Event may represent
*/
public interface Event {
/**
* Enumeration of states the ZooKeeper may be at the event
*/
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 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);

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 -112: return KeeperState.Expired;

default:
throw new RuntimeException("Invalid integer value for conversion to KeeperState");
}
}
}

/**
* Enumeration of types of events that may occur on the ZooKeeper
*/
public enum EventType {
None (-1),
NodeCreated (1),
NodeDeleted (2),
NodeDataChanged (3),
NodeChildrenChanged (4);

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;

default:
throw new RuntimeException("Invalid integer value for conversion to EventType");
}
}
}
}

abstract public void process(WatchedEvent event);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值