Module lettuce.core

Class AbstractRedisClient

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 Details

  • Constructor Details

    • AbstractRedisClient

      protected AbstractRedisClient​(ClientResources clientResources)
      Create a new instance with client resources.
      Parameters:
      clientResources - the client resources. If null, the client will create a new dedicated instance of client resources and keep track of them.
  • Method Details

    • getChannelCount

      protected int getChannelCount()
    • getDefaultTimeout

      public Duration getDefaultTimeout()
      Returns the default timeout for commands.
      Returns:
      the default timeout for commands.
    • setDefaultTimeout

      public void setDefaultTimeout​(Duration timeout)
      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 be null.
      Since:
      5.0
    • setDefaultTimeout

      @Deprecated public void setDefaultTimeout​(long timeout, TimeUnit unit)
      Deprecated.
      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 the ClientOptions 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 the ClientOptions for the client.
      Parameters:
      clientOptions - client options for the client and connections that are created after setting the options
    • getResources

      public ClientResources getResources()
      Returns the ClientResources 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 be null
    • removeListener

      public void removeListener​(RedisConnectionStateListener listener)
      Removes a listener.
      Parameters:
      listener - must not be null
    • 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 be null
      Since:
      6.1
    • removeListener

      public void removeListener​(CommandListener listener)
      Removes a listener.
      Parameters:
      listener - must not be null
      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-connect
      connectionBuilder - connection builder to configure the connection
      redisURI - URI of the Redis instance
    • channelType

      protected void channelType​(ConnectionBuilder connectionBuilder, ConnectionPoint connectionPoint)
    • getConnection

      protected <T> T getConnection​(ConnectionFuture<T> connectionFuture)
      Retrieve the connection from ConnectionFuture. Performs a blocking ConnectionFuture.get() to synchronize the channel/connection initialization. Any exception is rethrown as RedisConnectionException.
      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 from ConnectionFuture. Performs a blocking ConnectionFuture.get() to synchronize the channel/connection initialization. Any exception is rethrown as RedisConnectionException.
      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 from ConnectionBuilder.
      Parameters:
      connectionBuilder - must not be null.
      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 associated ClientResources 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 2 TimeUnit.SECONDS.
      See Also:
      EventExecutorGroup.shutdownGracefully(long, long, TimeUnit)
    • 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 associated ClientResources 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 associated ClientResources 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 of quietPeriod and timeout.
      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 associated ClientResources 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 2 TimeUnit.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 associated ClientResources 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 of quietPeriod and timeout.
      Since:
      4.4
      See Also:
      EventExecutorGroup.shutdownGracefully(long, long, TimeUnit)
    • createHandshake

      protected io.lettuce.core.RedisHandshake createHandshake​(ConnectionState state)