public interface NodeStateListener extends AutoCloseable
An implementation of this interface can be registered in the configuration, or with SessionBuilder.withNodeStateListener(NodeStateListener)
.
Note that the methods defined by this interface will be executed by internal driver threads, and are therefore expected to have short execution times. If you need to perform long computations or blocking calls in response to schema change events, it is strongly recommended to schedule them asynchronously on a separate thread provided by your application code.
If you implement this interface but don't need to implement all the methods, extend NodeStateListenerBase
.
Modifier and Type | Method and Description |
---|---|
void |
onAdd(Node node)
Invoked when a node is first added to the cluster.
|
void |
onDown(Node node)
Invoked when a node's state switches to
NodeState.DOWN or NodeState.FORCED_DOWN . |
void |
onRemove(Node node)
Invoked when a node leaves the cluster.
|
default void |
onSessionReady(Session session)
Invoked when the session is ready to process user requests.
|
void |
onUp(Node node)
Invoked when a node's state switches to
NodeState.UP . |
close
void onAdd(@NonNull Node node)
The node is not up yet at this point. onUp(Node)
will be notified later if the
driver successfully connects to the node (provided that a session is opened and the node is not
ignored
), or receives a topology event for it.
This method is not invoked for the contact points provided at initialization. It is however for new nodes discovered during the full node list refresh after the first connection.
void onUp(@NonNull Node node)
NodeState.UP
.void onDown(@NonNull Node node)
NodeState.DOWN
or NodeState.FORCED_DOWN
.void onRemove(@NonNull Node node)
This can be triggered by a topology event, or during a full node list refresh if the node is absent from the new list.
default void onSessionReady(@NonNull Session session)
This corresponds to the moment when SessionBuilder.build()
returns, or the future
returned by SessionBuilder.buildAsync()
completes. If the session initialization fails,
this method will not get called.
Listener methods are invoked from different threads; if you store the session in a field, make it at least volatile to guarantee proper publication.
Note that this method will not be the first one invoked on the listener; the driver emits node events before that, during the initialization of the session:
onDown(Node)
is invoked; for the one that
eventually succeeds, onUp(Node)
is invoked and that node becomes the control
node (if none succeeds, the session initialization fails and the process stops here).
system.peers
table is inspected to discover the remaining
nodes in the cluster. For any node that wasn't already a contact point, onAdd(Node)
is invoked; for any contact point that doesn't have a corresponding entry
in the table, onRemove(Node)
is invoked;
onUp(Node)
is invoked; otherwise, onDown(Node)
is invoked (no additional event is emitted for the control node, it is
considered up since we already have a connection to it).
SafeInitNodeStateListener
.
This method's default implementation is empty.
Copyright © 2017–2024. All rights reserved.