From 8fb3ab8c522c47636c2ad9d8fb41d14afaf3364b Mon Sep 17 00:00:00 2001 From: William Date: Sat, 15 Jan 2022 19:01:27 +0000 Subject: [PATCH] Update to Jedis 4.0.1; Use Jedis pooling; Close #122 --- pom.xml | 2 +- .../me/william278/huskhomes2/HuskHomes.java | 1 + .../data/message/redis/RedisMessage.java | 4 +- .../data/message/redis/RedisReceiver.java | 140 ++++++++++-------- 4 files changed, 82 insertions(+), 65 deletions(-) diff --git a/pom.xml b/pom.xml index 24d4a28e..662b67de 100644 --- a/pom.xml +++ b/pom.xml @@ -186,7 +186,7 @@ redis.clients jedis - 3.7.1 + 4.0.1 jar provided diff --git a/src/main/java/me/william278/huskhomes2/HuskHomes.java b/src/main/java/me/william278/huskhomes2/HuskHomes.java index 43dac8dc..8004ca20 100644 --- a/src/main/java/me/william278/huskhomes2/HuskHomes.java +++ b/src/main/java/me/william278/huskhomes2/HuskHomes.java @@ -127,6 +127,7 @@ public final class HuskHomes extends JavaPlugin { if (getSettings().getMessengerType().equalsIgnoreCase("pluginmessage")) { Bukkit.getMessenger().registerIncomingPluginChannel(this, "BungeeCord", new PluginMessageReceiver()); } else { + RedisReceiver.initialize(); RedisReceiver.listen(); } } diff --git a/src/main/java/me/william278/huskhomes2/data/message/redis/RedisMessage.java b/src/main/java/me/william278/huskhomes2/data/message/redis/RedisMessage.java index 31fd0569..1b41c7ae 100644 --- a/src/main/java/me/william278/huskhomes2/data/message/redis/RedisMessage.java +++ b/src/main/java/me/william278/huskhomes2/data/message/redis/RedisMessage.java @@ -25,7 +25,7 @@ public class RedisMessage extends Message { private void dispatchRedisMessage(String target) { Bukkit.getScheduler().runTask(plugin, () -> { - try (Jedis publisher = new Jedis(HuskHomes.getSettings().getRedisHost(), HuskHomes.getSettings().getRedisPort())) { + try (Jedis publisher = RedisReceiver.getJedis()) { final String jedisPassword = HuskHomes.getSettings().getRedisPassword(); if (!jedisPassword.equals("")) { publisher.auth(jedisPassword); @@ -48,6 +48,6 @@ public class RedisMessage extends Message { @Override public void sendToServer(Player sender, String server) { - dispatchRedisMessage("server-" + server + ""); + dispatchRedisMessage("server-" + server + ""); } } diff --git a/src/main/java/me/william278/huskhomes2/data/message/redis/RedisReceiver.java b/src/main/java/me/william278/huskhomes2/data/message/redis/RedisReceiver.java index c4a91886..9939b072 100644 --- a/src/main/java/me/william278/huskhomes2/data/message/redis/RedisReceiver.java +++ b/src/main/java/me/william278/huskhomes2/data/message/redis/RedisReceiver.java @@ -5,6 +5,8 @@ import me.william278.huskhomes2.data.message.CrossServerMessageHandler; import org.bukkit.Bukkit; import org.bukkit.entity.Player; import redis.clients.jedis.Jedis; +import redis.clients.jedis.JedisPool; +import redis.clients.jedis.JedisPoolConfig; import redis.clients.jedis.JedisPubSub; import java.util.ArrayList; @@ -12,6 +14,19 @@ import java.util.Collections; public class RedisReceiver { + private static JedisPool jedisPool; + + public static Jedis getJedis() { + return jedisPool.getResource(); + } + + public static void initialize() { + jedisPool = new JedisPool(new JedisPoolConfig(), + HuskHomes.getSettings().getRedisHost(), + HuskHomes.getSettings().getRedisPort()); + } + + private static final HuskHomes plugin = HuskHomes.getInstance(); public static final String REDIS_CHANNEL = "HuskHomes"; @@ -25,72 +40,73 @@ public class RedisReceiver { } public static void listen() { - Jedis jedis = new Jedis(HuskHomes.getSettings().getRedisHost(), HuskHomes.getSettings().getRedisPort()); final String jedisPassword = HuskHomes.getSettings().getRedisPassword(); - if (!jedisPassword.equals("")) { - jedis.auth(jedisPassword); - } - jedis.connect(); - if (jedis.isConnected()) { - plugin.getLogger().info("Enabled Redis listener successfully!"); - new Thread(() -> jedis.subscribe(new JedisPubSub() { - @Override - public void onMessage(String channel, String message) { - if (!channel.equals(REDIS_CHANNEL)) { - return; - } - - /* HuskHomes Redis messages have a header formatted as such: - * ::£ followed by the message arguments and data. */ - final String[] splitMessage = message.split(RedisMessage.REDIS_MESSAGE_HEADER_SEPARATOR); - final String messageHeader = splitMessage[0]; - int clusterID; - - // Ensure the cluster ID matches - try { - clusterID = Integer.parseInt(messageHeader.split(":")[0]); - } catch (Exception e) { - // In case the message is malformed or the cluster ID is invalid - HuskHomes.getInstance().getLogger().warning("Received a Redis message on the HuskHomes channel with an invalid server Cluster ID! \n" + - "Please ensure that the cluster ID is set to a valid integer on all servers."); - return; - } - if (HuskHomes.getSettings().getClusterId() != clusterID) { - return; - } - - // Get the type of redis message - final String messageType = messageHeader.split(":")[1]; - - // Get the player targeted by the message and make sure they are online - final String target = messageHeader.split(":")[2]; - Player receiver = Bukkit.getPlayerExact(target); - - // If the redis message was targeting this server - if (target.equalsIgnoreCase("-all-")) { - receiver = getRandomReceiver(); - } else if (target.contains("server-")) { //todo In the future, allow redis messages to execute without a player online. For now it's duplicating the behaviour of plugin messages for consistency. - if (target.split("-")[1].equalsIgnoreCase(HuskHomes.getSettings().getServerID())) { - receiver = getRandomReceiver(); - } else { - return; // The message was targeting another server; ignore + try (Jedis jedis = getJedis()) { + if (!jedisPassword.equals("")) { + jedis.auth(jedisPassword); + } + jedis.connect(); + if (jedis.isConnected()) { + plugin.getLogger().info("Enabled Redis listener successfully!"); + new Thread(() -> jedis.subscribe(new JedisPubSub() { + @Override + public void onMessage(String channel, String message) { + if (!channel.equals(REDIS_CHANNEL)) { + return; } - } - // Return if the target is not available - if (receiver == null) { - return; - } - if (!receiver.isOnline()) { - return; - } + /* HuskHomes Redis messages have a header formatted as such: + * ::£ followed by the message arguments and data. */ + final String[] splitMessage = message.split(RedisMessage.REDIS_MESSAGE_HEADER_SEPARATOR); + final String messageHeader = splitMessage[0]; + int clusterID; - final String messageData = splitMessage[1]; - CrossServerMessageHandler.handlePluginMessage(new RedisMessage(clusterID, receiver.getName(), messageType, messageData), receiver); - } - }, REDIS_CHANNEL), "Redis Subscriber").start(); - } else { - plugin.getLogger().severe("Failed to initialize the redis listener!"); + // Ensure the cluster ID matches + try { + clusterID = Integer.parseInt(messageHeader.split(":")[0]); + } catch (Exception e) { + // In case the message is malformed or the cluster ID is invalid + HuskHomes.getInstance().getLogger().warning("Received a Redis message on the HuskHomes channel with an invalid server Cluster ID! \n" + + "Please ensure that the cluster ID is set to a valid integer on all servers."); + return; + } + if (HuskHomes.getSettings().getClusterId() != clusterID) { + return; + } + + // Get the type of redis message + final String messageType = messageHeader.split(":")[1]; + + // Get the player targeted by the message and make sure they are online + final String target = messageHeader.split(":")[2]; + Player receiver = Bukkit.getPlayerExact(target); + + // If the redis message was targeting this server + if (target.equalsIgnoreCase("-all-")) { + receiver = getRandomReceiver(); + } else if (target.contains("server-")) { //todo In the future, allow redis messages to execute without a player online. For now it's duplicating the behaviour of plugin messages for consistency. + if (target.split("-")[1].equalsIgnoreCase(HuskHomes.getSettings().getServerID())) { + receiver = getRandomReceiver(); + } else { + return; // The message was targeting another server; ignore + } + } + + // Return if the target is not available + if (receiver == null) { + return; + } + if (!receiver.isOnline()) { + return; + } + + final String messageData = splitMessage[1]; + CrossServerMessageHandler.handlePluginMessage(new RedisMessage(clusterID, receiver.getName(), messageType, messageData), receiver); + } + }, REDIS_CHANNEL), "Redis Subscriber").start(); + } else { + plugin.getLogger().severe("Failed to initialize the redis listener!"); + } } } }