public class SafeInitNodeStateListener extends Object implements NodeStateListener
By default, the driver calls node state events, such as
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.onSessionReadyis 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
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.
|Constructor and Description|
Creates a new instance.
|Modifier and Type||Method and Description|
Invoked when a node is first added to the cluster.
Invoked when a node leaves the cluster.
Invoked when the session is ready to process user requests.
Invoked when a node's state switches to
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)
This corresponds to the moment when
SessionBuilder.build() returns, or the future
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.peerstable 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.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).
This method's default implementation is empty.
public void onAdd(@NonNull Node node)
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.
public void onUp(@NonNull Node node)
public 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.
Copyright © 2017–2020. All rights reserved.