2
0
mirror of https://github.com/proxiodev/RedisBungee.git synced 2024-12-23 02:28:01 +00:00

Make summoner create customized JedisPooled that can't be closed

This commit is contained in:
mohammed jasem alaajel 2022-10-25 08:01:41 +04:00
parent 4d58ee1742
commit a382a298a1
No known key found for this signature in database
5 changed files with 29 additions and 9 deletions

View File

@ -63,7 +63,7 @@
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>4.3.0</version>
<version>4.3.1</version>
<scope>compile</scope>
</dependency>
<dependency>

View File

@ -12,6 +12,7 @@ import ninja.leaping.configurate.objectmapping.ObjectMappingException;
import ninja.leaping.configurate.yaml.YAMLConfigurationLoader;
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import redis.clients.jedis.*;
import redis.clients.jedis.providers.PooledConnectionProvider;
import java.io.File;
import java.io.IOException;
@ -113,13 +114,13 @@ public interface ConfigLoader {
JedisPoolConfig config = new JedisPoolConfig();
config.setMaxTotal(node.getNode("compatibility-max-connections").getInt(3));
config.setBlockWhenExhausted(true);
jedisPool = new JedisPool(config, redisServer, redisPort, 0, redisPassword, useSSL);
jedisPool = new JedisPool(config, redisServer, redisPort, 5000, redisPassword, useSSL);
plugin.logInfo("Compatibility JedisPool was created");
}
GenericObjectPoolConfig<Connection> poolConfig = new GenericObjectPoolConfig<>();
poolConfig.setMaxTotal(maxConnections);
poolConfig.setBlockWhenExhausted(true);
summoner = new JedisPooledSummoner(new JedisPooled(poolConfig, redisServer, redisPort, 0, redisPassword, useSSL), jedisPool);
summoner = new JedisPooledSummoner(new PooledConnectionProvider(new ConnectionFactory(new HostAndPort(redisServer, redisPort), DefaultJedisClientConfig.builder().timeoutMillis(5000).password(redisPassword).build()), poolConfig), jedisPool);
redisBungeeMode = RedisBungeeMode.SINGLE;
}
plugin.logInfo("Successfully connected to Redis.");

View File

@ -3,16 +3,17 @@ package com.imaginarycode.minecraft.redisbungee.api.summoners;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPooled;
import redis.clients.jedis.providers.PooledConnectionProvider;
import java.io.IOException;
public class JedisPooledSummoner implements Summoner<JedisPooled> {
private final JedisPooled jedisPooled;
private final PooledConnectionProvider connectionProvider;
private final JedisPool jedisPool;
public JedisPooledSummoner(JedisPooled jedisPooled, JedisPool jedisPool) {
this.jedisPooled = jedisPooled;
public JedisPooledSummoner(PooledConnectionProvider connectionProvider, JedisPool jedisPool) {
this.connectionProvider = connectionProvider;
this.jedisPool = jedisPool;
// test connections
if (jedisPool != null) {
@ -20,7 +21,9 @@ public class JedisPooledSummoner implements Summoner<JedisPooled> {
// Test the connection to make sure configuration is right
jedis.ping();
}
}
final JedisPooled jedisPooled = this.obtainResource();
jedisPooled.set("random_data", "0");
jedisPooled.del("random_data");
@ -28,7 +31,8 @@ public class JedisPooledSummoner implements Summoner<JedisPooled> {
@Override
public JedisPooled obtainResource() {
return this.jedisPooled;
// create UnClosable JedisPool *disposable*
return new NotClosableJedisPooled(this.connectionProvider);
}
public JedisPool getCompatibilityJedisPool() {
@ -40,7 +44,7 @@ public class JedisPooledSummoner implements Summoner<JedisPooled> {
if (this.jedisPool != null) {
this.jedisPool.close();
}
this.jedisPooled.close();
this.connectionProvider.close();
}
}

View File

@ -0,0 +1,16 @@
package com.imaginarycode.minecraft.redisbungee.api.summoners;
import redis.clients.jedis.JedisPooled;
import redis.clients.jedis.providers.PooledConnectionProvider;
public class NotClosableJedisPooled extends JedisPooled {
NotClosableJedisPooled(PooledConnectionProvider provider) {
super(provider);
}
@Override
public void close() {
}
}

View File

@ -14,5 +14,4 @@ public interface Summoner<P> extends Closeable {
P obtainResource();
}