[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.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 org.springframework.boot.autoconfigure.SpringBootApplication;
import java.io.*; import java.io.*;
@ -34,8 +33,6 @@ 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;
@ -104,7 +101,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) {
new SpringCQApplication().start(argsStr); FrameworkManager.registerFramework(new SpringCQApplication());
} }
@Command @Command

View File

@ -27,6 +27,6 @@ public interface Framework {
* @return 返回标识名 * @return 返回标识名
*/ */
default String getName() { 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; package net.lamgc.cgj.bot.framework.coolq;
import com.google.common.base.Strings;
import net.lamgc.cgj.Main; import net.lamgc.cgj.Main;
import net.lamgc.cgj.bot.boot.BotGlobal; 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.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication; import org.springframework.boot.SpringApplication;
import org.springframework.boot.context.event.ApplicationFailedEvent; import org.springframework.boot.context.event.ApplicationFailedEvent;
import org.springframework.context.ApplicationListener; 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.ContextClosedEvent;
import org.springframework.context.event.ContextStoppedEvent; import org.springframework.context.event.ContextStoppedEvent;
import java.util.ArrayList; public class SpringCQApplication implements Framework {
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class SpringCQApplication { private Logger log;
private final static Logger log = LoggerFactory.getLogger(SpringCQApplication.class);
private final Object quitLock = new Object(); private final Object quitLock = new Object();
public void start(String argsStr) { @Override
log.info("酷Q机器人根目录: {}", BotGlobal.getGlobal().getDataStoreDir().getPath()); public void init(FrameworkManager.FrameworkResources resources) {
Pattern pattern = Pattern.compile("/\\s*(\".+?\"|[^:\\s])+((\\s*:\\s*(\".+?\"|[^\\s])+)|)|(\".+?\"|[^\"\\s])+"); this.log = resources.getLogger();
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); public void run() {
ConfigurableApplicationContext context = SpringApplication.run(Main.class, args); log.info("酷Q机器人根目录: {}", BotGlobal.getGlobal().getDataStoreDir().getPath());
ConfigurableApplicationContext context = SpringApplication.run(Main.class);
registerShutdownHook(context); registerShutdownHook(context);
try { try {
synchronized (quitLock) { synchronized (quitLock) {
@ -41,19 +34,22 @@ public class SpringCQApplication {
} catch (InterruptedException e) { } catch (InterruptedException e) {
log.warn("发生中断, 退出SpringCQ...", e); log.warn("发生中断, 退出SpringCQ...", e);
} }
context.stop();
context.close();
} }
private void registerShutdownHook(ConfigurableApplicationContext context) { private void registerShutdownHook(ConfigurableApplicationContext context) {
context.addApplicationListener((ApplicationListener<ApplicationFailedEvent>) context.addApplicationListener((ApplicationListener<ApplicationFailedEvent>)
event -> notifyThread()); event -> close());
context.addApplicationListener((ApplicationListener<ContextClosedEvent>) context.addApplicationListener((ApplicationListener<ContextClosedEvent>)
event -> notifyThread()); event -> close());
context.addApplicationListener((ApplicationListener<ContextStoppedEvent>) context.addApplicationListener((ApplicationListener<ContextStoppedEvent>)
event -> notifyThread()); event -> close());
Runtime.getRuntime().addShutdownHook(new Thread(this::notifyThread)); Runtime.getRuntime().addShutdownHook(new Thread(this::close));
} }
private void notifyThread() { public void close() {
synchronized (quitLock) { synchronized (quitLock) {
quitLock.notify(); quitLock.notify();
} }