mirror of
https://github.com/darksaid98/HuskHomes.git
synced 2026-12-04 06:28:49 +00:00
Update to Jedis 4.0.1; Use Jedis pooling; Close #122
This commit is contained in:
1 parent
be6ec03e52
commit
8fb3ab8c52
4 files changed
+82
-65
No files matched your search
@@ -186,7 +186,7 @@
|
|||||||
<dependency>
|
<dependency>
|
||||||
<groupId>redis.clients</groupId>
|
<groupId>redis.clients</groupId>
|
||||||
<artifactId>jedis</artifactId>
|
<artifactId>jedis</artifactId>
|
||||||
<version>3.7.1</version>
|
<version>4.0.1</version>
|
||||||
<type>jar</type>
|
<type>jar</type>
|
||||||
<scope>provided</scope>
|
<scope>provided</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|||||||
@@ -127,6 +127,7 @@ public final class HuskHomes extends JavaPlugin {
|
|||||||
if (getSettings().getMessengerType().equalsIgnoreCase("pluginmessage")) {
|
if (getSettings().getMessengerType().equalsIgnoreCase("pluginmessage")) {
|
||||||
Bukkit.getMessenger().registerIncomingPluginChannel(this, "BungeeCord", new PluginMessageReceiver());
|
Bukkit.getMessenger().registerIncomingPluginChannel(this, "BungeeCord", new PluginMessageReceiver());
|
||||||
} else {
|
} else {
|
||||||
|
RedisReceiver.initialize();
|
||||||
RedisReceiver.listen();
|
RedisReceiver.listen();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -25,7 +25,7 @@ public class RedisMessage extends Message {
|
|||||||
|
|
||||||
private void dispatchRedisMessage(String target) {
|
private void dispatchRedisMessage(String target) {
|
||||||
Bukkit.getScheduler().runTask(plugin, () -> {
|
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();
|
final String jedisPassword = HuskHomes.getSettings().getRedisPassword();
|
||||||
if (!jedisPassword.equals("")) {
|
if (!jedisPassword.equals("")) {
|
||||||
publisher.auth(jedisPassword);
|
publisher.auth(jedisPassword);
|
||||||
@@ -48,6 +48,6 @@ public class RedisMessage extends Message {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void sendToServer(Player sender, String server) {
|
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.Bukkit;
|
||||||
import org.bukkit.entity.Player;
|
import org.bukkit.entity.Player;
|
||||||
import redis.clients.jedis.Jedis;
|
import redis.clients.jedis.Jedis;
|
||||||
|
import redis.clients.jedis.JedisPool;
|
||||||
|
import redis.clients.jedis.JedisPoolConfig;
|
||||||
import redis.clients.jedis.JedisPubSub;
|
import redis.clients.jedis.JedisPubSub;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
@@ -12,6 +14,19 @@ import java.util.Collections;
|
|||||||
|
|
||||||
public class RedisReceiver {
|
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();
|
private static final HuskHomes plugin = HuskHomes.getInstance();
|
||||||
public static final String REDIS_CHANNEL = "HuskHomes";
|
public static final String REDIS_CHANNEL = "HuskHomes";
|
||||||
|
|
||||||
@@ -25,72 +40,73 @@ public class RedisReceiver {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static void listen() {
|
public static void listen() {
|
||||||
Jedis jedis = new Jedis(HuskHomes.getSettings().getRedisHost(), HuskHomes.getSettings().getRedisPort());
|
|
||||||
final String jedisPassword = HuskHomes.getSettings().getRedisPassword();
|
final String jedisPassword = HuskHomes.getSettings().getRedisPassword();
|
||||||
if (!jedisPassword.equals("")) {
|
try (Jedis jedis = getJedis()) {
|
||||||
jedis.auth(jedisPassword);
|
if (!jedisPassword.equals("")) {
|
||||||
}
|
jedis.auth(jedisPassword);
|
||||||
jedis.connect();
|
}
|
||||||
if (jedis.isConnected()) {
|
jedis.connect();
|
||||||
plugin.getLogger().info("Enabled Redis listener successfully!");
|
if (jedis.isConnected()) {
|
||||||
new Thread(() -> jedis.subscribe(new JedisPubSub() {
|
plugin.getLogger().info("Enabled Redis listener successfully!");
|
||||||
@Override
|
new Thread(() -> jedis.subscribe(new JedisPubSub() {
|
||||||
public void onMessage(String channel, String message) {
|
@Override
|
||||||
if (!channel.equals(REDIS_CHANNEL)) {
|
public void onMessage(String channel, String message) {
|
||||||
return;
|
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
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
// Return if the target is not available
|
/* HuskHomes Redis messages have a header formatted as such:
|
||||||
if (receiver == null) {
|
* <cluster_id>:<message_type>:<target_player>£ followed by the message arguments and data. */
|
||||||
return;
|
final String[] splitMessage = message.split(RedisMessage.REDIS_MESSAGE_HEADER_SEPARATOR);
|
||||||
}
|
final String messageHeader = splitMessage[0];
|
||||||
if (!receiver.isOnline()) {
|
int clusterID;
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
final String messageData = splitMessage[1];
|
// Ensure the cluster ID matches
|
||||||
CrossServerMessageHandler.handlePluginMessage(new RedisMessage(clusterID, receiver.getName(), messageType, messageData), receiver);
|
try {
|
||||||
}
|
clusterID = Integer.parseInt(messageHeader.split(":")[0]);
|
||||||
}, REDIS_CHANNEL), "Redis Subscriber").start();
|
} catch (Exception e) {
|
||||||
} else {
|
// In case the message is malformed or the cluster ID is invalid
|
||||||
plugin.getLogger().severe("Failed to initialize the redis listener!");
|
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!");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user