Package io.lettuce.core
Class AbstractRedisClient
- java.lang.Object
-
- io.lettuce.core.AbstractRedisClient
-
- All Implemented Interfaces:
AutoCloseable
- Direct Known Subclasses:
RedisClient
,RedisClusterClient
public abstract class AbstractRedisClient extends Object implements AutoCloseable
Base Redis client. This class holds the netty infrastructure,ClientOptions
and the basic connection procedure. This class creates the nettyEventLoopGroup
s for NIO (NioEventLoopGroup
) and EPoll (EpollEventLoopGroup
) with a default ofRuntime.getRuntime().availableProcessors() * 4
threads. Reuse the instance as much as possible since theEventLoopGroup
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 theio.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 ChannelGroup
channels
protected Set<Closeable>
closeableResources
protected ConnectionEvents
connectionEvents
-
Constructor Summary
Constructors Modifier Constructor Description protected
AbstractRedisClient(ClientResources clientResources)
Create a new instance with client resources.
-
Method Summary
All Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description void
addListener(CommandListener listener)
Add a listener for Redis Command events.void
addListener(RedisConnectionStateListener listener)
Add a listener for the RedisConnectionState.protected void
channelType(ConnectionBuilder connectionBuilder, ConnectionPoint connectionPoint)
void
close()
protected void
connectionBuilder(Mono<SocketAddress> socketAddressSupplier, ConnectionBuilder connectionBuilder, ConnectionEvents connectionEvents, RedisURI redisURI)
Populate connection builder with necessary resources.protected void
connectionBuilder(Mono<SocketAddress> socketAddressSupplier, ConnectionBuilder connectionBuilder, RedisURI redisURI)
Populate connection builder with necessary resources.protected io.lettuce.core.RedisHandshake
createHandshake(ConnectionState state)
protected int
getChannelCount()
protected List<CommandListener>
getCommandListeners()
protected <T> T
getConnection(ConnectionFuture<T> connectionFuture)
Retrieve the connection fromConnectionFuture
.protected <T> T
getConnection(CompletableFuture<T> connectionFuture)
Retrieve the connection fromConnectionFuture
.Duration
getDefaultTimeout()
Deprecated.since 6.2, useRedisURI.getTimeout()
to control timeouts.ClientOptions
getOptions()
Returns theClientOptions
which are valid for that client.protected int
getResourceCount()
ClientResources
getResources()
Returns theClientResources
which are used with that client.protected <K,V,T extends RedisChannelHandler<K,V>>
ConnectionFuture<T>initializeChannelAsync(ConnectionBuilder connectionBuilder)
Connect and initialize a channel fromConnectionBuilder
.void
removeListener(CommandListener listener)
Removes a listener.void
removeListener(RedisConnectionStateListener listener)
Removes a listener.void
setDefaultTimeout(long timeout, TimeUnit unit)
Deprecated.since 6.2, useRedisURI.getTimeout()
to control timeouts.void
setDefaultTimeout(Duration timeout)
Deprecated.since 6.2, useRedisURI.getTimeout()
to control timeouts.protected void
setOptions(ClientOptions clientOptions)
Set theClientOptions
for the client.void
shutdown()
Shutdown this client and close all open connections once this method is called.void
shutdown(long quietPeriod, long timeout, TimeUnit timeUnit)
Shutdown this client and close all open connections once this method is called.void
shutdown(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 Detail
-
connectionEvents
protected final ConnectionEvents connectionEvents
-
channels
protected final ChannelGroup channels
-
-
Constructor Detail
-
AbstractRedisClient
protected AbstractRedisClient(ClientResources clientResources)
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 Detail
-
getChannelCount
protected int getChannelCount()
-
getDefaultTimeout
@Deprecated public Duration getDefaultTimeout()
Deprecated.since 6.2, useRedisURI.getTimeout()
to control timeouts.Returns the defaulttimeout
for commands.- Returns:
- the default
timeout
for commands.
-
setDefaultTimeout
@Deprecated public void setDefaultTimeout(Duration timeout)
Deprecated.since 6.2, useRedisURI.getTimeout()
to control timeouts.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 public void setDefaultTimeout(long timeout, TimeUnit unit)
Deprecated.since 6.2, useRedisURI.getTimeout()
to control timeouts.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
public ClientOptions getOptions()
Returns theClientOptions
which 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
ClientOptions
for this client
-
setOptions
protected void setOptions(ClientOptions clientOptions)
Set theClientOptions
for the client.- Parameters:
clientOptions
- client options for the client and connections that are created after setting the options
-
getResources
public ClientResources getResources()
Returns theClientResources
which are used with that client.- Returns:
- the
ClientResources
for this client. - Since:
- 6.0
-
getResourceCount
protected int getResourceCount()
-
addListener
public void addListener(RedisConnectionStateListener listener)
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
public void removeListener(RedisConnectionStateListener listener)
Removes a listener.- Parameters:
listener
- must not benull
.
-
addListener
public void addListener(CommandListener listener)
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
public void removeListener(CommandListener listener)
Removes a listener.- Parameters:
listener
- must not benull
.- Since:
- 6.1
-
getCommandListeners
protected List<CommandListener> 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
-
connectionBuilder
protected void connectionBuilder(Mono<SocketAddress> socketAddressSupplier, ConnectionBuilder connectionBuilder, ConnectionEvents connectionEvents, RedisURI redisURI)
Populate connection builder with necessary resources.- Parameters:
socketAddressSupplier
- address supplier for initial connect and re-connectconnectionBuilder
- connection builder to configure the connectionconnectionEvents
- connection events dispatcherredisURI
- URI of the Redis instance- Since:
- 6.2
-
channelType
protected void channelType(ConnectionBuilder connectionBuilder, ConnectionPoint connectionPoint)
-
getConnection
protected <T> T getConnection(ConnectionFuture<T> connectionFuture)
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
protected <T> T getConnection(CompletableFuture<T> connectionFuture)
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
ConnectionFuture
to 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 associatedClientResources
are 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
.
-
close
public void close()
- Specified by:
close
in interfaceAutoCloseable
-
shutdown
public void shutdown(Duration quietPeriod, Duration timeout)
Shutdown this client and close all open connections once this method is called. Once all connections are closed, the associatedClientResources
are 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
public void shutdown(long quietPeriod, long timeout, TimeUnit timeUnit)
Shutdown this client and close all open connections once this method is called. Once all connections are closed, the associatedClientResources
are 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 ofquietPeriod
andtimeout
.- See Also:
EventExecutorGroup.shutdownGracefully(long, long, TimeUnit)
-
shutdownAsync
public CompletableFuture<Void> shutdownAsync()
Shutdown this client and close all open connections asynchronously. Once all connections are closed, the associatedClientResources
are 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
public CompletableFuture<Void> shutdownAsync(long quietPeriod, long timeout, TimeUnit timeUnit)
Shutdown this client and close all open connections asynchronously. Once all connections are closed, the associatedClientResources
are 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 ofquietPeriod
andtimeout
.- Since:
- 4.4
- See Also:
EventExecutorGroup.shutdownGracefully(long, long, TimeUnit)
-
createHandshake
protected io.lettuce.core.RedisHandshake createHandshake(ConnectionState state)
-
-