java.lang.Object
io.lettuce.core.RedisChannelHandler<K,V>
io.lettuce.core.cluster.StatefulRedisClusterConnectionImpl<K,V>
- All Implemented Interfaces:
StatefulConnection<K,V>,StatefulRedisClusterConnection<K,V>,AsyncCloseable,ConnectionFacade,Closeable,AutoCloseable
public class StatefulRedisClusterConnectionImpl<K,V> extends RedisChannelHandler<K,V> implements StatefulRedisClusterConnection<K,V>
A thread-safe connection to a Redis Cluster. Multiple threads may share one
StatefulRedisClusterConnectionImpl
A ConnectionWatchdog monitors each connection and reconnects automatically until RedisChannelHandler.close() is called. All
pending commands will be (re)sent after successful reconnection.- Since:
- 4.0
- Author:
- Mark Paluch
-
Field Summary
Fields Modifier and Type Field Description protected RedisAdvancedClusterAsyncCommandsImpl<K,V>asyncprotected RedisCodec<K,V>codecprotected RedisAdvancedClusterReactiveCommandsImpl<K,V>reactiveprotected RedisAdvancedClusterCommands<K,V>sync -
Constructor Summary
Constructors Constructor Description StatefulRedisClusterConnectionImpl(RedisChannelWriter writer, ClusterPushHandler pushHandler, RedisCodec<K,V> codec, Duration timeout)Initialize a new connection. -
Method Summary
Modifier and Type Method Description voidactivated()Notification when the connection becomes active (connected).voidaddListener(RedisClusterPushListener listener)Add a newlistenerto consume push messages.RedisAdvancedClusterAsyncCommands<K,V>async()Returns theRedisAdvancedClusterAsyncCommandsAPI for the current connection.<T> RedisCommand<K,V,T>dispatch(RedisCommand<K,V,T> command)Dispatch a command.Collection<RedisCommand<K,V,?>>dispatch(Collection<? extends RedisCommand<K,V,?>> commands)Dispatch multiple command in a single write on the channel.StatefulRedisConnection<K,V>getConnection(String nodeId)Retrieve a connection to the specified cluster node using the nodeId.StatefulRedisConnection<K,V>getConnection(String host, int port)Retrieve a connection to the specified cluster node using host and port.CompletableFuture<StatefulRedisConnection<K,V>>getConnectionAsync(String nodeId)Retrieve asynchronously a connection to the specified cluster node using the nodeId.CompletableFuture<StatefulRedisConnection<K,V>>getConnectionAsync(String host, int port)Retrieve asynchronously a connection to the specified cluster node using host and port.PartitionsgetPartitions()ReadFromgetReadFrom()Gets theReadFromsetting for this connection.RedisAdvancedClusterReactiveCommands<K,V>reactive()Returns theRedisAdvancedClusterReactiveCommandsAPI for the current connection.voidremoveListener(RedisClusterPushListener listener)Remove an existinglistener.voidsetPartitions(Partitions partitions)voidsetReadFrom(ReadFrom readFrom)Set from which nodes data is read.RedisAdvancedClusterCommands<K,V>sync()Returns theRedisAdvancedClusterCommandsAPI for the current connection.protected InvocationHandlersyncInvocationHandler()Methods inherited from class io.lettuce.core.RedisChannelHandler
addListener, close, closeAsync, deactivated, flushCommands, getChannelWriter, getOptions, getResources, getTimeout, isClosed, isOpen, registerCloseables, reset, setAutoFlushCommands, setOptions, setTimeout, syncHandlerMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface io.lettuce.core.api.StatefulConnection
close, closeAsync, flushCommands, getOptions, getResources, getTimeout, isOpen, reset, setAutoFlushCommands, setTimeoutMethods inherited from interface io.lettuce.core.cluster.api.StatefulRedisClusterConnection
getChannelWriter
-
Field Details
-
codec
-
sync
-
async
-
reactive
-
-
Constructor Details
-
StatefulRedisClusterConnectionImpl
public StatefulRedisClusterConnectionImpl(RedisChannelWriter writer, ClusterPushHandler pushHandler, RedisCodec<K,V> codec, Duration timeout)Initialize a new connection.- Parameters:
writer- the channel writerpushHandler- the Cluster push handlercodec- Codec used to encode/decode keys and values.timeout- Maximum time to wait for a response.
-
-
Method Details
-
sync
Description copied from interface:StatefulRedisClusterConnectionReturns theRedisAdvancedClusterCommandsAPI for the current connection. Does not create a new connection.- Specified by:
syncin interfaceStatefulRedisClusterConnection<K,V>- Returns:
- the synchronous API for the underlying connection.
-
syncInvocationHandler
-
async
Description copied from interface:StatefulRedisClusterConnectionReturns theRedisAdvancedClusterAsyncCommandsAPI for the current connection. Does not create a new connection.- Specified by:
asyncin interfaceStatefulRedisClusterConnection<K,V>- Returns:
- the asynchronous API for the underlying connection.
-
reactive
Description copied from interface:StatefulRedisClusterConnectionReturns theRedisAdvancedClusterReactiveCommandsAPI for the current connection. Does not create a new connection.- Specified by:
reactivein interfaceStatefulRedisClusterConnection<K,V>- Returns:
- the reactive API for the underlying connection.
-
addListener
Description copied from interface:StatefulRedisClusterConnectionAdd a newlistenerto consume push messages.- Specified by:
addListenerin interfaceStatefulRedisClusterConnection<K,V>- Parameters:
listener- the listener, must not benull.
-
removeListener
Description copied from interface:StatefulRedisClusterConnectionRemove an existinglistener.- Specified by:
removeListenerin interfaceStatefulRedisClusterConnection<K,V>- Parameters:
listener- the listener, must not benull.
-
getConnection
Description copied from interface:StatefulRedisClusterConnectionRetrieve 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 inPartitions. In contrast to theStatefulRedisClusterConnection, node-connections do not route commands to other cluster nodes.- Specified by:
getConnectionin interfaceStatefulRedisClusterConnection<K,V>- Parameters:
nodeId- the node Id- Returns:
- a connection to the requested cluster node
-
getConnectionAsync
Description copied from interface:StatefulRedisClusterConnectionRetrieve 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 inPartitions. In contrast to theStatefulRedisClusterConnection, node-connections do not route commands to other cluster nodes.- Specified by:
getConnectionAsyncin interfaceStatefulRedisClusterConnection<K,V>- Parameters:
nodeId- the node Id- Returns:
CompletableFutureto indicate success or failure to connect to the requested cluster node.
-
getConnection
Description copied from interface:StatefulRedisClusterConnectionRetrieve 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().In contrast to the
StatefulRedisClusterConnection, node-connections do not route commands to other cluster nodes.- Specified by:
getConnectionin interfaceStatefulRedisClusterConnection<K,V>- Parameters:
host- the hostport- the port- Returns:
- a connection to the requested cluster node
-
getConnectionAsync
Description copied from interface:StatefulRedisClusterConnectionRetrieve asynchronously 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().In contrast to the
StatefulRedisClusterConnection, node-connections do not route commands to other cluster nodes.- Specified by:
getConnectionAsyncin interfaceStatefulRedisClusterConnection<K,V>- Parameters:
host- the hostport- the port- Returns:
CompletableFutureto indicate success or failure to connect to the requested cluster node.
-
activated
public void activated()Description copied from class:RedisChannelHandlerNotification when the connection becomes active (connected).- Specified by:
activatedin interfaceConnectionFacade- Overrides:
activatedin classRedisChannelHandler<K,V>
-
dispatch
Description copied from interface:StatefulConnectionDispatch a command. Write a command on the channel. The command may be changed/wrapped during write and the written instance is returned after the call. This command does not wait until the command completes and does not guarantee whether the command is executed successfully.- Specified by:
dispatchin interfaceStatefulConnection<K,V>- Overrides:
dispatchin classRedisChannelHandler<K,V>- Type Parameters:
T- result type- Parameters:
command- the Redis command.- Returns:
- the written Redis command.
-
dispatch
public Collection<RedisCommand<K,V,?>> dispatch(Collection<? extends RedisCommand<K,V,?>> commands)Description copied from interface:StatefulConnectionDispatch multiple command in a single write on the channel. The commands may be changed/wrapped during write and the written instance is returned after the call. This command does not wait until the command completes and does not guarantee whether the command is executed successfully.- Specified by:
dispatchin interfaceStatefulConnection<K,V>- Overrides:
dispatchin classRedisChannelHandler<K,V>- Parameters:
commands- the Redis commands.- Returns:
- the written Redis commands.
-
setPartitions
-
getPartitions
- Specified by:
getPartitionsin interfaceStatefulRedisClusterConnection<K,V>- Returns:
- Known partitions for this connection.
-
setReadFrom
Description copied from interface:StatefulRedisClusterConnectionSet from which nodes data is read. The setting is used as default for read operations on this connection. See the documentation forReadFromfor more information.- Specified by:
setReadFromin interfaceStatefulRedisClusterConnection<K,V>- Parameters:
readFrom- the read from setting, must not benull
-
getReadFrom
Description copied from interface:StatefulRedisClusterConnectionGets theReadFromsetting for this connection. Defaults toReadFrom.UPSTREAMif not set.- Specified by:
getReadFromin interfaceStatefulRedisClusterConnection<K,V>- Returns:
- the read from setting
-