From d3d6f151d45096db0345ef6f2ab6f6fefa8a0bc4 Mon Sep 17 00:00:00 2001 From: LamGC Date: Thu, 2 Jul 2020 14:02:01 +0800 Subject: [PATCH] =?UTF-8?q?[Delete]=20application.properties=20=E7=A7=BB?= =?UTF-8?q?=E9=99=A4Properties=E7=89=88Application=E9=85=8D=E7=BD=AE?= =?UTF-8?q?=E6=96=87=E4=BB=B6;=20[Delete]=20CQConfig=20=E7=A7=BB=E9=99=A4?= =?UTF-8?q?=E4=B8=8D=E5=85=BC=E5=AE=B9=E7=9A=84=E7=B1=BB;=20[Add]=20applic?= =?UTF-8?q?ation.yml=20=E6=B7=BB=E5=8A=A0yml=E6=A0=BC=E5=BC=8F=E7=9A=84App?= =?UTF-8?q?lication=E9=85=8D=E7=BD=AE=E6=96=87=E4=BB=B6;=20[Change]=20Main?= =?UTF-8?q?=20=E7=A7=BB=E9=99=A4CQConfig=E7=B1=BB=E5=BC=95=E7=94=A8;=20[Up?= =?UTF-8?q?date]=20net.lz1998:spring-cq=20=E5=8D=87=E7=BA=A7=E4=BE=9D?= =?UTF-8?q?=E8=B5=96=E9=A1=B9=E7=89=88=E6=9C=AC(4.14.0.6=20->=204.15.0.1);?= =?UTF-8?q?=20[Change]=20SpringCQMessageSenderFactory,=20CQPluginMain=20?= =?UTF-8?q?=E4=BD=BF=E7=94=A8=E8=BE=83=E4=B8=BA=E5=A6=A5=E5=8D=8F=E7=9A=84?= =?UTF-8?q?=E6=96=B9=E6=B3=95=E5=AE=9E=E7=8E=B0SenderFactory;?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 2 +- src/main/java/net/lamgc/cgj/Main.java | 2 -- .../cgj/bot/framework/coolq/CQConfig.java | 27 ------------------- .../cgj/bot/framework/coolq/CQPluginMain.java | 2 ++ .../message/SpringCQMessageSenderFactory.java | 17 ++++++++++-- src/main/resources/application.properties | 2 -- src/main/resources/application.yml | 12 +++++++++ 7 files changed, 30 insertions(+), 34 deletions(-) delete mode 100644 src/main/java/net/lamgc/cgj/bot/framework/coolq/CQConfig.java delete mode 100644 src/main/resources/application.properties create mode 100644 src/main/resources/application.yml 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