Update to Jedis 4.0.1; Use Jedis pooling; Close #122

This commit is contained in:
William committed 2022-01-15 19:01:27 +00:00
1 parent be6ec03e52
commit 8fb3ab8c52
4 files changed
+82 -65

No files matched your search

+1 -1
View File
@@ -186,7 +186,7 @@
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>3.7.1</version>
<version>4.0.1</version>
<type>jar</type>
<scope>provided</scope>
</dependency>
@@ -127,6 +127,7 @@ public final class HuskHomes extends JavaPlugin {
if (getSettings().getMessengerType().equalsIgnoreCase("pluginmessage")) {
Bukkit.getMessenger().registerIncomingPluginChannel(this, "BungeeCord", new PluginMessageReceiver());
} else {
RedisReceiver.initialize();
RedisReceiver.listen();
}
}
@@ -25,7 +25,7 @@ public class RedisMessage extends Message {
private void dispatchRedisMessage(String target) {
Bukkit.getScheduler().runTask(plugin, () -> {
try (Jedis publisher = new Jedis(HuskHomes.getSettings().getRedisHost(), HuskHomes.getSettings().getRedisPort())) {
try (Jedis publisher = RedisReceiver.getJedis()) {
final String jedisPassword = HuskHomes.getSettings().getRedisPassword();
if (!jedisPassword.equals("")) {
publisher.auth(jedisPassword);
@@ -48,6 +48,6 @@ public class RedisMessage extends Message {
@Override
public void sendToServer(Player sender, String server) {
dispatchRedisMessage("server-" + server + "");
dispatchRedisMessage("server-" + server + "");
}
}
@@ -5,6 +5,8 @@ import me.william278.huskhomes2.data.message.CrossServerMessageHandler;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
import redis.clients.jedis.JedisPubSub;
import java.util.ArrayList;
@@ -12,6 +14,19 @@ import java.util.Collections;
public class RedisReceiver {
private static JedisPool jedisPool;
public static Jedis getJedis() {
return jedisPool.getResource();
}
public static void initialize() {
jedisPool = new JedisPool(new JedisPoolConfig(),
HuskHomes.getSettings().getRedisHost(),
HuskHomes.getSettings().getRedisPort());
}
private static final HuskHomes plugin = HuskHomes.getInstance();
public static final String REDIS_CHANNEL = "HuskHomes";
@@ -25,72 +40,73 @@ public class RedisReceiver {
}
public static void listen() {
Jedis jedis = new Jedis(HuskHomes.getSettings().getRedisHost(), HuskHomes.getSettings().getRedisPort());
final String jedisPassword = HuskHomes.getSettings().getRedisPassword();
if (!jedisPassword.equals("")) {
jedis.auth(jedisPassword);
}
jedis.connect();
if (jedis.isConnected()) {
plugin.getLogger().info("Enabled Redis listener successfully!");
new Thread(() -> jedis.subscribe(new JedisPubSub() {
@Override
public void onMessage(String channel, String message) {
if (!channel.equals(REDIS_CHANNEL)) {
return;
}
/* HuskHomes Redis messages have a header formatted as such:
* <cluster_id>:<message_type>:<target_player>£ followed by the message arguments and data. */
final String[] splitMessage = message.split(RedisMessage.REDIS_MESSAGE_HEADER_SEPARATOR);
final String messageHeader = splitMessage[0];
int clusterID;
// Ensure the cluster ID matches
try {
clusterID = Integer.parseInt(messageHeader.split(":")[0]);
} catch (Exception e) {
// In case the message is malformed or the cluster ID is invalid
HuskHomes.getInstance().getLogger().warning("Received a Redis message on the HuskHomes channel with an invalid server Cluster ID! \n" +
"Please ensure that the cluster ID is set to a valid integer on all servers.");
return;
}
if (HuskHomes.getSettings().getClusterId() != clusterID) {
return;
}
// Get the type of redis message
final String messageType = messageHeader.split(":")[1];
// Get the player targeted by the message and make sure they are online
final String target = messageHeader.split(":")[2];
Player receiver = Bukkit.getPlayerExact(target);
// If the redis message was targeting this server
if (target.equalsIgnoreCase("-all-")) {
receiver = getRandomReceiver();
} else if (target.contains("server-")) { //todo In the future, allow redis messages to execute without a player online. For now it's duplicating the behaviour of plugin messages for consistency.
if (target.split("-")[1].equalsIgnoreCase(HuskHomes.getSettings().getServerID())) {
receiver = getRandomReceiver();
} else {
return; // The message was targeting another server; ignore
try (Jedis jedis = getJedis()) {
if (!jedisPassword.equals("")) {
jedis.auth(jedisPassword);
}
jedis.connect();
if (jedis.isConnected()) {
plugin.getLogger().info("Enabled Redis listener successfully!");
new Thread(() -> jedis.subscribe(new JedisPubSub() {
@Override
public void onMessage(String channel, String message) {
if (!channel.equals(REDIS_CHANNEL)) {
return;
}
}
// Return if the target is not available
if (receiver == null) {
return;
}
if (!receiver.isOnline()) {
return;
}
/* HuskHomes Redis messages have a header formatted as such:
* <cluster_id>:<message_type>:<target_player>£ followed by the message arguments and data. */
final String[] splitMessage = message.split(RedisMessage.REDIS_MESSAGE_HEADER_SEPARATOR);
final String messageHeader = splitMessage[0];
int clusterID;
final String messageData = splitMessage[1];
CrossServerMessageHandler.handlePluginMessage(new RedisMessage(clusterID, receiver.getName(), messageType, messageData), receiver);
}
}, REDIS_CHANNEL), "Redis Subscriber").start();
} else {
plugin.getLogger().severe("Failed to initialize the redis listener!");
// Ensure the cluster ID matches
try {
clusterID = Integer.parseInt(messageHeader.split(":")[0]);
} catch (Exception e) {
// In case the message is malformed or the cluster ID is invalid
HuskHomes.getInstance().getLogger().warning("Received a Redis message on the HuskHomes channel with an invalid server Cluster ID! \n" +
"Please ensure that the cluster ID is set to a valid integer on all servers.");
return;
}
if (HuskHomes.getSettings().getClusterId() != clusterID) {
return;
}
// Get the type of redis message
final String messageType = messageHeader.split(":")[1];
// Get the player targeted by the message and make sure they are online
final String target = messageHeader.split(":")[2];
Player receiver = Bukkit.getPlayerExact(target);
// If the redis message was targeting this server
if (target.equalsIgnoreCase("-all-")) {
receiver = getRandomReceiver();
} else if (target.contains("server-")) { //todo In the future, allow redis messages to execute without a player online. For now it's duplicating the behaviour of plugin messages for consistency.
if (target.split("-")[1].equalsIgnoreCase(HuskHomes.getSettings().getServerID())) {
receiver = getRandomReceiver();
} else {
return; // The message was targeting another server; ignore
}
}
// Return if the target is not available
if (receiver == null) {
return;
}
if (!receiver.isOnline()) {
return;
}
final String messageData = splitMessage[1];
CrossServerMessageHandler.handlePluginMessage(new RedisMessage(clusterID, receiver.getName(), messageType, messageData), receiver);
}
}, REDIS_CHANNEL), "Redis Subscriber").start();
} else {
plugin.getLogger().severe("Failed to initialize the redis listener!");
}
}
}
}