change: 调整日志格式. ***命令相关功能暂时关闭.
This commit is contained in:
@@ -4,12 +4,8 @@ import com.serliunx.ddns.config.PropertiesConfiguration;
|
||||
import com.serliunx.ddns.constant.SystemConstants;
|
||||
import com.serliunx.ddns.core.context.FileInstanceContext;
|
||||
import com.serliunx.ddns.support.SystemInitializer;
|
||||
import com.serliunx.ddns.support.SystemSupport;
|
||||
import com.serliunx.ddns.support.command.CommandManager;
|
||||
import com.serliunx.ddns.support.command.cmd.ExitCommand;
|
||||
import com.serliunx.ddns.support.command.cmd.HelpCommand;
|
||||
import com.serliunx.ddns.support.command.cmd.IpCommand;
|
||||
import org.slf4j.MDC;
|
||||
import com.serliunx.ddns.support.command.cmd.*;
|
||||
|
||||
import java.util.Scanner;
|
||||
|
||||
@@ -21,21 +17,8 @@ import java.util.Scanner;
|
||||
public final class ManagerLite {
|
||||
|
||||
public static void main(String[] args) {
|
||||
// 日志参数调整
|
||||
beforeInit();
|
||||
|
||||
// 容器初始化
|
||||
SystemInitializer initializer = init();
|
||||
|
||||
// 指令注册
|
||||
CommandManager commandManager = registerCommand(initializer);
|
||||
|
||||
// 指令监听
|
||||
handleCommand(commandManager, initializer);
|
||||
}
|
||||
|
||||
private static void beforeInit() {
|
||||
MDC.put("pid", SystemSupport.getPid());
|
||||
init();
|
||||
}
|
||||
|
||||
private static SystemInitializer init() {
|
||||
@@ -48,14 +31,18 @@ public final class ManagerLite {
|
||||
return systemInitializer;
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
private static CommandManager registerCommand(SystemInitializer systemInitializer) {
|
||||
CommandManager commandManager = new CommandManager(systemInitializer);
|
||||
commandManager.register(new HelpCommand(commandManager));
|
||||
commandManager.register(new IpCommand(commandManager));
|
||||
commandManager.register(new ExitCommand(commandManager));
|
||||
commandManager.register(new InstanceCommand(commandManager));
|
||||
commandManager.register(new RefreshCommand(commandManager));
|
||||
return commandManager;
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
@SuppressWarnings("all")
|
||||
private static void handleCommand(CommandManager commandManager, SystemInitializer systemInitializer) {
|
||||
Scanner scanner = new Scanner(System.in);
|
||||
|
||||
@@ -53,6 +53,12 @@ public abstract class AbstractInstance implements Instance {
|
||||
*/
|
||||
protected String value;
|
||||
|
||||
/**
|
||||
* 暂停状态
|
||||
* <li> 默认为非暂停状态
|
||||
*/
|
||||
protected volatile boolean pause = false;
|
||||
|
||||
@Override
|
||||
public void refresh() {
|
||||
// 调用子类的初始化逻辑
|
||||
@@ -61,6 +67,8 @@ public abstract class AbstractInstance implements Instance {
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
if (isPause()) // 暂停态检查, 已暂停则不继续进行.
|
||||
return;
|
||||
value = query();
|
||||
final String ipAddress = NetworkContextHolder.getIpAddress();
|
||||
try {
|
||||
@@ -97,18 +105,22 @@ public abstract class AbstractInstance implements Instance {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getFatherName() {
|
||||
return fatherName;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setFatherName(String fatherName) {
|
||||
this.fatherName = fatherName;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Long getInterval() {
|
||||
return interval;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setInterval(Long interval) {
|
||||
this.interval = interval;
|
||||
}
|
||||
@@ -128,10 +140,21 @@ public abstract class AbstractInstance implements Instance {
|
||||
this.source = instanceSource;
|
||||
}
|
||||
|
||||
@Override
|
||||
public InstanceSource getSource() {
|
||||
return source;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setPause(boolean pause) {
|
||||
this.pause = pause;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isPause() {
|
||||
return pause;
|
||||
}
|
||||
|
||||
/**
|
||||
* 具体的初始化逻辑
|
||||
*/
|
||||
|
||||
@@ -75,4 +75,20 @@ public interface Instance extends Runnable, Refreshable {
|
||||
* @return 通过校验返回真, 否则返回假
|
||||
*/
|
||||
boolean validate();
|
||||
|
||||
/**
|
||||
* 设置实例是否暂停
|
||||
* <li> 暂停之后实力不会进行下一次活动, 不影响正在更新的实力。
|
||||
* <li> 暂停态目前只有一个检查点, 详情见{@link AbstractInstance#run()}
|
||||
* @param pause 暂停状态
|
||||
*/
|
||||
void setPause(boolean pause);
|
||||
|
||||
/**
|
||||
* 判断实例是否处于暂停状态
|
||||
* @return 暂停中返回真, 否则返回假
|
||||
*/
|
||||
default boolean isPause() {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -171,7 +171,6 @@ public final class SystemInitializer implements Refreshable, Clearable {
|
||||
|
||||
// 添加进程结束钩子函数
|
||||
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
|
||||
MDC.put("pid", SystemSupport.getPid());
|
||||
log.info("程序正在关闭中, 可能需要一定时间.");
|
||||
scheduledThreadPoolExecutor.shutdown();
|
||||
log.info("已关闭.");
|
||||
|
||||
@@ -1,22 +0,0 @@
|
||||
package com.serliunx.ddns.support;
|
||||
|
||||
import java.lang.management.ManagementFactory;
|
||||
|
||||
/**
|
||||
* @author SerLiunx
|
||||
* @since 1.0
|
||||
*/
|
||||
public final class SystemSupport {
|
||||
|
||||
private static final String PID;
|
||||
|
||||
static {
|
||||
PID = ManagementFactory.getRuntimeMXBean().getName().split("@")[0];
|
||||
}
|
||||
|
||||
private SystemSupport(){throw new UnsupportedOperationException();}
|
||||
|
||||
public static String getPid() {
|
||||
return PID;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package com.serliunx.ddns.support.command.cmd;
|
||||
|
||||
import com.serliunx.ddns.support.command.CommandManager;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
/**
|
||||
* 实例相关指令
|
||||
* @author <a href="mailto:serliunx@yeah.net">SerLiunx</a>
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public class InstanceCommand extends AbstractCommand {
|
||||
|
||||
public InstanceCommand(Command[] children, CommandManager commandManager) {
|
||||
super("instance", children, commandManager);
|
||||
}
|
||||
|
||||
public InstanceCommand(CommandManager commandManager) {
|
||||
this(null, commandManager);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean execute(String[] args) {
|
||||
System.out.println(Arrays.toString(args));
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
package com.serliunx.ddns.support.command.cmd;
|
||||
|
||||
import com.serliunx.ddns.support.command.CommandManager;
|
||||
|
||||
/**
|
||||
* @author <a href="mailto:serliunx@yeah.net">SerLiunx</a>
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public class RefreshCommand extends AbstractCommand {
|
||||
|
||||
public RefreshCommand(Command[] children, CommandManager commandManager) {
|
||||
super("refresh", children, commandManager);
|
||||
}
|
||||
|
||||
public RefreshCommand(CommandManager commandManager) {
|
||||
this(null, commandManager);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean execute(String[] args) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package com.serliunx.ddns.support.log;
|
||||
|
||||
import ch.qos.logback.classic.pattern.ClassicConverter;
|
||||
import ch.qos.logback.classic.spi.ILoggingEvent;
|
||||
|
||||
/**
|
||||
* 日志变量%pid(进程id) 转换器
|
||||
* @author <a href="mailto:serliunx@yeah.net">SerLiunx</a>
|
||||
* @version 1.0.0
|
||||
* @since 2024/6/15
|
||||
*/
|
||||
public class ProcessIdConverter extends ClassicConverter {
|
||||
|
||||
private static final String PROCESS_ID = java.lang.management.ManagementFactory.getRuntimeMXBean().getName().split("@")[0];
|
||||
|
||||
@Override
|
||||
public String convert(ILoggingEvent iLoggingEvent) {
|
||||
return PROCESS_ID;
|
||||
}
|
||||
}
|
||||
@@ -1,9 +1,7 @@
|
||||
package com.serliunx.ddns.thread;
|
||||
|
||||
import com.serliunx.ddns.support.Assert;
|
||||
import com.serliunx.ddns.support.SystemSupport;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.slf4j.MDC;
|
||||
|
||||
import java.util.concurrent.ThreadFactory;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
@@ -19,11 +17,7 @@ public class TaskThreadFactory implements ThreadFactory {
|
||||
@Override
|
||||
public Thread newThread(@NotNull Runnable r) {
|
||||
Assert.notNull(r);
|
||||
Runnable runnable = () -> {
|
||||
MDC.put("pid", SystemSupport.getPid());
|
||||
r.run();
|
||||
};
|
||||
return new Thread(runnable, String.format(getNamePattern(), count.getAndIncrement()));
|
||||
return new Thread(r, String.format(getNamePattern(), count.getAndIncrement()));
|
||||
}
|
||||
|
||||
protected String getNamePattern() {
|
||||
|
||||
Reference in New Issue
Block a user