Class RedisConnectionManager
java.lang.Object
org.gcube.informationsystem.resourceregistry.redis.RedisConnectionManager
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 TypeMethodDescriptionvoidclose()Closes the active Redis connection and releases client resources.longDeletes every key of this registry (RedisKeySpace.ALL_SCAN_PATTERN) with a SCAN loop, leaving any other key of the same Redis untouched.voidWrites every command queued since auto-flushing was disabled.Returns asynchronous Redis commands if available, otherwisenull.Returns synchronous Redis commands if available, otherwisenull.static RedisConnectionManagerReturns the singleton instance ofRedisConnectionManager.booleanReturnstrueif the Redis connection is established and open.voidPublishes a message on the given Redis pub/sub channel, broadcasting it to every subscribed service instance.voidsetAutoFlushCommands(boolean autoFlush) Enables or disables automatic flushing on the shared connection.voidSubscribes to the given Redis pub/sub channel, invokingonMessagefor every message received on it for as long as the process runs.
-
Method Details
-
getInstance
Returns the singleton instance ofRedisConnectionManager.- Returns:
- the singleton instance
-
isAvailable
public boolean isAvailable()Returnstrueif the Redis connection is established and open. It never waits: a lost connection is established again in the background.- Returns:
trueif the connection is available,falseotherwise
-
getCommands
Returns synchronous Redis commands if available, otherwisenull. Callers must checkisAvailable()or handle anullreturn.- Returns:
- synchronous Redis commands interface, or
nullif unavailable
-
getAsyncCommands
Returns asynchronous Redis commands if available, otherwisenull.Intended for bulk writes that would otherwise pay one network round trip per command. Disable auto-flushing on the returned handle, issue the commands, flush once, then await the futures — and always restore auto-flushing afterwards, since the handle is backed by the shared connection every other caller uses.
- Returns:
- asynchronous Redis commands interface, or
nullif unavailable
-
setAutoFlushCommands
public void setAutoFlushCommands(boolean autoFlush) Enables or disables automatic flushing on the shared connection.Disabling it lets a caller queue many commands and pay a single network round trip for all of them. The setting is a property of the connection every other caller shares, so a caller that disables it must re-enable it before returning, including on failure.
- Parameters:
autoFlush-trueto flush every command as it is issued,falseto queue untilflushCommands()
-
flushCommands
public void flushCommands()Writes every command queued since auto-flushing was disabled. -
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
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 namemessage- the message payload
-
subscribe
Subscribes to the given Redis pub/sub channel, invokingonMessagefor 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 fromconnection). 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 toonMessage- callback invoked with the message payload for every message received
-
close
public void close()Closes the active Redis connection and releases client resources.
-