New Reconnector hopefuly works

This commit is contained in:
ham1255
2020-06-14 17:43:46 +04:00
parent 3dffba07df
commit 39e9536e8f
3 changed files with 47 additions and 39 deletions

View File

@@ -7,19 +7,20 @@ import org.cryptomator.siv.UnauthenticCiphertextException;
import org.json.JSONObject;
import redis.clients.jedis.BinaryJedis;
import redis.clients.jedis.BinaryJedisPubSub;
import redis.clients.jedis.exceptions.JedisConnectionException;
import javax.crypto.IllegalBlockSizeException;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;
public class RedisSub extends BinaryJedisPubSub implements Runnable{
private AddonPlugin plugin;
private BinaryJedis j;
private Boolean isShuttingDown = false;
private List<String> channels;
private AtomicBoolean isShuttingDown = new AtomicBoolean(false);
private AtomicBoolean isRedisOnline = new AtomicBoolean();
public RedisSub(AddonPlugin plugin, BinaryJedis j, List<String> channels) {
this.plugin = plugin;
@@ -29,35 +30,34 @@ public class RedisSub extends BinaryJedisPubSub implements Runnable{
@Override
public void run(){
try{
this.j.subscribe(this, channels.get(0).getBytes(), channels.get(1).getBytes(), channels.get(2).getBytes(), channels.get(3).getBytes(), channels.get(4).getBytes());
} catch (Exception je){
plugin.getLogger().warning("Lost connection to redis!");
newJedis();
}
}
private void newJedis() {
//this.unsubscribe();
this.j.close();
while (!isShuttingDown){
while (!isShuttingDown.get()){
try {
plugin.getLogger().info("reconnecting to Redis!");
this.j = plugin.getJedisPool().getResource();
plugin.getLogger().info("Connected!");
break;
}catch (Exception e){
plugin.getLogger().warning("reconnecting to Redis has Failed! retrying in 4 seconds!");
try { Thread.sleep(4000);}catch (InterruptedException ignored){}
message("&e[Jedis] &cConnecting to redis...........");
if (!this.j.isConnected()) this.j = plugin.getJedisPool().getResource();
isRedisOnline.set(true);
message("&e[Jedis] &aRedis Connected");
this.j.subscribe(this,
channels.get(0).getBytes(),
channels.get(1).getBytes(),
channels.get(2).getBytes(),
channels.get(3).getBytes(),
channels.get(4).getBytes());
} catch (Exception e){
message("&e[Jedis] &cConnection to redis has failed! &ereconnecting...");
this.j.close();
isRedisOnline.set(false);
}
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
if (isShuttingDown) return;
plugin.getJedisExecutionService().execute(this);
}
private void message(String message){
plugin.getLogger().info(ChatColor.translateAlternateColorCodes('&', message));
}
@Override
public void onMessage(byte[] channel, byte[] message) {
@@ -83,9 +83,13 @@ public class RedisSub extends BinaryJedisPubSub implements Runnable{
}
public void shutdown(){
isShuttingDown = true;
this.isShuttingDown.set(true);
this.unsubscribe();
j.close();
}
public boolean IsRedisOnline() {
return isRedisOnline.get();
}
}