change: 代码注释、调整代码.
This commit is contained in:
@@ -1,5 +1,6 @@
|
|||||||
package com.serliunx.ddns.config;
|
package com.serliunx.ddns.config;
|
||||||
|
|
||||||
|
import com.serliunx.ddns.constant.ConfigurationKeys;
|
||||||
import com.serliunx.ddns.support.Assert;
|
import com.serliunx.ddns.support.Assert;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
package com.serliunx.ddns.config;
|
package com.serliunx.ddns.constant;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 配置文件键常量信息
|
* 配置文件键常量信息
|
||||||
@@ -16,7 +16,7 @@ import java.util.concurrent.ExecutionException;
|
|||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
import java.util.concurrent.TimeoutException;
|
import java.util.concurrent.TimeoutException;
|
||||||
|
|
||||||
import static com.serliunx.ddns.config.ConfigurationKeys.KEY_ALIYUN_ENDPOINT;
|
import static com.serliunx.ddns.constant.ConfigurationKeys.KEY_ALIYUN_ENDPOINT;
|
||||||
import static com.serliunx.ddns.constant.SystemConstants.XML_ROOT_INSTANCE_NAME;
|
import static com.serliunx.ddns.constant.SystemConstants.XML_ROOT_INSTANCE_NAME;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ import org.slf4j.LoggerFactory;
|
|||||||
|
|
||||||
import java.util.concurrent.CountDownLatch;
|
import java.util.concurrent.CountDownLatch;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
|
import java.util.concurrent.atomic.AtomicInteger;
|
||||||
import java.util.concurrent.locks.Lock;
|
import java.util.concurrent.locks.Lock;
|
||||||
import java.util.concurrent.locks.ReentrantLock;
|
import java.util.concurrent.locks.ReentrantLock;
|
||||||
|
|
||||||
@@ -24,16 +25,28 @@ public final class NetworkContextHolder {
|
|||||||
// 外网IP地址获取
|
// 外网IP地址获取
|
||||||
private static final Integer IP_CONTEXT_TIME_OUT = 5;
|
private static final Integer IP_CONTEXT_TIME_OUT = 5;
|
||||||
private static volatile String IP_ADDRESS;
|
private static volatile String IP_ADDRESS;
|
||||||
|
/**
|
||||||
|
* 失败计数
|
||||||
|
* <p>
|
||||||
|
* 失败次数过多后, 会影响
|
||||||
|
*/
|
||||||
|
private static final AtomicInteger FAILED_COUNTS = new AtomicInteger();
|
||||||
|
|
||||||
|
// private-ctor
|
||||||
private NetworkContextHolder() {throw new UnsupportedOperationException();}
|
private NetworkContextHolder() {throw new UnsupportedOperationException();}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 尝试设置IP地址
|
||||||
|
*
|
||||||
|
* @param i 新的ip地址, 为空时设置会失败
|
||||||
|
*/
|
||||||
public static void setIpAddress(String i) {
|
public static void setIpAddress(String i) {
|
||||||
if (i == null
|
if (i == null
|
||||||
|| i.isEmpty()) {
|
|| i.isEmpty()) {
|
||||||
log.error("IP 地址不能为空!");
|
log.error("IP 地址不能为空!");
|
||||||
|
FAILED_COUNTS.incrementAndGet();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
IP_LOCK.lock();
|
IP_LOCK.lock();
|
||||||
IP_ADDRESS = i;
|
IP_ADDRESS = i;
|
||||||
@@ -41,11 +54,23 @@ public final class NetworkContextHolder {
|
|||||||
IP_CONTEXT_WAIT_LATCH.countDown();
|
IP_CONTEXT_WAIT_LATCH.countDown();
|
||||||
}
|
}
|
||||||
} finally {
|
} finally {
|
||||||
|
FAILED_COUNTS.set(0);
|
||||||
IP_LOCK.unlock();
|
IP_LOCK.unlock();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取所缓存的ip的地址
|
||||||
|
* <p>
|
||||||
|
* 设置失败次数过多时将忽略已保存的缓存值,防止多次将旧IP重复更新.
|
||||||
|
*
|
||||||
|
* @return ip地址
|
||||||
|
*/
|
||||||
public static String getIpAddress() {
|
public static String getIpAddress() {
|
||||||
|
if (FAILED_COUNTS.get() > 10) {
|
||||||
|
log.warn("更新失败次数过多, 不在返回IP地址直到下次成功更新!");
|
||||||
|
return null;
|
||||||
|
}
|
||||||
if (IP_ADDRESS != null) {
|
if (IP_ADDRESS != null) {
|
||||||
return IP_ADDRESS;
|
return IP_ADDRESS;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
package com.serliunx.ddns.support;
|
package com.serliunx.ddns.support;
|
||||||
|
|
||||||
import com.serliunx.ddns.config.Configuration;
|
import com.serliunx.ddns.config.Configuration;
|
||||||
import com.serliunx.ddns.config.ConfigurationKeys;
|
import com.serliunx.ddns.constant.ConfigurationKeys;
|
||||||
import com.serliunx.ddns.constant.IpProviderType;
|
import com.serliunx.ddns.constant.IpProviderType;
|
||||||
import com.serliunx.ddns.constant.SystemConstants;
|
import com.serliunx.ddns.constant.SystemConstants;
|
||||||
import com.serliunx.ddns.core.Clearable;
|
import com.serliunx.ddns.core.Clearable;
|
||||||
@@ -27,8 +27,8 @@ 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;
|
||||||
|
|
||||||
import static com.serliunx.ddns.config.ConfigurationKeys.KEY_TASK_REFRESH_INTERVAL_IP;
|
import static com.serliunx.ddns.constant.ConfigurationKeys.KEY_TASK_REFRESH_INTERVAL_IP;
|
||||||
import static com.serliunx.ddns.config.ConfigurationKeys.KEY_THREAD_POOL_CORE_SIZE;
|
import static com.serliunx.ddns.constant.ConfigurationKeys.KEY_THREAD_POOL_CORE_SIZE;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 系统初始化
|
* 系统初始化
|
||||||
@@ -115,11 +115,17 @@ public final class SystemInitializer implements Refreshable, Clearable {
|
|||||||
return instances;
|
return instances;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 加载实例(不同的容器加载时机不同)
|
||||||
|
*/
|
||||||
private void loadInstances() {
|
private void loadInstances() {
|
||||||
instances = instanceContext.getInstances();
|
instances = instanceContext.getInstances();
|
||||||
log.info("载入 {} 个实例.", instances.size());
|
log.info("载入 {} 个实例.", instances.size());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 资源释放
|
||||||
|
*/
|
||||||
@SuppressWarnings("SameParameterValue")
|
@SuppressWarnings("SameParameterValue")
|
||||||
private void releaseResource(String resourceName) {
|
private void releaseResource(String resourceName) {
|
||||||
ClassLoader classLoader = SystemConstants.class.getClassLoader();
|
ClassLoader classLoader = SystemConstants.class.getClassLoader();
|
||||||
@@ -146,6 +152,9 @@ public final class SystemInitializer implements Refreshable, Clearable {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 运行实例
|
||||||
|
*/
|
||||||
private void runInstances() {
|
private void runInstances() {
|
||||||
Assert.notNull(scheduledThreadPoolExecutor);
|
Assert.notNull(scheduledThreadPoolExecutor);
|
||||||
Assert.notNull(instances);
|
Assert.notNull(instances);
|
||||||
@@ -164,6 +173,11 @@ public final class SystemInitializer implements Refreshable, Clearable {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 初始化线程池
|
||||||
|
*
|
||||||
|
* @param coreSize 线程池核心线程数量
|
||||||
|
*/
|
||||||
private void initThreadPool(int coreSize) {
|
private void initThreadPool(int coreSize) {
|
||||||
Assert.isLargerThan(coreSize, 1);
|
Assert.isLargerThan(coreSize, 1);
|
||||||
scheduledThreadPoolExecutor = new ScheduledThreadPoolExecutor(coreSize, ThreadFactoryBuilder.builder()
|
scheduledThreadPoolExecutor = new ScheduledThreadPoolExecutor(coreSize, ThreadFactoryBuilder.builder()
|
||||||
@@ -182,6 +196,9 @@ public final class SystemInitializer implements Refreshable, Clearable {
|
|||||||
}, "DDNS-ShutDownHook"));
|
}, "DDNS-ShutDownHook"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 初始化定时获取IP地址的任务
|
||||||
|
*/
|
||||||
private void initIpTask() {
|
private void initIpTask() {
|
||||||
scheduledProvider = new ScheduledProvider(getInternalProvider(),
|
scheduledProvider = new ScheduledProvider(getInternalProvider(),
|
||||||
configuration.getLong(KEY_TASK_REFRESH_INTERVAL_IP, 300L));
|
configuration.getLong(KEY_TASK_REFRESH_INTERVAL_IP, 300L));
|
||||||
@@ -192,11 +209,19 @@ public final class SystemInitializer implements Refreshable, Clearable {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取内置的IP供应器(获取IP地址的方式)
|
||||||
|
* <p>
|
||||||
|
* 根据配置文件中的定义{@link ConfigurationKeys#KEY_IP_PROVIDER_TYPE}, 默认为{@link com.serliunx.ddns.support.ipprovider.IpApiProvider}
|
||||||
|
*/
|
||||||
private Provider getInternalProvider() {
|
private Provider getInternalProvider() {
|
||||||
return configuration.getEnum(IpProviderType.class, ConfigurationKeys.KEY_IP_PROVIDER_TYPE,
|
return configuration.getEnum(IpProviderType.class, ConfigurationKeys.KEY_IP_PROVIDER_TYPE,
|
||||||
IpProviderType.IP_API).getProvider();
|
IpProviderType.IP_API).getProvider();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 关闭线程池逻辑
|
||||||
|
*/
|
||||||
private void checkAndCloseSafely() {
|
private void checkAndCloseSafely() {
|
||||||
if (scheduledThreadPoolExecutor == null)
|
if (scheduledThreadPoolExecutor == null)
|
||||||
return;
|
return;
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
package com.serliunx.ddns.support.okhttp;
|
package com.serliunx.ddns.support.okhttp;
|
||||||
|
|
||||||
import com.serliunx.ddns.config.Configuration;
|
import com.serliunx.ddns.config.Configuration;
|
||||||
import com.serliunx.ddns.config.ConfigurationKeys;
|
import com.serliunx.ddns.constant.ConfigurationKeys;
|
||||||
import okhttp3.OkHttpClient;
|
import okhttp3.OkHttpClient;
|
||||||
import okhttp3.Request;
|
import okhttp3.Request;
|
||||||
import okhttp3.Response;
|
import okhttp3.Response;
|
||||||
|
|||||||
Reference in New Issue
Block a user