Database Reference
In-Depth Information
A client using the
ZooKeeper
object can receive notifications of the state transitions by
registering a
Watcher
object. On entering the
CONNECTED
state, the watcher receives a
WatchedEvent
whose
KeeperState
value is
SyncConnected
.
NOTE
A ZooKeeper
Watcher
object serves double duty: it can be used to be notified of changes in the
ZooKeeper state (as described in this section), and it can be used to be notified of changes in znodes (de-
scribed in
Watch triggers
). The (default) watcher passed into the
ZooKeeper
object constructor is used
for state changes, but znode changes may either use a dedicated instance of
Watcher
(by passing one in
to the appropriate read operation) or share the default one if using the form of the read operation that
takes a Boolean flag to specify whether to use a watcher.
The
ZooKeeper
instance may disconnect and reconnect to the ZooKeeper service, mov-
ing between the
CONNECTED
and
CONNECTING
states. If it disconnects, the watcher re-
ceives a
Disconnected
event. Note that these state transitions are initiated by the
ZooKeeper
instance itself, and it will automatically try to reconnect if the connection is
lost.
The
ZooKeeper
instance may transition to a third state,
CLOSED
, if either the
close()
method is called or the session times out, as indicated by a
KeeperState
of
type
Expired
. Once in the
CLOSED
state, the
ZooKeeper
object is no longer con-
sidered to be alive (this can be tested using the
isAlive()
method on
States
) and
cannot be reused. To reconnect to the ZooKeeper service, the client must construct a new
ZooKeeper
instance.