feat: 新增配置信息上下文.
This commit is contained in:
@@ -24,4 +24,9 @@ public final class ConfigurationKeys {
|
||||
* 定时任务周期: 获取最新IP
|
||||
*/
|
||||
public static final String KEY_TASK_REFRESH_INTERVAL_IP = "system.task.refresh.interval.ip";
|
||||
|
||||
/**
|
||||
* 阿里云解析线路
|
||||
*/
|
||||
public static final String KEY_ALIYUN_ENDPOINT = "instance.aliyun.endpoint.url";
|
||||
}
|
||||
|
||||
@@ -8,15 +8,15 @@ import com.fasterxml.jackson.annotation.JsonIgnore;
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.json.JsonMapper;
|
||||
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement;
|
||||
import com.serliunx.ddns.support.ConfigurationContextHolder;
|
||||
import darabonba.core.client.ClientOverrideConfiguration;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.TimeoutException;
|
||||
|
||||
import static com.serliunx.ddns.config.ConfigurationKeys.KEY_ALIYUN_ENDPOINT;
|
||||
import static com.serliunx.ddns.constant.SystemConstants.XML_ROOT_INSTANCE_NAME;
|
||||
|
||||
/**
|
||||
@@ -25,7 +25,6 @@ import static com.serliunx.ddns.constant.SystemConstants.XML_ROOT_INSTANCE_NAME;
|
||||
* @version 1.0.0
|
||||
* @since 2024/5/15
|
||||
*/
|
||||
@SuppressWarnings("all")
|
||||
@JacksonXmlRootElement(localName = XML_ROOT_INSTANCE_NAME)
|
||||
public class AliyunInstance extends AbstractInstance {
|
||||
|
||||
@@ -96,10 +95,10 @@ public class AliyunInstance extends AbstractInstance {
|
||||
.credentialsProvider(provider)
|
||||
.overrideConfiguration(
|
||||
ClientOverrideConfiguration.create()
|
||||
.setEndpointOverride("alidns.cn-hangzhou.aliyuncs.com")
|
||||
.setEndpointOverride(ConfigurationContextHolder.getConfiguration().getString(KEY_ALIYUN_ENDPOINT))
|
||||
)
|
||||
.build();
|
||||
log.debug(getName() + ": 初始化完成.");
|
||||
log.debug("{}: 初始化完成.", getName());
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -115,7 +114,7 @@ public class AliyunInstance extends AbstractInstance {
|
||||
try {
|
||||
requestResponse.whenComplete((v, t) -> {
|
||||
if (t != null) { //出现异常
|
||||
log.error("出现异常 {}:", t.getCause(), t.getMessage());
|
||||
log.error("出现异常 {} : {}", t.getCause(), t.getMessage());
|
||||
} else {
|
||||
String result = null;
|
||||
try {
|
||||
@@ -145,7 +144,7 @@ public class AliyunInstance extends AbstractInstance {
|
||||
}
|
||||
return null;
|
||||
} catch (InterruptedException | ExecutionException e) {
|
||||
log.error("出现了不应该出现的异常 => {}", e);
|
||||
log.error("出现了不应该出现的异常 => {}", e.getMessage());
|
||||
return null;
|
||||
} catch (TimeoutException e) {
|
||||
log.error("记录查询超时!");
|
||||
|
||||
@@ -0,0 +1,36 @@
|
||||
package com.serliunx.ddns.support;
|
||||
|
||||
import com.serliunx.ddns.config.Configuration;
|
||||
|
||||
/**
|
||||
* 配置信息上下文
|
||||
* <li> {@link Configuration}
|
||||
* @author <a href="mailto:serliunx@yeah.net">SerLiunx</a>
|
||||
* @version 1.0.1
|
||||
* @since 2024/7/8
|
||||
*/
|
||||
public final class ConfigurationContextHolder {
|
||||
|
||||
private static final ThreadLocal<Configuration> CONFIGURATION_HOLDER = new ThreadLocal<>();
|
||||
|
||||
private ConfigurationContextHolder() {throw new UnsupportedOperationException();}
|
||||
|
||||
/**
|
||||
* 获取配置信息
|
||||
*
|
||||
* @return 配置信息
|
||||
*/
|
||||
public static Configuration getConfiguration() {
|
||||
return CONFIGURATION_HOLDER.get();
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置配置信息
|
||||
* <li> 一般在程序初始化时调用
|
||||
*
|
||||
* @param configuration 配置信息
|
||||
*/
|
||||
public static void setConfiguration(Configuration configuration) {
|
||||
CONFIGURATION_HOLDER.set(configuration);
|
||||
}
|
||||
}
|
||||
@@ -73,6 +73,7 @@ public final class SystemInitializer implements Refreshable, Clearable {
|
||||
|
||||
// 刷新配置信息
|
||||
configuration.refresh();
|
||||
ConfigurationContextHolder.setConfiguration(configuration);
|
||||
|
||||
// 获取核心线程数量, 默认为CPU核心数量
|
||||
int coreSize = configuration.getInteger(KEY_THREAD_POOL_CORE_SIZE, Runtime.getRuntime().availableProcessors());
|
||||
|
||||
Reference in New Issue
Block a user