Async Command handling.

keep-around/d31701866686f66088b78de2e29736ae36e55a68
Pingex aka Raphaël 9 years ago
parent dea35f59a6
commit 73f350aacc

@ -6,12 +6,15 @@ import net.pingex.dcf.commands.parser.ParserException;
import net.pingex.dcf.core.Configuration;
import net.pingex.dcf.modularity.PluginWrapper;
import net.pingex.dcf.util.DiscordInteractionsUtil;
import org.apache.commons.lang3.concurrent.BasicThreadFactory;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import sx.blah.discord.handle.impl.events.MessageReceivedEvent;
import java.lang.reflect.Method;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
/**
* Landing class for the whole command package
@ -28,6 +31,12 @@ public class CommandHandler
*/
private static final Logger LOGGER = LogManager.getLogger(CommandHandler.class);
/**
* Executor Service for dispatching commands to threads
* TODO: ExecutorService.shutdown() when using a stop command or whatever
*/
private static final ExecutorService threadPool = Executors.newCachedThreadPool(new BasicThreadFactory.Builder().namingPattern("CommandHandler-%d").build());
/**
* Landing method for a MRE
*/
@ -58,14 +67,17 @@ public class CommandHandler
}
// Run it
try
threadPool.submit(() ->
{
targetCommand.get().execute(event, arguments);
}
catch(Throwable throwable)
{
LOGGER.error("Error while executing command " + command + ".", throwable);
}
try
{
targetCommand.get().execute(event, arguments);
}
catch(Throwable throwable)
{
LOGGER.error("Error while executing command " + command + ".", throwable);
}
});
}
/**