feat: 日志配置文件
This commit is contained in:
@@ -41,6 +41,11 @@ public final class SystemConstants {
|
|||||||
*/
|
*/
|
||||||
public static final String PROPERTIES_FILE = "settings.properties";
|
public static final String PROPERTIES_FILE = "settings.properties";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 日志配置文件名称
|
||||||
|
*/
|
||||||
|
public static final String LOG_CONFIG_FILE = "logback.xml";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* sqlite
|
* sqlite
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -17,8 +17,11 @@ import java.io.OutputStream;
|
|||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
import java.nio.file.Paths;
|
import java.nio.file.Paths;
|
||||||
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
import java.util.concurrent.CompletableFuture;
|
||||||
|
import java.util.concurrent.ScheduledFuture;
|
||||||
import java.util.concurrent.ScheduledThreadPoolExecutor;
|
import java.util.concurrent.ScheduledThreadPoolExecutor;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
@@ -39,7 +42,7 @@ public final class SystemInitializer implements Refreshable {
|
|||||||
|
|
||||||
private ScheduledThreadPoolExecutor scheduledThreadPoolExecutor;
|
private ScheduledThreadPoolExecutor scheduledThreadPoolExecutor;
|
||||||
private Set<Instance> instances;
|
private Set<Instance> instances;
|
||||||
private Map<String, Instance> runningInstances;
|
private final Map<String, ScheduledFuture<?>> runningInstances = new HashMap<>(64);
|
||||||
|
|
||||||
SystemInitializer(Configuration configuration, MultipleSourceInstanceContext instanceContext) {
|
SystemInitializer(Configuration configuration, MultipleSourceInstanceContext instanceContext) {
|
||||||
this.configuration = configuration;
|
this.configuration = configuration;
|
||||||
@@ -56,6 +59,8 @@ public final class SystemInitializer implements Refreshable {
|
|||||||
|
|
||||||
// 释放配置文件
|
// 释放配置文件
|
||||||
releaseResource(SystemConstants.PROPERTIES_FILE);
|
releaseResource(SystemConstants.PROPERTIES_FILE);
|
||||||
|
// 释放日志文件
|
||||||
|
releaseResource(SystemConstants.LOG_CONFIG_FILE);
|
||||||
|
|
||||||
// 刷新配置信息
|
// 刷新配置信息
|
||||||
configuration.refresh();
|
configuration.refresh();
|
||||||
@@ -66,6 +71,9 @@ public final class SystemInitializer implements Refreshable {
|
|||||||
// 初始化线程池
|
// 初始化线程池
|
||||||
initThreadPool(coreSize);
|
initThreadPool(coreSize);
|
||||||
|
|
||||||
|
// 尝试链接dashboard
|
||||||
|
registerToDashboard();
|
||||||
|
|
||||||
// 加载实例(不同的容器加载时机不同)
|
// 加载实例(不同的容器加载时机不同)
|
||||||
loadInstances();
|
loadInstances();
|
||||||
|
|
||||||
@@ -128,7 +136,9 @@ public final class SystemInitializer implements Refreshable {
|
|||||||
}
|
}
|
||||||
// 初始化实例
|
// 初始化实例
|
||||||
i.refresh();
|
i.refresh();
|
||||||
scheduledThreadPoolExecutor.scheduleWithFixedDelay(i, 0, i.getInterval(), TimeUnit.SECONDS);
|
ScheduledFuture<?> future = scheduledThreadPoolExecutor.scheduleWithFixedDelay(i, 0,
|
||||||
|
i.getInterval(), TimeUnit.SECONDS);
|
||||||
|
runningInstances.put(i.getName(), future);
|
||||||
log.info("{}({})已启动, 运行周期 {} 秒.", i.getName(), i.getType(), i.getInterval());
|
log.info("{}({})已启动, 运行周期 {} 秒.", i.getName(), i.getType(), i.getInterval());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -161,4 +171,12 @@ public final class SystemInitializer implements Refreshable {
|
|||||||
log.info("已关闭.");
|
log.info("已关闭.");
|
||||||
}, "DDNS-ShutDownHook"));
|
}, "DDNS-ShutDownHook"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void registerToDashboard() {
|
||||||
|
CompletableFuture.runAsync(() -> {
|
||||||
|
|
||||||
|
}, scheduledThreadPoolExecutor).whenComplete((r, t) -> {
|
||||||
|
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -23,6 +23,10 @@ public class TaskThreadFactory implements ThreadFactory {
|
|||||||
MDC.put("pid", SystemSupport.getPid());
|
MDC.put("pid", SystemSupport.getPid());
|
||||||
r.run();
|
r.run();
|
||||||
};
|
};
|
||||||
return new Thread(runnable, String.format("ddns-task-%s", count.getAndIncrement()));
|
return new Thread(runnable, String.format(getNamePattern(), count.getAndIncrement()));
|
||||||
|
}
|
||||||
|
|
||||||
|
protected String getNamePattern() {
|
||||||
|
return "ddns-task-%s";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,13 @@
|
|||||||
|
package com.serliunx.ddns.thread;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author SerLiunx
|
||||||
|
* @since 1.0
|
||||||
|
*/
|
||||||
|
public class UtilThreadFactory extends TaskThreadFactory {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected String getNamePattern() {
|
||||||
|
return "ddns-util-%s";
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,7 +1,9 @@
|
|||||||
package com.serliunx.ddns.test;
|
package com.serliunx.ddns.test;
|
||||||
|
|
||||||
import com.serliunx.ddns.constant.SystemConstants;
|
import com.serliunx.ddns.constant.SystemConstants;
|
||||||
|
import com.serliunx.ddns.core.context.FileInstanceContext;
|
||||||
import com.serliunx.ddns.core.context.GenericInstanceContext;
|
import com.serliunx.ddns.core.context.GenericInstanceContext;
|
||||||
|
import com.serliunx.ddns.core.context.MultipleSourceInstanceContext;
|
||||||
import com.serliunx.ddns.core.factory.JsonFileInstanceFactory;
|
import com.serliunx.ddns.core.factory.JsonFileInstanceFactory;
|
||||||
import com.serliunx.ddns.core.factory.XmlFileInstanceFactory;
|
import com.serliunx.ddns.core.factory.XmlFileInstanceFactory;
|
||||||
import com.serliunx.ddns.core.factory.YamlFileInstanceFactory;
|
import com.serliunx.ddns.core.factory.YamlFileInstanceFactory;
|
||||||
@@ -14,7 +16,7 @@ import org.junit.Test;
|
|||||||
public class ContextTest {
|
public class ContextTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testContext() {
|
public void testGenericContext() {
|
||||||
GenericInstanceContext genericInstanceContext = new GenericInstanceContext();
|
GenericInstanceContext genericInstanceContext = new GenericInstanceContext();
|
||||||
|
|
||||||
genericInstanceContext.addListableInstanceFactory(new XmlFileInstanceFactory(SystemConstants.USER_INSTANCE_DIR));
|
genericInstanceContext.addListableInstanceFactory(new XmlFileInstanceFactory(SystemConstants.USER_INSTANCE_DIR));
|
||||||
@@ -24,4 +26,11 @@ public class ContextTest {
|
|||||||
genericInstanceContext.refresh();
|
genericInstanceContext.refresh();
|
||||||
genericInstanceContext.getInstances().forEach(System.out::println);
|
genericInstanceContext.getInstances().forEach(System.out::println);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testFileContext(){
|
||||||
|
MultipleSourceInstanceContext context = new FileInstanceContext();
|
||||||
|
|
||||||
|
context.getInstances().forEach(System.out::println);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user