Class StatefulRedisClusterConnectionImpl<K,V>
- java.lang.Object
-
- io.lettuce.core.RedisChannelHandler<K,V>
-
- io.lettuce.core.cluster.StatefulRedisClusterConnectionImpl<K,V>
-
- All Implemented Interfaces:
AsyncCloseable
,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 oneStatefulRedisClusterConnectionImpl
AConnectionWatchdog
monitors each connection and reconnects automatically untilRedisChannelHandler.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>
async
protected RedisCodec<K,V>
codec
protected RedisAdvancedClusterReactiveCommandsImpl<K,V>
reactive
protected 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
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
activated()
Notification when the connection becomes active (connected).void
addListener(RedisClusterPushListener listener)
Add a newlistener
to consume push messages.RedisAdvancedClusterAsyncCommands<K,V>
async()
Returns theRedisAdvancedClusterAsyncCommands
API for the current connection.protected <T> T
clusterSyncHandler(Class<?>... interfaces)
<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 host, int port, ConnectionIntent connectionIntent)
Retrieve a connection to the specified cluster node using host and port.StatefulRedisConnection<K,V>
getConnection(String nodeId, ConnectionIntent connectionIntent)
Retrieve a connection to the specified cluster node using thenodeId
for the givenConnectionIntent
.CompletableFuture<StatefulRedisConnection<K,V>>
getConnectionAsync(String host, int port, ConnectionIntent connectionIntent)
Retrieve asynchronously a connection to the specified cluster node using host and port for the givenConnectionIntent
.CompletableFuture<StatefulRedisConnection<K,V>>
getConnectionAsync(String nodeId, ConnectionIntent connectionIntent)
Retrieve asynchronously a connection to the specified cluster node using thenodeId
for the givenConnectionIntent
.Partitions
getPartitions()
ReadFrom
getReadFrom()
Gets theReadFrom
setting for this connection.protected RedisAdvancedClusterAsyncCommandsImpl<K,V>
newRedisAdvancedClusterAsyncCommandsImpl()
protected RedisAdvancedClusterCommands<K,V>
newRedisAdvancedClusterCommandsImpl()
protected RedisAdvancedClusterReactiveCommandsImpl<K,V>
newRedisAdvancedClusterReactiveCommandsImpl()
RedisAdvancedClusterReactiveCommands<K,V>
reactive()
Returns theRedisAdvancedClusterReactiveCommands
API for the current connection.void
removeListener(RedisClusterPushListener listener)
Remove an existinglistener
.void
setPartitions(Partitions partitions)
void
setReadFrom(ReadFrom readFrom)
Set from which nodes data is read.RedisAdvancedClusterCommands<K,V>
sync()
Returns theRedisAdvancedClusterCommands
API for the current connection.protected InvocationHandler
syncInvocationHandler()
-
Methods inherited from class io.lettuce.core.RedisChannelHandler
addListener, addListener, close, closeAsync, deactivated, flushCommands, getChannelWriter, getConnectionEvents, getOptions, getResources, getTimeout, isClosed, isOpen, registerCloseables, removeListener, reset, setAutoFlushCommands, setOptions, setTimeout, syncHandler
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface io.lettuce.core.api.StatefulConnection
addListener, close, closeAsync, flushCommands, getOptions, getResources, getTimeout, isOpen, removeListener, reset, setAutoFlushCommands, setTimeout
-
Methods inherited from interface io.lettuce.core.cluster.api.StatefulRedisClusterConnection
getChannelWriter, getConnection, getConnection, getConnectionAsync, getConnectionAsync
-
-
-
-
Field Detail
-
codec
protected final RedisCodec<K,V> codec
-
sync
protected final RedisAdvancedClusterCommands<K,V> sync
-
async
protected final RedisAdvancedClusterAsyncCommandsImpl<K,V> async
-
reactive
protected final RedisAdvancedClusterReactiveCommandsImpl<K,V> reactive
-
-
Constructor Detail
-
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 Detail
-
newRedisAdvancedClusterReactiveCommandsImpl
protected RedisAdvancedClusterReactiveCommandsImpl<K,V> newRedisAdvancedClusterReactiveCommandsImpl()
-
newRedisAdvancedClusterCommandsImpl
protected RedisAdvancedClusterCommands<K,V> newRedisAdvancedClusterCommandsImpl()
-
clusterSyncHandler
protected <T> T clusterSyncHandler(Class<?>... interfaces)
-
newRedisAdvancedClusterAsyncCommandsImpl
protected RedisAdvancedClusterAsyncCommandsImpl<K,V> newRedisAdvancedClusterAsyncCommandsImpl()
-
sync
public RedisAdvancedClusterCommands<K,V> sync()
Description copied from interface:StatefulRedisClusterConnection
Returns theRedisAdvancedClusterCommands
API for the current connection. Does not create a new connection.- Specified by:
sync
in interfaceStatefulRedisClusterConnection<K,V>
- Returns:
- the synchronous API for the underlying connection.
-
syncInvocationHandler
protected InvocationHandler syncInvocationHandler()
-
async
public RedisAdvancedClusterAsyncCommands<K,V> async()
Description copied from interface:StatefulRedisClusterConnection
Returns theRedisAdvancedClusterAsyncCommands
API for the current connection. Does not create a new connection.- Specified by:
async
in interfaceStatefulRedisClusterConnection<K,V>
- Returns:
- the asynchronous API for the underlying connection.
-
reactive
public RedisAdvancedClusterReactiveCommands<K,V> reactive()
Description copied from interface:StatefulRedisClusterConnection
Returns theRedisAdvancedClusterReactiveCommands
API for the current connection. Does not create a new connection.- Specified by:
reactive
in interfaceStatefulRedisClusterConnection<K,V>
- Returns:
- the reactive API for the underlying connection.
-
addListener
public void addListener(RedisClusterPushListener listener)
Description copied from interface:StatefulRedisClusterConnection
Add a newlistener
to consume push messages.- Specified by:
addListener
in interfaceStatefulRedisClusterConnection<K,V>
- Parameters:
listener
- the listener, must not benull
.
-
removeListener
public void removeListener(RedisClusterPushListener listener)
Description copied from interface:StatefulRedisClusterConnection
Remove an existinglistener
.- Specified by:
removeListener
in interfaceStatefulRedisClusterConnection<K,V>
- Parameters:
listener
- the listener, must not benull
.
-
getConnection
public StatefulRedisConnection<K,V> getConnection(String nodeId, ConnectionIntent connectionIntent)
Description copied from interface:StatefulRedisClusterConnection
Retrieve a connection to the specified cluster node using thenodeId
for the givenConnectionIntent
. 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 specifiednodeId
is 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
.This method is intended to be used for cases where the caller requires a specific connection type (READ or WRITE) to a given node in a cluster.
In contrast to the
StatefulRedisClusterConnection
, node-connections do not route commands to other cluster nodes.- Specified by:
getConnection
in interfaceStatefulRedisClusterConnection<K,V>
- Parameters:
nodeId
- the node IdconnectionIntent
- the intent for usage of the connection.- Returns:
- a connection to the requested cluster node
-
getConnectionAsync
public CompletableFuture<StatefulRedisConnection<K,V>> getConnectionAsync(String nodeId, ConnectionIntent connectionIntent)
Description copied from interface:StatefulRedisClusterConnection
Retrieve asynchronously a connection to the specified cluster node using thenodeId
for the givenConnectionIntent
. 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 specifiednodeId
is 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
.This method is intended to be used for cases where the caller requires a specific connection type (READ or WRITE) to a given node in a cluster.
In contrast to the
StatefulRedisClusterConnection
, node-connections do not route commands to other cluster nodes.- Specified by:
getConnectionAsync
in interfaceStatefulRedisClusterConnection<K,V>
- Parameters:
nodeId
- the node IdconnectionIntent
- the intent for usage of the connection.- Returns:
CompletableFuture
to indicate success or failure to connect to the requested cluster node.
-
getConnection
public StatefulRedisConnection<K,V> getConnection(String host, int port, ConnectionIntent connectionIntent)
Description copied from interface:StatefulRedisClusterConnection
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 byhost
andport
, 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:
getConnection
in interfaceStatefulRedisClusterConnection<K,V>
- Parameters:
host
- the hostport
- the portconnectionIntent
- the intent of the connection seeStatefulRedisClusterConnection.getConnection(String, ConnectionIntent)
- Returns:
- a connection to the requested cluster node
-
getConnectionAsync
public CompletableFuture<StatefulRedisConnection<K,V>> getConnectionAsync(String host, int port, ConnectionIntent connectionIntent)
Description copied from interface:StatefulRedisClusterConnection
Retrieve asynchronously a connection to the specified cluster node using host and port for the givenConnectionIntent
. This connection is bound to a host and port. Updates to the cluster topology view can close the connection once the host, identified byhost
andport
, 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:
getConnectionAsync
in interfaceStatefulRedisClusterConnection<K,V>
- Parameters:
host
- the hostport
- the portconnectionIntent
- the intent of the connection seeStatefulRedisClusterConnection.getConnection(String, ConnectionIntent)
- Returns:
CompletableFuture
to indicate success or failure to connect to the requested cluster node.
-
activated
public void activated()
Description copied from class:RedisChannelHandler
Notification when the connection becomes active (connected).- Specified by:
activated
in interfaceConnectionFacade
- Overrides:
activated
in classRedisChannelHandler<K,V>
-
dispatch
public <T> RedisCommand<K,V,T> dispatch(RedisCommand<K,V,T> command)
Description copied from interface:StatefulConnection
Dispatch 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:
dispatch
in interfaceStatefulConnection<K,V>
- Overrides:
dispatch
in 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:StatefulConnection
Dispatch 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:
dispatch
in interfaceStatefulConnection<K,V>
- Overrides:
dispatch
in classRedisChannelHandler<K,V>
- Parameters:
commands
- the Redis commands.- Returns:
- the written Redis commands.
-
setPartitions
public void setPartitions(Partitions partitions)
-
getPartitions
public Partitions getPartitions()
- Specified by:
getPartitions
in interfaceStatefulRedisClusterConnection<K,V>
- Returns:
- Known partitions for this connection.
-
setReadFrom
public void setReadFrom(ReadFrom readFrom)
Description copied from interface:StatefulRedisClusterConnection
Set from which nodes data is read. The setting is used as default for read operations on this connection. See the documentation forReadFrom
for more information.- Specified by:
setReadFrom
in interfaceStatefulRedisClusterConnection<K,V>
- Parameters:
readFrom
- the read from setting, must not benull
-
getReadFrom
public ReadFrom getReadFrom()
Description copied from interface:StatefulRedisClusterConnection
Gets theReadFrom
setting for this connection. Defaults toReadFrom.UPSTREAM
if not set.- Specified by:
getReadFrom
in interfaceStatefulRedisClusterConnection<K,V>
- Returns:
- the read from setting
-
-