Class RedisManager

java.lang.Object
cz.foresttech.forestredis.shared.RedisManager

public class RedisManager extends Object
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 Details

    • RedisManager

      public RedisManager(IForestRedisPlugin plugin, String serverIdentifier, RedisConfiguration redisConfiguration)
      Constructor method for creating RedisManager instance. Constructor does not subscribe to the channels, it just stores the provided data for the future.
      Parameters:
      plugin - Origin plugin which tries to obtain the instance
      serverIdentifier - Identifier of the server (e.g. 'Bungee01'). Shall be unique to prevent bugs
      redisConfiguration - RedisConfiguration object with Redis server credentials
  • Method Details

    • reload

      public void reload(String serverIdentifier, RedisConfiguration redisConfiguration, boolean keepChannels)
      Reloads the manager while keeping already subscribed channels if set.
      Parameters:
      serverIdentifier - New server identifier (if null, already using server id will be used)
      redisConfiguration - New RedisConfiguration (if null, already using configuration will be used)
      keepChannels - Keep already subscribed channels
    • setup

      public boolean setup(String... channels)
      Primary setup method which establishes JedisPool from the redisConfiguration. Method then automatically subscribes to channels.

      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

      public void unsubscribe(String... channels)
      Unsubscribes the channels given.
      Parameters:
      channels - Names of the channels to unsubscribe (case-sensitive)
    • subscribe

      public boolean subscribe(String... channels)
      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

      public boolean publishObject(String targetChannel, Object objectToPublish)
      Publishes the object to the provided channel. Also handles server identification. DO NOT USE this to publish simple String 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

      public boolean publishMessage(String targetChannel, String messageToPublish)
      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

      public String getServerIdentifier()
      Returns the current server identifier. It is used as sender name in events.
      Returns:
      Server identifier
    • isSubscribed

      public boolean isSubscribed(String channel)
      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

      public Set<String> getSubscribedChannels()
      Returns list of all subscribed channels.
      Returns:
      List of all subscribed channels (case-sensitive)
    • init

      public static void init(IForestRedisPlugin plugin, String serverIdentifier, RedisConfiguration redisConfiguration)
      Initialization method for creating RedisManager main instance. This won't start any connection or subscription.
      Parameters:
      plugin - Origin plugin which tries to obtain the instance
      serverIdentifier - Identifier of the server (e.g. 'Bungee01'). Shall be unique to prevent bugs
      redisConfiguration - RedisConfiguration object with Redis server credentials
    • getAPI

      public static RedisManager getAPI()
      Gets the main instance of RedisManager object. This is the only recommended approach to access the API methods.
      Returns:
      Main instance of RedisManager