Fixed bug where connection manager is keep reconnecting when connection reconnect after failure

This commit is contained in:
mohammed jasem alaajel 2020-12-25 23:27:06 +04:00
parent 029222a068
commit bfb8ebc862
2 changed files with 15 additions and 6 deletions

View File

@ -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)
);
}

View File

@ -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();
}