Fix wrong class name & Fully fix sending messages while disabled

This commit is contained in:
Govindas 2020-11-03 11:29:06 +02:00
parent f35ad377d1
commit f53f9dd5a2
5 changed files with 38 additions and 13 deletions

View File

@ -6,7 +6,7 @@
<groupId>net.limework.rediskript</groupId> <groupId>net.limework.rediskript</groupId>
<artifactId>RediSkript</artifactId> <artifactId>RediSkript</artifactId>
<version>1.2.2</version> <version>1.2.5</version>
<packaging>jar</packaging> <packaging>jar</packaging>
<build> <build>

View File

@ -2,11 +2,14 @@ package net.limework.rediskript;
import net.limework.rediskript.commands.CommandReloadRedis; import net.limework.rediskript.commands.CommandReloadRedis;
import net.limework.rediskript.skript.SkriptHook; import net.limework.rediskript.skript.SkriptHook;
import net.limework.rediskript.managers.RedisManager; import net.limework.rediskript.managers.RedisManager;
import org.bukkit.Bukkit;
import org.bukkit.command.PluginCommand; import org.bukkit.command.PluginCommand;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority; import org.bukkit.event.EventPriority;
import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.plugin.java.JavaPlugin;
import java.util.concurrent.TimeUnit;
public class RediSkript extends JavaPlugin { public class RediSkript extends JavaPlugin {
//Redis manager //Redis manager
@ -31,14 +34,12 @@ public class RediSkript extends JavaPlugin {
@Override @Override
//using HIGHEST event priority so it shuts down last and code can still execute well in "on script unload" and "on skript unload" events //using HIGHEST event priority so it shuts down last and code can still execute well in "on script unload" and "on skript unload" events
@EventHandler(priority = EventPriority.HIGHEST) @EventHandler(priority = EventPriority.LOWEST)
public void onDisable() { public void onDisable() {
if (rm != null) { if (rm != null) {
rm.shutdown(); rm.shutdown();
} }
} }
public RedisManager getRm() { public RedisManager getRm() {
return rm; return rm;
} }

View File

@ -18,10 +18,12 @@ import java.nio.charset.StandardCharsets;
import java.util.List; import java.util.List;
import java.util.concurrent.ExecutorService; import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicBoolean;
public class RedisManager extends BinaryJedisPubSub implements Runnable { public class RedisManager extends BinaryJedisPubSub implements Runnable {
private final ExecutorService RedisReconnector;
private RediSkript plugin; private RediSkript plugin;
private JedisPool jedisPool; private JedisPool jedisPool;
@ -52,7 +54,8 @@ public class RedisManager extends BinaryJedisPubSub implements Runnable {
config.getInt("Redis.TimeOut"), config.getInt("Redis.TimeOut"),
config.getString("Redis.Password"), config.getString("Redis.Password"),
config.getBoolean("Redis.useTLS")); config.getBoolean("Redis.useTLS"));
RedisService = Executors.newFixedThreadPool(3); RedisReconnector = Executors.newSingleThreadExecutor();
RedisService = Executors.newSingleThreadExecutor();
try { try {
this.subscribeJedis = this.jedisPool.getResource(); this.subscribeJedis = this.jedisPool.getResource();
} catch (Exception ignored) { } catch (Exception ignored) {
@ -63,7 +66,7 @@ public class RedisManager extends BinaryJedisPubSub implements Runnable {
} }
public void start() { public void start() {
this.RedisService.execute(this); this.RedisReconnector.execute(this);
} }
@Override @Override
@ -130,7 +133,12 @@ public class RedisManager extends BinaryJedisPubSub implements Runnable {
JSONObject j = new JSONObject(receivedMessage); JSONObject j = new JSONObject(receivedMessage);
//System.out.println("Message got from channel: "+channel +" and the Message: " +json.toString()); //System.out.println("Message got from channel: "+channel +" and the Message: " +json.toString());
RedisMessageEvent event = new RedisMessageEvent(channelString, j.getString("Message"), j.getLong("Date")); RedisMessageEvent event = new RedisMessageEvent(channelString, j.getString("Message"), j.getLong("Date"));
Bukkit.getScheduler().runTask(plugin, () -> plugin.getServer().getPluginManager().callEvent(event));
//if plugin is disabling, don't call events anymore
if (plugin.isEnabled()) {
Bukkit.getScheduler().runTask(plugin, () -> plugin.getServer().getPluginManager().callEvent(event));
}
} }
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
@ -141,6 +149,11 @@ public class RedisManager extends BinaryJedisPubSub implements Runnable {
} }
public void shutdown() { public void shutdown() {
try {
this.RedisService.awaitTermination(1, TimeUnit.SECONDS);
} catch (InterruptedException e) {
e.printStackTrace();
}
this.isShuttingDown.set(true); this.isShuttingDown.set(true);
if (this.subscribeJedis != null) { if (this.subscribeJedis != null) {
this.unsubscribe(); this.unsubscribe();
@ -148,6 +161,7 @@ public class RedisManager extends BinaryJedisPubSub implements Runnable {
this.subscribeJedis.getClient().close(); this.subscribeJedis.getClient().close();
this.jedisPool.getResource().close(); this.jedisPool.getResource().close();
} }
this.RedisReconnector.shutdown();
this.RedisService.shutdown(); this.RedisService.shutdown();
} }

View File

@ -16,12 +16,10 @@ import net.limework.rediskript.skript.elements.ExprMessageDate;
import java.io.IOException; import java.io.IOException;
public class SkriptHook { public class SkriptHook {
private SkriptAddon addon;
public SkriptHook(RediSkript plugin) { public SkriptHook(RediSkript plugin) {
addon = Skript.registerAddon(plugin); SkriptAddon addon = Skript.registerAddon(plugin);
try { try {
addon.loadClasses("net.limework.core.skript", "elements"); addon.loadClasses("net.limework.rediskript.skript", "elements");
Skript.registerEvent("redis message", EvtRedis.class, RedisMessageEvent.class, "redis message"); Skript.registerEvent("redis message", EvtRedis.class, RedisMessageEvent.class, "redis message");
Skript.registerExpression(ExprChannel.class, String.class, ExpressionType.SIMPLE, "redis channel"); Skript.registerExpression(ExprChannel.class, String.class, ExpressionType.SIMPLE, "redis channel");
EventValues.registerEventValue(RedisMessageEvent.class, String.class, new Getter<String, RedisMessageEvent>() { EventValues.registerEventValue(RedisMessageEvent.class, String.class, new Getter<String, RedisMessageEvent>() {

View File

@ -55,11 +55,23 @@ public class EffSendMessage extends Effect {
msg = json.toString().getBytes(StandardCharsets.UTF_8); msg = json.toString().getBytes(StandardCharsets.UTF_8);
} }
try { try {
manager.getRedisService().execute(() -> {
//execute sending of redis message on the main thread if plugin is disabling
//so it can still process the sending
//sending a redis message blocks main thread if there's no more connections available
//so to avoid issues, it's best to do it always on separate thread
if (plugin.isEnabled()) {
manager.getRedisService().execute(() -> {
BinaryJedis j = manager.getJedisPool().getResource();
j.publish(channel.getBytes(StandardCharsets.UTF_8), msg);
j.close();
});
} else {
BinaryJedis j = manager.getJedisPool().getResource(); BinaryJedis j = manager.getJedisPool().getResource();
j.publish(channel.getBytes(StandardCharsets.UTF_8), msg); j.publish(channel.getBytes(StandardCharsets.UTF_8), msg);
j.close(); j.close();
}); }
} catch (JedisConnectionException exception) { } catch (JedisConnectionException exception) {
exception.printStackTrace(); exception.printStackTrace();
} }