Package cz.foresttech.forestredis.shared
Class RedisManager
java.lang.Object
cz.foresttech.forestredis.shared.RedisManager
Class for maintaining and handling connection to Redis server.
It automatically fires Bungee/Spigot events corresponding to current server type.
It allows developers to subscribe channels and listen to them using generic EventHandlers.
-
Constructor Summary
ConstructorsConstructorDescriptionRedisManager(IForestRedisPlugin plugin, String serverIdentifier, RedisConfiguration redisConfiguration) Constructor method for creatingRedisManagerinstance. -
Method Summary
Modifier and TypeMethodDescriptionvoidclose()Closes the Redis connection and unsubscribes to all channels.static RedisManagergetAPI()Gets the main instance ofRedisManagerobject.Returns the current server identifier.Returns list of all subscribed channels.static voidinit(IForestRedisPlugin plugin, String serverIdentifier, RedisConfiguration redisConfiguration) Initialization method for creatingRedisManagermain instance.booleanisSubscribed(String channel) Returns whether the channel is subscribed or not.booleanpublishMessage(String targetChannel, String messageToPublish) Publishes the message to the provided channel.booleanpublishObject(String targetChannel, Object objectToPublish) Publishes the object to the provided channel.voidreload(String serverIdentifier, RedisConfiguration redisConfiguration, boolean keepChannels) Reloads the manager while keeping already subscribed channels if set.booleanPrimary setup method which establishesJedisPoolfrom theredisConfiguration.booleanSubscribes to the provided channels if they're not subscribed already.voidunsubscribe(String... channels) Unsubscribes the channels given.
-
Constructor Details
-
Method Details
-
setup
Primary setup method which establishesJedisPoolfrom theredisConfiguration. Method then automatically subscribes tochannels.Note! It does not return false if subscription itself was unsuccessful as the calls are asynchronous.
- Parameters:
channels- Default list channels to listen on (case-sensitive), can be empty and provided afterwards- Returns:
- Whether the setup was successful
- See Also:
-
unsubscribe
Unsubscribes the channels given.- Parameters:
channels- Names of the channels to unsubscribe (case-sensitive)
-
subscribe
Subscribes to the provided channels if they're not subscribed already. Corresponding Events will be thrown only if the received message is sent to subscribed channels.- Parameters:
channels- Names of the channels to subscribe (case-sensitive)- Returns:
- Whether at least one of the channel was successfully subscribed
-
publishObject
Publishes the object to the provided channel. Also handles server identification. DO NOT USE this to publish simpleStringmessage.- Parameters:
targetChannel- Channel to be published into (case-sensitive)objectToPublish- Object to be published- Returns:
- Returns 'false' if the message cannot be converted to JSON or in closing state. Returns 'true' if the process was successful
- See Also:
-
publishMessage
Publishes the message to the provided channel. Also handles server identification. This method is not recommended for publishing serialized objects. To publish objects:- Parameters:
targetChannel- Channel to be published into (case-sensitive)messageToPublish- The message to be published- Returns:
- Returns 'false' if the message cannot be converted to JSON or in closing state. Returns 'true' if the process was successful.
- See Also:
-
close
public void close()Closes the Redis connection and unsubscribes to all channels. -
getServerIdentifier
Returns the current server identifier. It is used as sender name in events.- Returns:
- Server identifier
-
isSubscribed
Returns whether the channel is subscribed or not.- Parameters:
channel- Channel's name to check (case-sensitive).- Returns:
- Whether the channel is subscribed or not
-
getSubscribedChannels
Returns list of all subscribed channels.- Returns:
- List of all subscribed channels (case-sensitive)
-
getAPI
Gets the main instance ofRedisManagerobject. This is the only recommended approach to access the API methods.- Returns:
- Main instance of
RedisManager