Class RedisConnectionManager

java.lang.Object
org.gcube.informationsystem.resourceregistry.redis.RedisConnectionManager

public class RedisConnectionManager extends Object
Singleton manager for the Lettuce Redis connection. Provides graceful degradation: if Redis is unavailable, isAvailable() returns false and callers must fall back to the database.
Author:
Luca Frosini (ISTI - CNR)
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Closes the active Redis connection and releases client resources.
    long
    Deletes every key of this registry (RedisKeySpace.ALL_SCAN_PATTERN) with a SCAN loop, leaving any other key of the same Redis untouched.
    io.lettuce.core.api.sync.RedisCommands<String,String>
    Returns synchronous Redis commands if available, otherwise null.
    Returns the singleton instance of RedisConnectionManager.
    boolean
    Returns true if the Redis connection is established and open.
    io.lettuce.core.api.StatefulRedisConnection<String,String>
    Opens a connection of its own for a bulk write, with auto-flushing disabled: the caller issues its commands through async(), writes them with flushCommands() in a single network round trip, awaits the futures and closes the connection.
    void
    publish(String channel, String message)
    Publishes a message on the given Redis pub/sub channel, broadcasting it to every subscribed service instance.
    void
    subscribe(String channel, Consumer<String> onMessage)
    Subscribes to the given Redis pub/sub channel, invoking onMessage for every message received on it for as long as the process runs.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • getInstance

      public static RedisConnectionManager getInstance()
      Returns the singleton instance of RedisConnectionManager.
      Returns:
      the singleton instance
    • isAvailable

      public boolean isAvailable()
      Returns true if the Redis connection is established and open. It never waits: a lost connection is established again in the background.
      Returns:
      true if the connection is available, false otherwise
    • getCommands

      public io.lettuce.core.api.sync.RedisCommands<String,String> getCommands()
      Returns synchronous Redis commands if available, otherwise null. Callers must check isAvailable() or handle a null return.
      Returns:
      synchronous Redis commands interface, or null if unavailable
    • openBulkConnection

      public io.lettuce.core.api.StatefulRedisConnection<String,String> openBulkConnection()
      Opens a connection of its own for a bulk write, with auto-flushing disabled: the caller issues its commands through async(), writes them with flushCommands() in a single network round trip, awaits the futures and closes the connection. The shared connection keeps flushing every command, so the other callers never wait for the batch.
      Returns:
      a new connection with auto-flushing disabled, or null if Redis is unavailable
    • deleteAllKeys

      public long deleteAllKeys()
      Deletes every key of this registry (RedisKeySpace.ALL_SCAN_PATTERN) with a SCAN loop, leaving any other key of the same Redis untouched.
      Returns:
      the number of deleted keys
      Throws:
      IllegalStateException - if Redis is not reachable
    • publish

      public void publish(String channel, String message)
      Publishes a message on the given Redis pub/sub channel, broadcasting it to every subscribed service instance. A no-op (silently) if Redis is unavailable.
      Parameters:
      channel - the pub/sub channel name
      message - the message payload
    • subscribe

      public void subscribe(String channel, Consumer<String> onMessage)
      Subscribes to the given Redis pub/sub channel, invoking onMessage for every message received on it for as long as the process runs. All subscriptions on this manager share a single dedicated pub/sub connection (a connection in subscriber mode cannot also issue regular commands, so it must be separate from connection). The subscription outlives the connection: it is established as soon as Redis is reachable and again after every reconnection.
      Parameters:
      channel - the pub/sub channel name to subscribe to
      onMessage - callback invoked with the message payload for every message received
    • close

      public void close()
      Closes the active Redis connection and releases client resources.