mirror of
https://github.com/LamGC/ContentGrabbingJi.git
synced 2025-04-29 22:27:33 +00:00
[Change] SpringCQApplication, Main 将SpringCQ启动部分迁移到SpringCQApplication内;
This commit is contained in:
parent
d3d6f151d4
commit
553212e556
@ -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
|
||||
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user