Fix Redis hanging

This commit is contained in:
Telesphoreo 2022-02-04 13:30:05 -06:00
parent c93bd4fd2c
commit 140f298018
5 changed files with 41 additions and 15 deletions

View File

@ -70,13 +70,6 @@ public class Plex extends JavaPlugin
sqlConnection = new SQLConnection(); sqlConnection = new SQLConnection();
mongoConnection = new MongoConnection(); mongoConnection = new MongoConnection();
redisConnection = new RedisConnection(); redisConnection = new RedisConnection();
/*try {
redisConnection.openPool();
PlexLog.log("Successfully opened redis pool. Closing.");
} catch (Exception e) {
e.printStackTrace();
}
redisConnection.getJedis().close();*/
} }
@Override @Override
@ -103,7 +96,7 @@ public class Plex extends JavaPlugin
if (redisConnection.isEnabled()) if (redisConnection.isEnabled())
{ {
redisConnection.openPool(); redisConnection.getJedis();
} }
else else
{ {

View File

@ -50,6 +50,7 @@ public class PlexCMD extends PlexCommand
plugin.getRedisConnection().getJedis().set("test", "123"); plugin.getRedisConnection().getJedis().set("test", "123");
send(sender, "Set test to 123. Now outputting key test..."); send(sender, "Set test to 123. Now outputting key test...");
send(sender, plugin.getRedisConnection().getJedis().get("test")); send(sender, plugin.getRedisConnection().getJedis().get("test"));
plugin.getRedisConnection().getJedis().close();
} }
else else
{ {

View File

@ -98,6 +98,7 @@ public class PunishedPlayer extends PlexBase
Punishment punishment = Punishment.fromJson(obj.toString()); Punishment punishment = Punishment.fromJson(obj.toString());
punishments.add(punishment); punishments.add(punishment);
}); });
plugin.getRedisConnection().getJedis().close();
return punishments; return punishments;
} }

View File

@ -41,6 +41,7 @@ public class PunishmentManager extends PlexBase
{ {
plugin.getRedisConnection().getJedis().set(player.getUuid(), object.toString()); plugin.getRedisConnection().getJedis().set(player.getUuid(), object.toString());
PlexLog.debug("Added " + player.getUuid() + "'s punishment to the Redis database."); PlexLog.debug("Added " + player.getUuid() + "'s punishment to the Redis database.");
plugin.getRedisConnection().getJedis().close();
} }
FileWriter writer = new FileWriter(file); FileWriter writer = new FileWriter(file);
@ -62,6 +63,7 @@ public class PunishmentManager extends PlexBase
{ {
plugin.getRedisConnection().getJedis().set(player.getUuid(), object.toString()); plugin.getRedisConnection().getJedis().set(player.getUuid(), object.toString());
PlexLog.debug("Added " + player.getUuid() + "'s punishment to the Redis database."); PlexLog.debug("Added " + player.getUuid() + "'s punishment to the Redis database.");
plugin.getRedisConnection().getJedis().close();
} }
FileWriter writer = new FileWriter(file); FileWriter writer = new FileWriter(file);

View File

@ -1,35 +1,64 @@
package dev.plex.storage; package dev.plex.storage;
import dev.plex.Plex;
import dev.plex.PlexBase; import dev.plex.PlexBase;
import dev.plex.util.PlexLog; import dev.plex.util.PlexLog;
import redis.clients.jedis.Jedis; import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
public class RedisConnection extends PlexBase public class RedisConnection extends PlexBase
{ {
private JedisPool pool;
private Jedis jedis; private Jedis jedis;
public JedisPool openPool() /*public JedisPool openPool()
{ {
JedisPoolConfig jedisConfig = new JedisPoolConfig();
//jedisConfig.setMaxIdle(10);
//jedisConfig.setMaxTotal(100);
ClassLoader previous = Thread.currentThread().getContextClassLoader(); ClassLoader previous = Thread.currentThread().getContextClassLoader();
Thread.currentThread().setContextClassLoader(RedisConnection.class.getClassLoader()); Thread.currentThread().setContextClassLoader(RedisConnection.class.getClassLoader());
this.pool = new JedisPool(new JedisPoolConfig(), plugin.config.getString("data.side.hostname"), Plex.get().getConfig().getInt("data.side.port")); this.pool = new JedisPool(jedisConfig, plugin.config.getString("data.side.hostname"),
plugin.config.getInt("data.side.port"));
Thread.currentThread().setContextClassLoader(previous); Thread.currentThread().setContextClassLoader(previous);
PlexLog.log("Connected to Redis!"); PlexLog.log("Connected to Redis!");
return pool; return pool;
} }
public Jedis getJedis() public Jedis getJedis()
{
try
{ {
this.jedis = pool.getResource(); this.jedis = pool.getResource();
}
catch (Exception ex)
{
PlexLog.error("An error occurred with Redis.");
ex.printStackTrace();
}
if (plugin.config.getBoolean("data.side.auth")) if (plugin.config.getBoolean("data.side.auth"))
{ {
jedis.auth(plugin.config.getString("data.side.password")); jedis.auth(plugin.config.getString("data.side.password"));
} }
return jedis; return jedis;
}*/
public Jedis getJedis()
{
try
{
jedis = new Jedis(plugin.config.getString("data.side.hostname"),
plugin.config.getInt("data.side.port"));
if (plugin.config.getBoolean("data.side.auth"))
{
jedis.auth(plugin.config.getString("data.side.password"));
}
PlexLog.log("Connected to Redis!");
return jedis;
}
catch (Exception ex)
{
PlexLog.error("An error occurred with Redis.");
ex.printStackTrace();
}
return jedis;
} }
public final boolean isEnabled() public final boolean isEnabled()