public class SafeInitNodeStateListener extends Object implements NodeStateListener
By default, the driver calls node state events, such as onUp(com.datastax.oss.driver.api.core.metadata.Node)
and onAdd(com.datastax.oss.driver.api.core.metadata.Node)
,
before the session is ready; see NodeStateListener.onSessionReady(Session)
for a detailed
explanation. This can make things complicated if your listener implementation needs the session
to process those events.
This class wraps another implementation to shield it from those details:
NodeStateListener delegate = ... // your listener implementation SafeInitNodeStateListener wrapper = new SafeInitNodeStateListener(delegate, true); CqlSession session = CqlSession.builder() .withNodeStateListener(wrapper) .build();With this setup,
delegate.onSessionReady
is guaranteed to be invoked first, before any
other method. The second constructor argument indicates what to do with the method calls that
were ignored before that:
true
, they are recorded, and replayed to delegate
immediately after
onSessionReady(com.datastax.oss.driver.api.core.session.Session)
. They are guaranteed to happen in the original order, and before
any post-initialization events.
false
, they are discarded.
Usage in non-blocking applications: beware that this class is not lock-free. It is implemented with locks for internal coordination.
Constructor and Description |
---|
SafeInitNodeStateListener(NodeStateListener delegate,
boolean replayInitEvents)
Creates a new instance.
|
Modifier and Type | Method and Description |
---|---|
void |
close() |
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.
|
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 . |
public SafeInitNodeStateListener(@NonNull NodeStateListener delegate, boolean replayInitEvents)
delegate
- the wrapped listener, to which method invocations will be forwarded.replayInitEvents
- whether to record events during initialization and replay them to the
child listener once it's created, or just ignore them.public void onSessionReady(@NonNull Session session)
NodeStateListener
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:
NodeStateListener.onDown(Node)
is invoked; for the one that
eventually succeeds, NodeStateListener.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, NodeStateListener.onAdd(Node)
is invoked; for any contact point that doesn't have a corresponding entry
in the table, NodeStateListener.onRemove(Node)
is invoked;
NodeStateListener.onUp(Node)
is invoked; otherwise, NodeStateListener.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.
onSessionReady
in interface NodeStateListener
public void onAdd(@NonNull Node node)
NodeStateListener
The node is not up yet at this point. NodeStateListener.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.
onAdd
in interface NodeStateListener
public void onUp(@NonNull Node node)
NodeStateListener
NodeState.UP
.onUp
in interface NodeStateListener
public void onDown(@NonNull Node node)
NodeStateListener
NodeState.DOWN
or NodeState.FORCED_DOWN
.onDown
in interface NodeStateListener
public void onRemove(@NonNull Node node)
NodeStateListener
This can be triggered by a topology event, or during a full node list refresh if the node is absent from the new list.
onRemove
in interface NodeStateListener
public void close() throws Exception
close
in interface AutoCloseable
Exception
Copyright © 2017–2024. All rights reserved.