Merge branch 'update-SpringCQ'

This commit is contained in:
LamGC 2020-07-03 08:20:18 +08:00
commit 05e933838e
Signed by: LamGC
GPG Key ID: 6C5AE2A913941E1D
8 changed files with 95 additions and 51 deletions

View File

@ -121,7 +121,7 @@
<dependency> <dependency>
<groupId>net.lz1998</groupId> <groupId>net.lz1998</groupId>
<artifactId>spring-cq</artifactId> <artifactId>spring-cq</artifactId>
<version>4.14.0.6</version> <version>4.15.0.1</version>
<exclusions> <exclusions>
<exclusion> <exclusion>
<groupId>ch.qos.logback</groupId> <groupId>ch.qos.logback</groupId>

View File

@ -8,7 +8,7 @@ import com.google.gson.JsonObject;
import net.lamgc.cgj.bot.boot.ApplicationBoot; import net.lamgc.cgj.bot.boot.ApplicationBoot;
import net.lamgc.cgj.bot.boot.BotGlobal; import net.lamgc.cgj.bot.boot.BotGlobal;
import net.lamgc.cgj.bot.framework.cli.ConsoleMain; import net.lamgc.cgj.bot.framework.cli.ConsoleMain;
import net.lamgc.cgj.bot.framework.coolq.CQConfig; import net.lamgc.cgj.bot.framework.coolq.SpringCQApplication;
import net.lamgc.cgj.bot.framework.mirai.MiraiMain; import net.lamgc.cgj.bot.framework.mirai.MiraiMain;
import net.lamgc.cgj.pixiv.PixivDownload; import net.lamgc.cgj.pixiv.PixivDownload;
import net.lamgc.cgj.pixiv.PixivSearchLinkBuilder; import net.lamgc.cgj.pixiv.PixivSearchLinkBuilder;
@ -25,20 +25,15 @@ import org.apache.http.util.EntityUtils;
import org.apache.tomcat.util.http.fileupload.util.Streams; import org.apache.tomcat.util.http.fileupload.util.Streams;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import java.io.*; import java.io.*;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.text.ParseException; import java.text.ParseException;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.*; import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.zip.ZipEntry; import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream; import java.util.zip.ZipOutputStream;
@SpringBootApplication
public class Main { public class Main {
private final static Logger log = LoggerFactory.getLogger(Main.class); private final static Logger log = LoggerFactory.getLogger(Main.class);
@ -105,17 +100,7 @@ public class Main {
@Command @Command
public static void pluginMode(@Argument(name = "args", force = false) String argsStr) { public static void pluginMode(@Argument(name = "args", force = false) String argsStr) {
log.info("酷Q机器人根目录: {}", BotGlobal.getGlobal().getDataStoreDir().getPath()); new SpringCQApplication().start(argsStr);
CQConfig.init();
Pattern pattern = Pattern.compile("/\\s*(\".+?\"|[^:\\s])+((\\s*:\\s*(\".+?\"|[^\\s])+)|)|(\".+?\"|[^\"\\s])+");
Matcher matcher = pattern.matcher(Strings.nullToEmpty(argsStr));
ArrayList<String> argsList = new ArrayList<>();
while (matcher.find()) {
argsList.add(matcher.group());
}
String[] args = new String[argsList.size()];
argsList.toArray(args);
SpringApplication.run(Main.class, args);
} }
@Command @Command

View File

@ -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()
);
}
}

View File

@ -3,6 +3,7 @@ package net.lamgc.cgj.bot.framework.coolq;
import net.lamgc.cgj.bot.boot.ApplicationBoot; import net.lamgc.cgj.bot.boot.ApplicationBoot;
import net.lamgc.cgj.bot.event.BotEventHandler; import net.lamgc.cgj.bot.event.BotEventHandler;
import net.lamgc.cgj.bot.framework.coolq.message.SpringCQMessageEvent; 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.lamgc.utils.event.EventHandler;
import net.lz1998.cq.event.message.CQDiscussMessageEvent; import net.lz1998.cq.event.message.CQDiscussMessageEvent;
import net.lz1998.cq.event.message.CQGroupMessageEvent; import net.lz1998.cq.event.message.CQGroupMessageEvent;
@ -17,8 +18,6 @@ import org.springframework.stereotype.Component;
public class CQPluginMain extends CQPlugin implements EventHandler { public class CQPluginMain extends CQPlugin implements EventHandler {
public CQPluginMain() { public CQPluginMain() {
// TODO(LamGC, 2020.04.21): SpringCQ无法适配MessageSenderBuilder
// MessageSenderBuilder.setCurrentMessageSenderFactory(new SpringCQMessageSenderFactory());
ApplicationBoot.initialBot(); ApplicationBoot.initialBot();
LoggerFactory.getLogger(CQPluginMain.class) LoggerFactory.getLogger(CQPluginMain.class)
.info("BotEventHandler.COMMAND_PREFIX = {}", BotEventHandler.COMMAND_PREFIX); .info("BotEventHandler.COMMAND_PREFIX = {}", BotEventHandler.COMMAND_PREFIX);
@ -46,6 +45,7 @@ public class CQPluginMain extends CQPlugin implements EventHandler {
* @return 是否拦截消息 * @return 是否拦截消息
*/ */
private static int processMessage(CoolQ cq, CQMessageEvent event) { private static int processMessage(CoolQ cq, CQMessageEvent event) {
SpringCQMessageSenderFactory.setCoolQ(cq);
if(BotEventHandler.mismatch(event.getMessage())) { if(BotEventHandler.mismatch(event.getMessage())) {
return MESSAGE_IGNORE; return MESSAGE_IGNORE;
} }

View File

@ -0,0 +1,63 @@
package net.lamgc.cgj.bot.framework.coolq;
import com.google.common.base.Strings;
import net.lamgc.cgj.bot.boot.BotGlobal;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
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;
@SpringBootApplication
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<String> argsList = new ArrayList<>();
while (matcher.find()) {
argsList.add(matcher.group());
}
String[] args = new String[argsList.size()];
argsList.toArray(args);
ConfigurableApplicationContext context = SpringApplication.run(SpringCQApplication.class, args);
registerShutdownHook(context);
try {
synchronized (quitLock) {
quitLock.wait();
}
} catch (InterruptedException e) {
log.warn("发生中断, 退出SpringCQ...", e);
}
}
private void registerShutdownHook(ConfigurableApplicationContext context) {
context.addApplicationListener((ApplicationListener<ApplicationFailedEvent>)
event -> notifyThread());
context.addApplicationListener((ApplicationListener<ContextClosedEvent>)
event -> notifyThread());
context.addApplicationListener((ApplicationListener<ContextStoppedEvent>)
event -> notifyThread());
Runtime.getRuntime().addShutdownHook(new Thread(this::notifyThread));
}
private void notifyThread() {
synchronized (quitLock) {
quitLock.notify();
}
}
}

View File

@ -6,13 +6,26 @@ import net.lamgc.cgj.bot.message.MessageSource;
import net.lz1998.cq.robot.CoolQ; import net.lz1998.cq.robot.CoolQ;
import java.util.Objects; import java.util.Objects;
import java.util.concurrent.atomic.AtomicReference;
public class SpringCQMessageSenderFactory implements MessageSenderFactory { public class SpringCQMessageSenderFactory implements MessageSenderFactory {
private final static ThreadLocal<CoolQ> threadCoolQ = new ThreadLocal<>(); private final static AtomicReference<CoolQ> coolQ = new AtomicReference<>();
/**
* 设置CoolQ对象.
* <p>该方法仅接受第一次设置的CoolQ对象, 其他对象将会忽略.</p>
* @param coolQObj CoolQ对象
*/
public static void setCoolQ(CoolQ coolQObj) {
if(coolQ.get() == null) {
coolQ.set(coolQObj);
}
}
@Override @Override
public MessageSender createMessageSender(MessageSource source, long id) { public MessageSender createMessageSender(MessageSource source, long id) {
return new SpringCQMessageSender( 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);
} }
} }

View File

@ -1,2 +0,0 @@
server.port=8081
server.tomcat.max-threads=1

View File

@ -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