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 creatingRedisManager
instance. -
Method Summary
Modifier and TypeMethodDescriptionvoid
close()
Closes the Redis connection and unsubscribes to all channels.static RedisManager
getAPI()
Gets the main instance ofRedisManager
object.Returns the current server identifier.Returns list of all subscribed channels.static void
init
(IForestRedisPlugin plugin, String serverIdentifier, RedisConfiguration redisConfiguration) Initialization method for creatingRedisManager
main instance.boolean
isSubscribed
(String channel) Returns whether the channel is subscribed or not.boolean
publishMessage
(String targetChannel, String messageToPublish) Publishes the message to the provided channel.boolean
publishObject
(String targetChannel, Object objectToPublish) Publishes the object to the provided channel.void
reload
(String serverIdentifier, RedisConfiguration redisConfiguration, boolean keepChannels) Reloads the manager while keeping already subscribed channels if set.boolean
Primary setup method which establishesJedisPool
from theredisConfiguration
.boolean
Subscribes to the provided channels if they're not subscribed already.void
unsubscribe
(String... channels) Unsubscribes the channels given.
-
Constructor Details
-
Method Details
-
setup
Primary setup method which establishesJedisPool
from 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 simpleString
message.- 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 ofRedisManager
object. This is the only recommended approach to access the API methods.- Returns:
- Main instance of
RedisManager