From d3d6f151d45096db0345ef6f2ab6f6fefa8a0bc4 Mon Sep 17 00:00:00 2001 From: LamGC Date: Thu, 2 Jul 2020 14:02:01 +0800 Subject: [PATCH 1/2] =?UTF-8?q?[Delete]=20application.properties=20?= =?UTF-8?q?=E7=A7=BB=E9=99=A4Properties=E7=89=88Application=E9=85=8D?= =?UTF-8?q?=E7=BD=AE=E6=96=87=E4=BB=B6;=20[Delete]=20CQConfig=20=E7=A7=BB?= =?UTF-8?q?=E9=99=A4=E4=B8=8D=E5=85=BC=E5=AE=B9=E7=9A=84=E7=B1=BB;=20[Add]?= =?UTF-8?q?=20application.yml=20=E6=B7=BB=E5=8A=A0yml=E6=A0=BC=E5=BC=8F?= =?UTF-8?q?=E7=9A=84Application=E9=85=8D=E7=BD=AE=E6=96=87=E4=BB=B6;=20[Ch?= =?UTF-8?q?ange]=20Main=20=E7=A7=BB=E9=99=A4CQConfig=E7=B1=BB=E5=BC=95?= =?UTF-8?q?=E7=94=A8;=20[Update]=20net.lz1998:spring-cq=20=E5=8D=87?= =?UTF-8?q?=E7=BA=A7=E4=BE=9D=E8=B5=96=E9=A1=B9=E7=89=88=E6=9C=AC(4.14.0.6?= =?UTF-8?q?=20->=204.15.0.1);=20[Change]=20SpringCQMessageSenderFactory,?= =?UTF-8?q?=20CQPluginMain=20=E4=BD=BF=E7=94=A8=E8=BE=83=E4=B8=BA=E5=A6=A5?= =?UTF-8?q?=E5=8D=8F=E7=9A=84=E6=96=B9=E6=B3=95=E5=AE=9E=E7=8E=B0SenderFac?= =?UTF-8?q?tory;?= 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 From 553212e556f77ed4af659a10110a40a427674169 Mon Sep 17 00:00:00 2001 From: LamGC Date: Fri, 3 Jul 2020 00:23:56 +0800 Subject: [PATCH 2/2] =?UTF-8?q?[Change]=20SpringCQApplication,=20Main=20?= =?UTF-8?q?=E5=B0=86SpringCQ=E5=90=AF=E5=8A=A8=E9=83=A8=E5=88=86=E8=BF=81?= =?UTF-8?q?=E7=A7=BB=E5=88=B0SpringCQApplication=E5=86=85;?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/net/lamgc/cgj/Main.java | 12 +--- .../framework/coolq/SpringCQApplication.java | 62 +++++++++++++++++++ 2 files changed, 64 insertions(+), 10 deletions(-) create mode 100644 src/main/java/net/lamgc/cgj/bot/framework/coolq/SpringCQApplication.java diff --git a/src/main/java/net/lamgc/cgj/Main.java b/src/main/java/net/lamgc/cgj/Main.java index 33d727d..0ff5cbe 100644 --- a/src/main/java/net/lamgc/cgj/Main.java +++ b/src/main/java/net/lamgc/cgj/Main.java @@ -8,6 +8,7 @@ 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.SpringCQApplication; import net.lamgc.cgj.bot.framework.mirai.MiraiMain; import net.lamgc.cgj.pixiv.PixivDownload; import net.lamgc.cgj.pixiv.PixivSearchLinkBuilder; @@ -104,16 +105,7 @@ public class Main { @Command public static void pluginMode(@Argument(name = "args", force = false) String argsStr) { - log.info("酷Q机器人根目录: {}", BotGlobal.getGlobal().getDataStoreDir().getPath()); - Pattern pattern = Pattern.compile("/\\s*(\".+?\"|[^:\\s])+((\\s*:\\s*(\".+?\"|[^\\s])+)|)|(\".+?\"|[^\"\\s])+"); - Matcher matcher = pattern.matcher(Strings.nullToEmpty(argsStr)); - ArrayList argsList = new ArrayList<>(); - while (matcher.find()) { - argsList.add(matcher.group()); - } - String[] args = new String[argsList.size()]; - argsList.toArray(args); - SpringApplication.run(Main.class, args); + new SpringCQApplication().start(argsStr); } @Command diff --git a/src/main/java/net/lamgc/cgj/bot/framework/coolq/SpringCQApplication.java b/src/main/java/net/lamgc/cgj/bot/framework/coolq/SpringCQApplication.java new file mode 100644 index 0000000..0e4ae0b --- /dev/null +++ b/src/main/java/net/lamgc/cgj/bot/framework/coolq/SpringCQApplication.java @@ -0,0 +1,62 @@ +package net.lamgc.cgj.bot.framework.coolq; + +import com.google.common.base.Strings; +import net.lamgc.cgj.Main; +import net.lamgc.cgj.bot.boot.BotGlobal; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.context.event.ApplicationFailedEvent; +import org.springframework.context.ApplicationListener; +import org.springframework.context.ConfigurableApplicationContext; +import org.springframework.context.event.ContextClosedEvent; +import org.springframework.context.event.ContextStoppedEvent; + +import java.util.ArrayList; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +public class SpringCQApplication { + + private final static Logger log = LoggerFactory.getLogger(SpringCQApplication.class); + + private final Object quitLock = new Object(); + + public void start(String argsStr) { + log.info("酷Q机器人根目录: {}", BotGlobal.getGlobal().getDataStoreDir().getPath()); + Pattern pattern = Pattern.compile("/\\s*(\".+?\"|[^:\\s])+((\\s*:\\s*(\".+?\"|[^\\s])+)|)|(\".+?\"|[^\"\\s])+"); + Matcher matcher = pattern.matcher(Strings.nullToEmpty(argsStr)); + ArrayList argsList = new ArrayList<>(); + while (matcher.find()) { + argsList.add(matcher.group()); + } + String[] args = new String[argsList.size()]; + argsList.toArray(args); + ConfigurableApplicationContext context = SpringApplication.run(Main.class, args); + registerShutdownHook(context); + try { + synchronized (quitLock) { + quitLock.wait(); + } + } catch (InterruptedException e) { + log.warn("发生中断, 退出SpringCQ...", e); + } + } + + private void registerShutdownHook(ConfigurableApplicationContext context) { + context.addApplicationListener((ApplicationListener) + event -> notifyThread()); + context.addApplicationListener((ApplicationListener) + event -> notifyThread()); + context.addApplicationListener((ApplicationListener) + event -> notifyThread()); + Runtime.getRuntime().addShutdownHook(new Thread(this::notifyThread)); + } + + private void notifyThread() { + synchronized (quitLock) { + quitLock.notify(); + } + } + +}