[Change] SpringCQApplication, Main 将SpringCQ启动部分迁移到SpringCQApplication内;

This commit is contained in:
LamGC 2020-07-03 00:23:56 +08:00
parent d3d6f151d4
commit 553212e556
Signed by: LamGC
GPG Key ID: 6C5AE2A913941E1D
2 changed files with 64 additions and 10 deletions

View File

@ -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<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);
new SpringCQApplication().start(argsStr);
}
@Command

View File

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