java.lang.Object
io.lettuce.core.AbstractRedisClient
- Direct Known Subclasses:
RedisClient,RedisClusterClient
public abstract class AbstractRedisClient extends Object
Base Redis client. This class holds the netty infrastructure,
ClientOptions and the basic connection procedure. This
class creates the netty EventLoopGroups for NIO (NioEventLoopGroup) and EPoll (
EpollEventLoopGroup) with a default of Runtime.getRuntime().availableProcessors() * 4
threads. Reuse the instance as much as possible since the EventLoopGroup instances are expensive and can consume a
huge part of your resources, if you create multiple instances.
You can set the number of threads per NioEventLoopGroup by setting the io.netty.eventLoopThreads system
property to a reasonable number of threads.
- Since:
- 3.0
- Author:
- Mark Paluch, Jongyeol Choi, Poorva Gokhale
- See Also:
ClientResources
-
Field Summary
Fields Modifier and Type Field Description protected ChannelGroupchannelsprotected Set<Closeable>closeableResourcesprotected ConnectionEventsconnectionEvents -
Constructor Summary
Constructors Modifier Constructor Description protectedAbstractRedisClient(ClientResources clientResources)Create a new instance with client resources. -
Method Summary
Modifier and Type Method Description voidaddListener(CommandListener listener)Add a listener for Redis Command events.voidaddListener(RedisConnectionStateListener listener)Add a listener for the RedisConnectionState.protected voidchannelType(ConnectionBuilder connectionBuilder, ConnectionPoint connectionPoint)protected voidconnectionBuilder(Mono<SocketAddress> socketAddressSupplier, ConnectionBuilder connectionBuilder, RedisURI redisURI)Populate connection builder with necessary resources.protected io.lettuce.core.RedisHandshakecreateHandshake(ConnectionState state)protected intgetChannelCount()protected List<CommandListener>getCommandListeners()protected <T> TgetConnection(ConnectionFuture<T> connectionFuture)Retrieve the connection fromConnectionFuture.protected <T> TgetConnection(CompletableFuture<T> connectionFuture)Retrieve the connection fromConnectionFuture.DurationgetDefaultTimeout()Returns the defaulttimeoutfor commands.ClientOptionsgetOptions()Returns theClientOptionswhich are valid for that client.protected intgetResourceCount()ClientResourcesgetResources()Returns theClientResourceswhich are used with that client.protected <K, V, T extends RedisChannelHandler<K, V>>
ConnectionFuture<T>initializeChannelAsync(ConnectionBuilder connectionBuilder)Connect and initialize a channel fromConnectionBuilder.voidremoveListener(CommandListener listener)Removes a listener.voidremoveListener(RedisConnectionStateListener listener)Removes a listener.voidsetDefaultTimeout(long timeout, TimeUnit unit)Deprecated.voidsetDefaultTimeout(Duration timeout)Set the default timeout for connections created by this client.protected voidsetOptions(ClientOptions clientOptions)Set theClientOptionsfor the client.voidshutdown()Shutdown this client and close all open connections once this method is called.voidshutdown(long quietPeriod, long timeout, TimeUnit timeUnit)Shutdown this client and close all open connections once this method is called.voidshutdown(Duration quietPeriod, Duration timeout)Shutdown this client and close all open connections once this method is called.CompletableFuture<Void>shutdownAsync()Shutdown this client and close all open connections asynchronously.CompletableFuture<Void>shutdownAsync(long quietPeriod, long timeout, TimeUnit timeUnit)Shutdown this client and close all open connections asynchronously.
-
Field Details
-
connectionEvents
-
closeableResources
-
channels
-
-
Constructor Details
-
AbstractRedisClient
Create a new instance with client resources.- Parameters:
clientResources- the client resources. Ifnull, the client will create a new dedicated instance of client resources and keep track of them.
-
-
Method Details
-
getChannelCount
protected int getChannelCount() -
getDefaultTimeout
Returns the defaulttimeoutfor commands.- Returns:
- the default
timeoutfor commands.
-
setDefaultTimeout
Set the default timeout for connections created by this client. The timeout applies to connection attempts and non-blocking commands.- Parameters:
timeout- default connection timeout, must not benull.- Since:
- 5.0
-
setDefaultTimeout
Deprecated.since 5.0, usesetDefaultTimeout(Duration).Set the default timeout for connections created by this client. The timeout applies to connection attempts and non-blocking commands.- Parameters:
timeout- Default connection timeout.unit- Unit of time for the timeout.
-
getOptions
Returns theClientOptionswhich are valid for that client. Connections inherit the current options at the moment the connection is created. Changes to options will not affect existing connections.- Returns:
- the
ClientOptionsfor this client
-
setOptions
Set theClientOptionsfor the client.- Parameters:
clientOptions- client options for the client and connections that are created after setting the options
-
getResources
Returns theClientResourceswhich are used with that client.- Returns:
- the
ClientResourcesfor this client. - Since:
- 6.0
-
getResourceCount
protected int getResourceCount() -
addListener
Add a listener for the RedisConnectionState. The listener is notified every time a connect/disconnect/IO exception happens. The listeners are not bound to a specific connection, so every time a connection event happens on any connection, the listener will be notified. The corresponding netty channel handler (async connection) is passed on the event.- Parameters:
listener- must not benull
-
removeListener
Removes a listener.- Parameters:
listener- must not benull
-
addListener
Add a listener for Redis Command events. The listener is notified on each command start/success/failure.- Parameters:
listener- must not benull- Since:
- 6.1
-
removeListener
Removes a listener.- Parameters:
listener- must not benull- Since:
- 6.1
-
getCommandListeners
-
connectionBuilder
protected void connectionBuilder(Mono<SocketAddress> socketAddressSupplier, ConnectionBuilder connectionBuilder, RedisURI redisURI)Populate connection builder with necessary resources.- Parameters:
socketAddressSupplier- address supplier for initial connect and re-connectconnectionBuilder- connection builder to configure the connectionredisURI- URI of the Redis instance
-
channelType
-
getConnection
Retrieve the connection fromConnectionFuture. Performs a blockingConnectionFuture.get()to synchronize the channel/connection initialization. Any exception is rethrown asRedisConnectionException.- Type Parameters:
T- Connection type.- Parameters:
connectionFuture- must not be null.- Returns:
- the connection.
- Throws:
RedisConnectionException- in case of connection failures.- Since:
- 4.4
-
getConnection
Retrieve the connection fromConnectionFuture. Performs a blockingConnectionFuture.get()to synchronize the channel/connection initialization. Any exception is rethrown asRedisConnectionException.- Type Parameters:
T- Connection type.- Parameters:
connectionFuture- must not be null.- Returns:
- the connection.
- Throws:
RedisConnectionException- in case of connection failures.- Since:
- 5.0
-
initializeChannelAsync
protected <K, V, T extends RedisChannelHandler<K, V>> ConnectionFuture<T> initializeChannelAsync(ConnectionBuilder connectionBuilder)Connect and initialize a channel fromConnectionBuilder.- Parameters:
connectionBuilder- must not benull.- Returns:
- the
ConnectionFutureto synchronize the connection process. - Since:
- 4.4
-
shutdown
public void shutdown()Shutdown this client and close all open connections once this method is called. Once all connections are closed, the associatedClientResourcesare shut down/released gracefully considering quiet time and the shutdown timeout. The client should be discarded after calling shutdown. The shutdown is executed without quiet time and a timeout of 2TimeUnit.SECONDS. -
shutdown
Shutdown this client and close all open connections once this method is called. Once all connections are closed, the associatedClientResourcesare shut down/released gracefully considering quiet time and the shutdown timeout. The client should be discarded after calling shutdown.- Parameters:
quietPeriod- the quiet period to allow the executor gracefully shut down.timeout- the maximum amount of time to wait until the backing executor is shutdown regardless if a task was submitted during the quiet period.- Since:
- 5.0
- See Also:
EventExecutorGroup.shutdownGracefully(long, long, TimeUnit)
-
shutdown
Shutdown this client and close all open connections once this method is called. Once all connections are closed, the associatedClientResourcesare shut down/released gracefully considering quiet time and the shutdown timeout. The client should be discarded after calling shutdown.- Parameters:
quietPeriod- the quiet period to allow the executor gracefully shut down.timeout- the maximum amount of time to wait until the backing executor is shutdown regardless if a task was submitted during the quiet period.timeUnit- the unit ofquietPeriodandtimeout.- See Also:
EventExecutorGroup.shutdownGracefully(long, long, TimeUnit)
-
shutdownAsync
Shutdown this client and close all open connections asynchronously. Once all connections are closed, the associatedClientResourcesare shut down/released gracefully considering quiet time and the shutdown timeout. The client should be discarded after calling shutdown. The shutdown is executed without quiet time and a timeout of 2TimeUnit.SECONDS.- Since:
- 4.4
- See Also:
EventExecutorGroup.shutdownGracefully(long, long, TimeUnit)
-
shutdownAsync
Shutdown this client and close all open connections asynchronously. Once all connections are closed, the associatedClientResourcesare shut down/released gracefully considering quiet time and the shutdown timeout. The client should be discarded after calling shutdown.- Parameters:
quietPeriod- the quiet period to allow the executor gracefully shut down.timeout- the maximum amount of time to wait until the backing executor is shutdown regardless if a task was submitted during the quiet period.timeUnit- the unit ofquietPeriodandtimeout.- Since:
- 4.4
- See Also:
EventExecutorGroup.shutdownGracefully(long, long, TimeUnit)
-
createHandshake
-
setDefaultTimeout(Duration).