You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
131 lines
4.1 KiB
131 lines
4.1 KiB
/**
|
|
* 动态配置管理系统
|
|
* 支持配置的热更新,无需重启应用即可生效
|
|
*/
|
|
import java.util.*;
|
|
import java.util.concurrent.ConcurrentHashMap;
|
|
import java.util.concurrent.CopyOnWriteArrayList;
|
|
|
|
public class DynamicConfigManager {
|
|
// 配置存储
|
|
private final Map<String, Object> configs;
|
|
// 配置监听器列表
|
|
private final List<ConfigChangeListener> listeners;
|
|
// 单例实例
|
|
private static volatile DynamicConfigManager instance;
|
|
|
|
// 配置变更监听器接口
|
|
public interface ConfigChangeListener {
|
|
void onConfigChange(String key, Object oldValue, Object newValue);
|
|
}
|
|
|
|
private DynamicConfigManager() {
|
|
configs = new ConcurrentHashMap<>();
|
|
listeners = new CopyOnWriteArrayList<>();
|
|
System.out.println("动态配置管理系统初始化完成");
|
|
}
|
|
|
|
// 获取单例实例
|
|
public static DynamicConfigManager getInstance() {
|
|
if (instance == null) {
|
|
synchronized (DynamicConfigManager.class) {
|
|
if (instance == null) {
|
|
instance = new DynamicConfigManager();
|
|
}
|
|
}
|
|
}
|
|
return instance;
|
|
}
|
|
|
|
// 获取配置
|
|
public <T> T getConfig(String key, T defaultValue) {
|
|
Object value = configs.get(key);
|
|
if (value != null) {
|
|
try {
|
|
return (T) value;
|
|
} catch (ClassCastException e) {
|
|
System.err.println("配置类型转换错误: " + key + ", 返回默认值");
|
|
}
|
|
}
|
|
return defaultValue;
|
|
}
|
|
|
|
// 设置配置(支持热更新)
|
|
public synchronized void setConfig(String key, Object value) {
|
|
Object oldValue = configs.put(key, value);
|
|
System.out.println("配置更新: " + key + ", 旧值: " + oldValue + ", 新值: " + value);
|
|
|
|
// 通知所有监听器
|
|
notifyListeners(key, oldValue, value);
|
|
}
|
|
|
|
// 批量设置配置
|
|
public synchronized void setConfigs(Map<String, Object> newConfigs) {
|
|
for (Map.Entry<String, Object> entry : newConfigs.entrySet()) {
|
|
setConfig(entry.getKey(), entry.getValue());
|
|
}
|
|
}
|
|
|
|
// 移除配置
|
|
public synchronized void removeConfig(String key) {
|
|
Object oldValue = configs.remove(key);
|
|
if (oldValue != null) {
|
|
System.out.println("配置移除: " + key + ", 移除值: " + oldValue);
|
|
notifyListeners(key, oldValue, null);
|
|
}
|
|
}
|
|
|
|
// 获取所有配置键
|
|
public Set<String> getAllKeys() {
|
|
return new HashSet<>(configs.keySet());
|
|
}
|
|
|
|
// 注册配置变更监听器
|
|
public void addListener(ConfigChangeListener listener) {
|
|
if (listener != null && !listeners.contains(listener)) {
|
|
listeners.add(listener);
|
|
System.out.println("配置监听器已注册");
|
|
}
|
|
}
|
|
|
|
// 移除配置变更监听器
|
|
public void removeListener(ConfigChangeListener listener) {
|
|
if (listener != null) {
|
|
listeners.remove(listener);
|
|
System.out.println("配置监听器已移除");
|
|
}
|
|
}
|
|
|
|
// 通知所有监听器配置变更
|
|
private void notifyListeners(String key, Object oldValue, Object newValue) {
|
|
for (ConfigChangeListener listener : listeners) {
|
|
try {
|
|
listener.onConfigChange(key, oldValue, newValue);
|
|
} catch (Exception e) {
|
|
System.err.println("通知配置变更时出错: " + e.getMessage());
|
|
}
|
|
}
|
|
}
|
|
|
|
// 清空所有配置
|
|
public synchronized void clear() {
|
|
Set<String> keys = new HashSet<>(configs.keySet());
|
|
configs.clear();
|
|
System.out.println("所有配置已清空");
|
|
|
|
// 通知所有键被移除
|
|
for (String key : keys) {
|
|
notifyListeners(key, null, null);
|
|
}
|
|
}
|
|
|
|
// 获取配置数量
|
|
public int size() {
|
|
return configs.size();
|
|
}
|
|
|
|
// 检查配置是否存在
|
|
public boolean containsKey(String key) {
|
|
return configs.containsKey(key);
|
|
}
|
|
} |