9
0
mirror of https://github.com/WiIIiam278/HuskSync.git synced 2025-12-26 01:59:20 +00:00

Use JedisPool instead of single Jedis connection

This commit is contained in:
William
2022-01-19 17:29:25 +00:00
parent 4ec4ba9a1e
commit 93be26a946
11 changed files with 83 additions and 42 deletions

View File

@@ -6,6 +6,7 @@ import me.william278.husksync.Settings;
import me.william278.husksync.proxy.data.DataManager;
import me.william278.husksync.proxy.data.sql.Database;
import me.william278.husksync.proxy.data.sql.MySQL;
import me.william278.husksync.redis.RedisListener;
import me.william278.husksync.redis.RedisMessage;
import me.william278.husksync.util.Logger;
@@ -95,7 +96,7 @@ public class MPDBMigrator {
}
// Carry out the migration
public void executeMigrationOperations(DataManager dataManager, HashSet<Server> synchronisedServers) {
public void executeMigrationOperations(DataManager dataManager, HashSet<Server> synchronisedServers, RedisListener redisListener) {
// Prepare the target database for insertion
prepareTargetDatabase(dataManager);
@@ -109,7 +110,7 @@ public class MPDBMigrator {
getExperienceData();
// Send the encoded data to the Bukkit servers for conversion
sendEncodedData(synchronisedServers);
sendEncodedData(synchronisedServers, redisListener);
}
// Clear the new database out of current data
@@ -200,7 +201,7 @@ public class MPDBMigrator {
}
}
private void sendEncodedData(HashSet<Server> synchronisedServers) {
private void sendEncodedData(HashSet<Server> synchronisedServers, RedisListener redisListener) {
for (Server processingServer : synchronisedServers) {
if (processingServer.hasMySqlPlayerDataBridge()) {
for (MPDBPlayerData data : mpdbPlayerData) {

View File

@@ -1,9 +1,7 @@
package me.william278.husksync.redis;
import me.william278.husksync.Settings;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisClientConfig;
import redis.clients.jedis.JedisPubSub;
import redis.clients.jedis.*;
import redis.clients.jedis.exceptions.JedisException;
import java.io.IOException;
@@ -16,6 +14,18 @@ public abstract class RedisListener {
*/
public boolean isActiveAndEnabled;
/**
* Pool of connections to the Redis server
*/
private static JedisPool jedisPool;
/**
* Creates a new RedisListener and initialises the Redis connection
*/
public RedisListener() {
jedisPool = new JedisPool(new JedisPoolConfig(), Settings.redisHost, Settings.redisPort);
}
/**
* Handle an incoming {@link RedisMessage}
*
@@ -31,20 +41,34 @@ public abstract class RedisListener {
*/
public abstract void log(Level level, String message);
/**
* Fetch a connection to the Redis server from the JedisPool
*
* @return Jedis instance from the pool
*/
public static Jedis getJedisConnection() {
return jedisPool.getResource();
}
/**
* Start the Redis listener
*/
public final void listen() {
try (Jedis jedis = new Jedis(Settings.redisHost, Settings.redisPort)) {
final String jedisPassword = Settings.redisPassword;
jedis.connect();
if (jedis.isConnected()) {
final String jedisPassword = Settings.redisPassword;
new Thread(() -> {
try (Jedis jedis = getJedisConnection()) {
if (!jedisPassword.equals("")) {
jedis.auth(jedisPassword);
}
isActiveAndEnabled = true;
log(Level.INFO, "Enabled Redis listener successfully!");
new Thread(() -> jedis.subscribe(new JedisPubSub() {
if (jedis.isConnected()) {
isActiveAndEnabled = true;
log(Level.INFO, "Enabled Redis listener successfully!");
} else {
isActiveAndEnabled = false;
log(Level.SEVERE, "Connection to the Redis server could not be established, please check the credentials.");
return;
}
jedis.subscribe(new JedisPubSub() {
@Override
public void onMessage(String channel, String message) {
// Only accept messages to the HuskSync channel
@@ -59,13 +83,11 @@ public abstract class RedisListener {
log(Level.SEVERE, "Failed to deserialize message target");
}
}
}, RedisMessage.REDIS_CHANNEL), "Redis Subscriber").start();
} else {
}, RedisMessage.REDIS_CHANNEL);
} catch (JedisException | IllegalStateException e) {
log(Level.SEVERE, "An exception occurred with the Jedis Subscriber!");
isActiveAndEnabled = false;
log(Level.SEVERE, "Failed to initialize the redis listener!");
}
} catch (JedisException e) {
log(Level.SEVERE, "Failed to establish a connection to the Redis server!");
}
}, "Redis Subscriber").start();
}
}

View File

@@ -22,8 +22,9 @@ public class RedisMessage {
/**
* Create a new RedisMessage
* @param type The type of the message
* @param target Who will receive this message
*
* @param type The type of the message
* @param target Who will receive this message
* @param messageData The message data elements
*/
public RedisMessage(MessageType type, MessageTarget target, String... messageData) {
@@ -38,6 +39,7 @@ public class RedisMessage {
/**
* Get a new RedisMessage from an incoming message string
*
* @param messageString The message string to parse
*/
public RedisMessage(String messageString) throws IOException, ClassNotFoundException {
@@ -49,6 +51,7 @@ public class RedisMessage {
/**
* Returns the full, formatted message string with type, target & data
*
* @return The fully formatted message
*/
private String getFullMessage() throws IOException {
@@ -61,21 +64,23 @@ public class RedisMessage {
* Send the redis message
*/
public void send() throws IOException {
try (Jedis publisher = new Jedis(Settings.redisHost, Settings.redisPort)) {
final String jedisPassword = Settings.redisPassword;
publisher.connect();
if (!jedisPassword.equals("")) {
publisher.auth(jedisPassword);
}
publisher.publish(REDIS_CHANNEL, getFullMessage());
try (Jedis publisher = RedisListener.getJedisConnection()) {
final String jedisPassword = Settings.redisPassword;
publisher.connect();
if (!jedisPassword.equals("")) {
publisher.auth(jedisPassword);
}
publisher.publish(REDIS_CHANNEL, getFullMessage());
}
}
public String getMessageData() {
return messageData;
}
public String[] getMessageDataElements() { return messageData.split(MESSAGE_DATA_SEPARATOR); }
public String[] getMessageDataElements() {
return messageData.split(MESSAGE_DATA_SEPARATOR);
}
public MessageType getMessageType() {
return messageType;
@@ -173,7 +178,9 @@ public class RedisMessage {
/**
* A record that defines the target of a plugin message; a spigot server or the proxy server(s). For Bukkit servers, the name of the server must also be specified
*/
public record MessageTarget(Settings.ServerType targetServerType, UUID targetPlayerUUID, String targetClusterId) implements Serializable { }
public record MessageTarget(Settings.ServerType targetServerType, UUID targetPlayerUUID,
String targetClusterId) implements Serializable {
}
/**
* Deserialize an object from a Base64 string