Class 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 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
    • Constructor Detail

      • 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 Detail

      • getChannelCount

        protected int getChannelCount()
      • setDefaultTimeout

        @Deprecated
        public void setDefaultTimeout​(Duration timeout)
        Deprecated.
        since 6.2, use RedisURI.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 be null.
        Since:
        5.0
      • setDefaultTimeout

        @Deprecated
        public void setDefaultTimeout​(long timeout,
                                      TimeUnit unit)
        Deprecated.
        since 6.2, use RedisURI.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 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
      • 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
      • 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
      • 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-connect
        connectionBuilder - connection builder to configure the connection
        connectionEvents - connection events dispatcher
        redisURI - URI of the Redis instance
        Since:
        6.2
      • 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​(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)