diff --git a/pom.xml b/pom.xml index 0acd0c0..efc69c6 100644 --- a/pom.xml +++ b/pom.xml @@ -121,7 +121,7 @@ net.lz1998 spring-cq - 4.14.0.6 + 4.15.0.1 ch.qos.logback diff --git a/src/main/java/net/lamgc/cgj/Main.java b/src/main/java/net/lamgc/cgj/Main.java index 8aee025..33d727d 100644 --- a/src/main/java/net/lamgc/cgj/Main.java +++ b/src/main/java/net/lamgc/cgj/Main.java @@ -8,7 +8,6 @@ import com.google.gson.JsonObject; import net.lamgc.cgj.bot.boot.ApplicationBoot; import net.lamgc.cgj.bot.boot.BotGlobal; import net.lamgc.cgj.bot.framework.cli.ConsoleMain; -import net.lamgc.cgj.bot.framework.coolq.CQConfig; import net.lamgc.cgj.bot.framework.mirai.MiraiMain; import net.lamgc.cgj.pixiv.PixivDownload; import net.lamgc.cgj.pixiv.PixivSearchLinkBuilder; @@ -106,7 +105,6 @@ public class Main { @Command public static void pluginMode(@Argument(name = "args", force = false) String argsStr) { log.info("酷Q机器人根目录: {}", BotGlobal.getGlobal().getDataStoreDir().getPath()); - CQConfig.init(); Pattern pattern = Pattern.compile("/\\s*(\".+?\"|[^:\\s])+((\\s*:\\s*(\".+?\"|[^\\s])+)|)|(\".+?\"|[^\"\\s])+"); Matcher matcher = pattern.matcher(Strings.nullToEmpty(argsStr)); ArrayList argsList = new ArrayList<>(); diff --git a/src/main/java/net/lamgc/cgj/bot/framework/coolq/CQConfig.java b/src/main/java/net/lamgc/cgj/bot/framework/coolq/CQConfig.java deleted file mode 100644 index f71fec3..0000000 --- a/src/main/java/net/lamgc/cgj/bot/framework/coolq/CQConfig.java +++ /dev/null @@ -1,27 +0,0 @@ -package net.lamgc.cgj.bot.framework.coolq; - -import com.google.common.util.concurrent.ThreadFactoryBuilder; -import net.lz1998.cq.CQGlobal; -import net.lz1998.cq.EnableCQ; - -import java.util.concurrent.LinkedBlockingQueue; -import java.util.concurrent.ThreadPoolExecutor; -import java.util.concurrent.TimeUnit; - -@EnableCQ -public class CQConfig { - - public static void init() { - CQGlobal.pluginList.add(CQPluginMain.class); - CQGlobal.executor = new ThreadPoolExecutor( - (int) Math.ceil(Runtime.getRuntime().availableProcessors() / 2F), - Runtime.getRuntime().availableProcessors(), - 25, TimeUnit.SECONDS, - new LinkedBlockingQueue<>(512), - new ThreadFactoryBuilder() - .setNameFormat("Plugin-ProcessThread-%d") - .build() - ); - } - -} diff --git a/src/main/java/net/lamgc/cgj/bot/framework/coolq/CQPluginMain.java b/src/main/java/net/lamgc/cgj/bot/framework/coolq/CQPluginMain.java index 2073387..546f3e2 100644 --- a/src/main/java/net/lamgc/cgj/bot/framework/coolq/CQPluginMain.java +++ b/src/main/java/net/lamgc/cgj/bot/framework/coolq/CQPluginMain.java @@ -3,6 +3,7 @@ package net.lamgc.cgj.bot.framework.coolq; import net.lamgc.cgj.bot.boot.ApplicationBoot; import net.lamgc.cgj.bot.event.BotEventHandler; import net.lamgc.cgj.bot.framework.coolq.message.SpringCQMessageEvent; +import net.lamgc.cgj.bot.framework.coolq.message.SpringCQMessageSenderFactory; import net.lamgc.utils.event.EventHandler; import net.lz1998.cq.event.message.CQDiscussMessageEvent; import net.lz1998.cq.event.message.CQGroupMessageEvent; @@ -46,6 +47,7 @@ public class CQPluginMain extends CQPlugin implements EventHandler { * @return 是否拦截消息 */ private static int processMessage(CoolQ cq, CQMessageEvent event) { + SpringCQMessageSenderFactory.setCoolQ(cq); if(BotEventHandler.mismatch(event.getMessage())) { return MESSAGE_IGNORE; } diff --git a/src/main/java/net/lamgc/cgj/bot/framework/coolq/message/SpringCQMessageSenderFactory.java b/src/main/java/net/lamgc/cgj/bot/framework/coolq/message/SpringCQMessageSenderFactory.java index ac8ee1e..ab4b147 100644 --- a/src/main/java/net/lamgc/cgj/bot/framework/coolq/message/SpringCQMessageSenderFactory.java +++ b/src/main/java/net/lamgc/cgj/bot/framework/coolq/message/SpringCQMessageSenderFactory.java @@ -6,13 +6,26 @@ import net.lamgc.cgj.bot.message.MessageSource; import net.lz1998.cq.robot.CoolQ; import java.util.Objects; +import java.util.concurrent.atomic.AtomicReference; public class SpringCQMessageSenderFactory implements MessageSenderFactory { - private final static ThreadLocal threadCoolQ = new ThreadLocal<>(); + private final static AtomicReference coolQ = new AtomicReference<>(); + + /** + * 设置CoolQ对象. + *

该方法仅接受第一次设置的CoolQ对象, 其他对象将会忽略.

+ * @param coolQObj CoolQ对象 + */ + public static void setCoolQ(CoolQ coolQObj) { + if(coolQ.get() == null) { + coolQ.set(coolQObj); + } + } + @Override public MessageSender createMessageSender(MessageSource source, long id) { return new SpringCQMessageSender( - Objects.requireNonNull(threadCoolQ.get(), "CoolQ object is not included in ThreadLocal"), source, id); + Objects.requireNonNull(coolQ.get(), "CoolQ object not ready"), source, id); } } diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties deleted file mode 100644 index aa21d2c..0000000 --- a/src/main/resources/application.properties +++ /dev/null @@ -1,2 +0,0 @@ -server.port=8081 -server.tomcat.max-threads=1 \ No newline at end of file diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml new file mode 100644 index 0000000..b313f1c --- /dev/null +++ b/src/main/resources/application.yml @@ -0,0 +1,12 @@ +server: + port: 8081 + +spring: + cq: + plugin-list: + - net.lamgc.cgj.bot.framework.coolq.CQPluginMain + event: + corePoolSize: 8 + maxPoolSize: 16 + keepAliveTime: 25000 + workQueueSize: 1024 \ No newline at end of file