From 8fce07792fcff84591196453207a80813698dbf5 Mon Sep 17 00:00:00 2001 From: Pingex Date: Sun, 24 Apr 2016 17:17:10 +0200 Subject: [PATCH] Proper command invokation threading. Updated dependencies --- build.gradle | 5 +-- .../java/net/pingex/discordbot/Async.java | 17 -------- .../pingex/discordbot/CommandDispatcher.java | 42 ++++++++----------- 3 files changed, 20 insertions(+), 44 deletions(-) delete mode 100644 src/main/java/net/pingex/discordbot/Async.java diff --git a/build.gradle b/build.gradle index 6e00ff2..696ca2e 100644 --- a/build.gradle +++ b/build.gradle @@ -5,7 +5,6 @@ version '0.1-dev' repositories { mavenCentral() - jcenter() maven { url "https://jitpack.io" } @@ -13,8 +12,8 @@ repositories { dependencies { testCompile group: 'junit', name: 'junit', version: '4.11' - compile "com.github.austinv11:Discord4j:2.4.4" - compile "org.mod4j.org.apache.commons:cli:1.0.0" + compile "com.github.austinv11:Discord4j:2.4.5" compile "org.ini4j:ini4j:0.5.4" compile "org.slf4j:slf4j-simple:1.7.9" + compile "org.apache.commons:commons-lang3:3.0" } diff --git a/src/main/java/net/pingex/discordbot/Async.java b/src/main/java/net/pingex/discordbot/Async.java deleted file mode 100644 index 149a942..0000000 --- a/src/main/java/net/pingex/discordbot/Async.java +++ /dev/null @@ -1,17 +0,0 @@ -package net.pingex.discordbot; - -import java.lang.annotation.ElementType; -import java.lang.annotation.Retention; -import java.lang.annotation.RetentionPolicy; -import java.lang.annotation.Target; - -/** - * Marks a function as async, for long-running commands. - * @version 0.1-dev - * @author Raphael "Pingex" NAAS - */ -@Retention(RetentionPolicy.RUNTIME) -@Target(ElementType.METHOD) -public @interface Async -{ -} diff --git a/src/main/java/net/pingex/discordbot/CommandDispatcher.java b/src/main/java/net/pingex/discordbot/CommandDispatcher.java index 291474a..3676410 100644 --- a/src/main/java/net/pingex/discordbot/CommandDispatcher.java +++ b/src/main/java/net/pingex/discordbot/CommandDispatcher.java @@ -1,5 +1,6 @@ package net.pingex.discordbot; +import org.apache.commons.lang3.concurrent.BasicThreadFactory; import sx.blah.discord.api.EventSubscriber; import sx.blah.discord.handle.impl.events.MessageReceivedEvent; import sx.blah.discord.util.DiscordException; @@ -9,6 +10,7 @@ import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.util.ArrayList; import java.util.HashMap; +import java.util.concurrent.*; import java.util.logging.Logger; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -37,6 +39,11 @@ public class CommandDispatcher */ private String commandPrefix; + /** + * Thread pool used to invoke commands + */ + private ExecutorService threadPool; + /** * Basic Constructor, automatically rebuilds command list from the modules registry */ @@ -44,6 +51,7 @@ public class CommandDispatcher { logger = Logger.getLogger(this.getClass().getName()); rebuildCommandList(); + threadPool = Executors.newCachedThreadPool(new BasicThreadFactory.Builder().namingPattern("CommandDispatcher-%d").build()); // TODO: threadPool.shutdown() if(Configuration.exists("general", "commandPrefix") && !Configuration.getValue("general", "commandPrefix").isEmpty()) commandPrefix = Configuration.getValue("general", "commandPrefix"); @@ -108,39 +116,25 @@ public class CommandDispatcher // Run command if(commandAnswer == null) { - if (commandList.get(fullCommand).getMethod().isAnnotationPresent(Async.class)) // Run async - { - String finalFullCommand = fullCommand; - Object[] finalParsedArray = parsedArray; - new Thread(() -> // TODO: Very hacky - { - try - { - String ans = (String) commandList.get(finalFullCommand).invoke(finalParsedArray); - if(ans != null) event.getMessage().reply(ans); - } catch (IllegalArgumentException | IllegalAccessException | InvocationTargetException e) - { - e.printStackTrace(); - } catch (MissingPermissionsException | HTTP429Exception | DiscordException e) - { - logger.warning("Couldn't reply to command (" + e.getClass().getName() + "): " + e.getMessage()); - } - }).start(); - } - else // Run sync - { + String finalFullCommand = fullCommand; + Object[] finalParsedArray = parsedArray; + threadPool.submit(() -> { try { - String ans = (String) commandList.get(fullCommand).invoke(parsedArray); + String ans = (String) commandList.get(finalFullCommand).invoke(finalParsedArray); if(ans != null) event.getMessage().reply(ans); } catch (IllegalArgumentException | IllegalAccessException | InvocationTargetException e) { - e.printStackTrace(); + logger.severe("Couldn't call target method (" + e.getClass().getName() + "): " + e.getMessage()); } catch (MissingPermissionsException | HTTP429Exception | DiscordException e) { logger.warning("Couldn't reply to command (" + e.getClass().getName() + "): " + e.getMessage()); + } catch (Exception e) + { + logger.severe("Error in threaded command"); + e.printStackTrace(); } - } + }); } else // Error answer try