[Change] Framework 调整"getName"方法的默认实现;

[Change] Main 调整pluginMode启动方式;
[Change] SpringCQApplication 实现Framework接口;
This commit is contained in:
LamGC 2020-07-03 08:01:31 +08:00
parent 5c2b6b4ee5
commit 1599a5325a
Signed by: LamGC
GPG Key ID: 6C5AE2A913941E1D
3 changed files with 21 additions and 28 deletions

View File

@ -26,7 +26,6 @@ import org.apache.http.util.EntityUtils;
import org.apache.tomcat.util.http.fileupload.util.Streams;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import java.io.*;
@ -34,8 +33,6 @@ import java.nio.charset.StandardCharsets;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
@ -104,7 +101,7 @@ public class Main {
@Command
public static void pluginMode(@Argument(name = "args", force = false) String argsStr) {
new SpringCQApplication().start(argsStr);
FrameworkManager.registerFramework(new SpringCQApplication());
}
@Command

View File

@ -27,6 +27,6 @@ public interface Framework {
* @return 返回标识名
*/
default String getName() {
return this.toString();
return this.getClass().getSimpleName() + "@" + Integer.toHexString(this.hashCode());
}
}

View File

@ -1,10 +1,10 @@
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 net.lamgc.cgj.bot.framework.Framework;
import net.lamgc.cgj.bot.framework.FrameworkManager;
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;
@ -12,27 +12,20 @@ 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 implements Framework {
public class SpringCQApplication {
private final static Logger log = LoggerFactory.getLogger(SpringCQApplication.class);
private Logger log;
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());
@Override
public void init(FrameworkManager.FrameworkResources resources) {
this.log = resources.getLogger();
}
String[] args = new String[argsList.size()];
argsList.toArray(args);
ConfigurableApplicationContext context = SpringApplication.run(Main.class, args);
public void run() {
log.info("酷Q机器人根目录: {}", BotGlobal.getGlobal().getDataStoreDir().getPath());
ConfigurableApplicationContext context = SpringApplication.run(Main.class);
registerShutdownHook(context);
try {
synchronized (quitLock) {
@ -41,19 +34,22 @@ public class SpringCQApplication {
} catch (InterruptedException e) {
log.warn("发生中断, 退出SpringCQ...", e);
}
context.stop();
context.close();
}
private void registerShutdownHook(ConfigurableApplicationContext context) {
context.addApplicationListener((ApplicationListener<ApplicationFailedEvent>)
event -> notifyThread());
event -> close());
context.addApplicationListener((ApplicationListener<ContextClosedEvent>)
event -> notifyThread());
event -> close());
context.addApplicationListener((ApplicationListener<ContextStoppedEvent>)
event -> notifyThread());
Runtime.getRuntime().addShutdownHook(new Thread(this::notifyThread));
event -> close());
Runtime.getRuntime().addShutdownHook(new Thread(this::close));
}
private void notifyThread() {
public void close() {
synchronized (quitLock) {
quitLock.notify();
}