From bfb8ebc86242afa1706bf3806f215d5897922461 Mon Sep 17 00:00:00 2001 From: mohammed jasem alaajel <34905970+ham1255@users.noreply.github.com> Date: Fri, 25 Dec 2020 23:27:06 +0400 Subject: [PATCH] Fixed bug where connection manager is keep reconnecting when connection reconnect after failure --- .../net/limework/rediskript/RediSkript.java | 4 ++-- .../rediskript/managers/RedisController.java | 17 +++++++++++++---- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/src/main/java/net/limework/rediskript/RediSkript.java b/src/main/java/net/limework/rediskript/RediSkript.java index 233f7d6..509d0bc 100644 --- a/src/main/java/net/limework/rediskript/RediSkript.java +++ b/src/main/java/net/limework/rediskript/RediSkript.java @@ -26,12 +26,12 @@ public class RediSkript extends JavaPlugin { public void sendLogs(String message) { getLogger().info( - ChatColor.translateAlternateColorCodes('&', "&b[RediSkript]&e " + message) + ChatColor.translateAlternateColorCodes('&', "&b" + message) ); } public void sendErrorLogs(String message) { getLogger().severe( - ChatColor.translateAlternateColorCodes('&', "&b[RediSkript]&c " + message) + ChatColor.translateAlternateColorCodes('&', "&c" + message) ); } diff --git a/src/main/java/net/limework/rediskript/managers/RedisController.java b/src/main/java/net/limework/rediskript/managers/RedisController.java index 1cb4b00..5d52d47 100644 --- a/src/main/java/net/limework/rediskript/managers/RedisController.java +++ b/src/main/java/net/limework/rediskript/managers/RedisController.java @@ -33,6 +33,7 @@ public class RedisController extends BinaryJedisPubSub implements Runnable { private byte[][] channelsInByte; private final AtomicBoolean isConnectionBroken; + private final AtomicBoolean isConnecting; private final RediSkript plugin; private final BukkitTask ConnectionTask; @@ -58,30 +59,38 @@ public class RedisController extends BinaryJedisPubSub implements Runnable { encryption = new Encryption(config); setupChannels(config); isConnectionBroken = new AtomicBoolean(true); + isConnecting = new AtomicBoolean(false); //Start the main task on async thread - ConnectionTask = plugin.getServer().getScheduler().runTaskTimerAsynchronously(plugin,this, 0 , 20 * 5 ); + ConnectionTask = plugin.getServer().getScheduler().runTaskTimerAsynchronously(plugin, this, 0, 20 * 5); } @Override public void run() { - if (!isConnectionBroken.get()) { + if (!isConnectionBroken.get() || isConnecting.get()) { return; } plugin.sendLogs("Connecting to redis......"); + isConnecting.set(true); try (Jedis jedis = jedisPool.getResource()) { isConnectionBroken.set(false); plugin.sendLogs("&aConnection to redis has established!"); jedis.subscribe(this, channelsInByte); - } catch (Exception e) { + isConnecting.set(false); isConnectionBroken.set(true); plugin.sendLogs("Connection has &kFAILED &cor Unable to connect to redis retrying to make connection..."); + e.printStackTrace(); } } public void shutdown() { ConnectionTask.cancel(); - this.unsubscribe(); + try { + this.unsubscribe(); + } catch (Exception e) { + plugin.sendErrorLogs("Something went wrong during unsubscribing..."); + } + jedisPool.close(); }