Interface StatefulRedisClusterPubSubConnection<K,V>
- All Superinterfaces:
AsyncCloseable,AutoCloseable,StatefulConnection<K,V>,StatefulRedisConnection<K,V>,StatefulRedisPubSubConnection<K,V>
public interface StatefulRedisClusterPubSubConnection<K,V> extends StatefulRedisPubSubConnection<K,V>
This connection allows publishing and subscription to Pub/Sub messages within a Redis Cluster. Due to Redis Cluster's nature, messages are broadcasted across the cluster and a client can connect to any arbitrary node to participate with a subscription.
StatefulRedisClusterPubSubConnection<String, String> connection = clusterClient.connectPubSub();
connection.addListener(…);
RedisClusterPubSubCommands<String, String> sync = connection.sync();
sync.subscribe("channel");
sync.publish("channel", "message");
Keyspace notifications
Redis clients can subscribe to user-space Pub/Sub messages and Redis keyspace notifications. Other than user-space Pub/Sub messages are Keyspace notifications not broadcasted to the whole cluster. They stay node-local. Subscription to keyspace notifications requires subscription to the nodes which publish the keyspace notifications.
StatefulRedisClusterPubSubConnection allows node-specific subscriptions and message
propagation. setNodeMessagePropagation(boolean) can notify a RedisPubSubListener that requires a single registration
with this connection. Node-subscriptions are supported on
connection and NodeSelection levels through
asynchronous, synchronous, and reactive APIs.
StatefulRedisClusterPubSubConnection<String, String> connection = clusterClient.connectPubSub();
connection.addListener(…);
RedisClusterPubSubCommands<String, String> sync = connection.sync();
sync.replicas().commands().psubscribe("__key*__:*");
- Since:
- 4.4
- Author:
- Mark Paluch
-
Method Summary
Modifier and Type Method Description voidaddListener(RedisClusterPushListener listener)Add a newlistenerto consume push messages.voidaddListener(RedisClusterPubSubListener<K,V> listener)Add a newlistener.RedisClusterPubSubAsyncCommands<K,V>async()Returns theRedisClusterPubSubAsyncCommandsAPI for the current connection.StatefulRedisPubSubConnection<K,V>getConnection(String nodeId)Retrieve a connection to the specified cluster node using the nodeId.StatefulRedisPubSubConnection<K,V>getConnection(String host, int port)Retrieve a connection to the specified cluster node using host and port.CompletableFuture<StatefulRedisPubSubConnection<K,V>>getConnectionAsync(String nodeId)Retrieve asynchronously a connection to the specified cluster node using the nodeId.CompletableFuture<StatefulRedisPubSubConnection<K,V>>getConnectionAsync(String host, int port)Retrieve a connection to the specified cluster node using host and port.PartitionsgetPartitions()RedisClusterPubSubReactiveCommands<K,V>reactive()Returns theRedisClusterPubSubReactiveCommandsAPI for the current connection.voidremoveListener(RedisClusterPushListener listener)Remove an existinglistener.voidremoveListener(RedisClusterPubSubListener<K,V> listener)Remove an existinglistener.voidsetNodeMessagePropagation(boolean enabled)RedisClusterPubSubCommands<K,V>sync()Returns theRedisClusterPubSubCommandsAPI for the current connection.Methods inherited from interface io.lettuce.core.api.StatefulConnection
close, closeAsync, dispatch, dispatch, flushCommands, getOptions, getResources, getTimeout, isOpen, reset, setAutoFlushCommands, setTimeoutMethods inherited from interface io.lettuce.core.api.StatefulRedisConnection
addListener, isMulti, removeListenerMethods inherited from interface io.lettuce.core.pubsub.StatefulRedisPubSubConnection
addListener, removeListener
-
Method Details
-
sync
RedisClusterPubSubCommands<K,V> sync()Returns theRedisClusterPubSubCommandsAPI for the current connection. Does not create a new connection.- Specified by:
syncin interfaceStatefulRedisConnection<K,V>- Specified by:
syncin interfaceStatefulRedisPubSubConnection<K,V>- Returns:
- the synchronous API for the underlying connection.
-
async
RedisClusterPubSubAsyncCommands<K,V> async()Returns theRedisClusterPubSubAsyncCommandsAPI for the current connection. Does not create a new connection.- Specified by:
asyncin interfaceStatefulRedisConnection<K,V>- Specified by:
asyncin interfaceStatefulRedisPubSubConnection<K,V>- Returns:
- the asynchronous API for the underlying connection.
-
reactive
RedisClusterPubSubReactiveCommands<K,V> reactive()Returns theRedisClusterPubSubReactiveCommandsAPI for the current connection. Does not create a new connection.- Specified by:
reactivein interfaceStatefulRedisConnection<K,V>- Specified by:
reactivein interfaceStatefulRedisPubSubConnection<K,V>- Returns:
- the reactive API for the underlying connection.
-
getConnection
Retrieve a connection to the specified cluster node using the nodeId. Host and port are looked up in the node list. This connection is bound to the node id. Once the cluster topology view is updated, the connection will try to reconnect the to the node with the specifiednodeId, that behavior can also lead to a closed connection once the node with the specifiednodeIdis no longer part of the cluster.Do not close the connections. Otherwise, unpredictable behavior will occur. The nodeId must be part of the cluster and is validated against the current topology view in
Partitions.- Parameters:
nodeId- the node Id- Returns:
- a connection to the requested cluster node
- Throws:
RedisException- if the requested node identified bynodeIdis not part of the cluster
-
getConnectionAsync
Retrieve asynchronously a connection to the specified cluster node using the nodeId. Host and port are looked up in the node list. This connection is bound to the node id. Once the cluster topology view is updated, the connection will try to reconnect the to the node with the specifiednodeId, that behavior can also lead to a closed connection once the node with the specifiednodeIdis no longer part of the cluster.Do not close the connections. Otherwise, unpredictable behavior will occur. The nodeId must be part of the cluster and is validated against the current topology view in
Partitions.- Parameters:
nodeId- the node Id- Returns:
CompletableFutureto indicate success or failure to connect to the requested cluster node.- Throws:
RedisException- if the requested node identified bynodeIdis not part of the cluster- Since:
- 5.0
-
getConnection
Retrieve a connection to the specified cluster node using host and port. This connection is bound to a host and port. Updates to the cluster topology view can close the connection once the host, identified byhostandport, are no longer part of the cluster.Do not close the connections. Otherwise, unpredictable behavior will occur. Host and port connections are verified by default for cluster membership, see
ClusterClientOptions.isValidateClusterNodeMembership().- Parameters:
host- the hostport- the port- Returns:
- a connection to the requested cluster node
- Throws:
RedisException- if the requested node identified byhostandportis not part of the cluster
-
getConnectionAsync
Retrieve a connection to the specified cluster node using host and port. This connection is bound to a host and port. Updates to the cluster topology view can close the connection once the host, identified byhostandport, are no longer part of the cluster.Do not close the connections. Otherwise, unpredictable behavior will occur. Host and port connections are verified by default for cluster membership, see
ClusterClientOptions.isValidateClusterNodeMembership().- Parameters:
host- the hostport- the port- Returns:
CompletableFutureto indicate success or failure to connect to the requested cluster node.- Throws:
RedisException- if the requested node identified byhostandportis not part of the cluster- Since:
- 5.0
-
getPartitions
Partitions getPartitions()- Returns:
- Known partitions for this connection.
-
setNodeMessagePropagation
void setNodeMessagePropagation(boolean enabled)Enables/disables node message propagation tothisconnectionslisteners.If
enabledistrue, then Pub/Sub messages received on node-specific connections are propagated to this connection facade. RegisteredRedisPubSubListenerwill receive messages from individual node subscriptions.Node event propagation is disabled by default.
- Parameters:
enabled-trueto enable node message propagation;false(default) to disable message propagation.
-
addListener
Add a newlistener.- Parameters:
listener- the listener, must not benull.
-
removeListener
Remove an existinglistener.- Parameters:
listener- the listener, must not benull.
-
addListener
Add a newlistenerto consume push messages.- Parameters:
listener- the listener, must not benull.- Since:
- 6.0
-
removeListener
Remove an existinglistener.- Parameters:
listener- the listener, must not benull.- Since:
- 6.0
-