|
|
|
package com.fanruan.api.conf;
|
|
|
|
|
|
|
|
import com.fanruan.api.generic.Matcher;
|
|
|
|
import com.fanruan.api.generic.Runner;
|
|
|
|
import com.fr.config.Configuration;
|
|
|
|
import com.fr.config.holder.ConfigChangeListener;
|
|
|
|
import com.fr.decision.webservice.utils.WebServiceUtils;
|
|
|
|
import com.fr.transaction.Configurations;
|
|
|
|
import com.fr.transaction.ValidateProxy;
|
|
|
|
import com.fr.transaction.WorkerCallBack;
|
|
|
|
import com.fr.transaction.WorkerFacade;
|
|
|
|
|
|
|
|
import java.util.Map;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @author richie
|
|
|
|
* @version 10.0
|
|
|
|
* Created by richie on 2019-09-17
|
|
|
|
* 配置管理工具类
|
|
|
|
*/
|
|
|
|
public class ConfigurationKit {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 获取配置项的属性,组成map
|
|
|
|
*
|
|
|
|
* @param config 配置项
|
|
|
|
* @return map
|
|
|
|
*/
|
|
|
|
public static Map<String, Object> getConfig(Configuration config) {
|
|
|
|
return WebServiceUtils.getConfigValue(config);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 更新配置项信息
|
|
|
|
*
|
|
|
|
* @param config 配置项
|
|
|
|
* @param map 更新内容
|
|
|
|
*/
|
|
|
|
public static void setConfig(Configuration config, Map<String, Object> map) {
|
|
|
|
WebServiceUtils.setConfigValue(config, map);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 兼容配置缓存失效
|
|
|
|
*
|
|
|
|
* @param matcher 匹配器
|
|
|
|
* @param runner 匹配后执行的动作
|
|
|
|
*/
|
|
|
|
public static void listenCacheChange(final Matcher<Class<? extends Configuration>> matcher, final Runner runner) {
|
|
|
|
ValidateProxy.getInstance().getValidateManager().registerListener(new ConfigChangeListener() {
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean accept(Class<? extends Configuration> clazz) {
|
|
|
|
if (matcher == null) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return matcher.match(clazz);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void change() {
|
|
|
|
if (matcher != null) {
|
|
|
|
runner.run();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 在一个事务中修改配置
|
|
|
|
*
|
|
|
|
* @param configType 配置类
|
|
|
|
* @param runner 执行器
|
|
|
|
*/
|
|
|
|
public static void modify(Class<? extends Configuration> configType, final Runner runner) {
|
|
|
|
Configurations.modify(new WorkerFacade(configType) {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
if (runner != null) {
|
|
|
|
runner.run();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 在一个事务中修改配置
|
|
|
|
* 支持 让 configType, configTypes 缓存失效
|
|
|
|
*
|
|
|
|
* @param configType 配置类
|
|
|
|
* @param runner 执行器
|
|
|
|
* @param configTypes 配置类(所有的配置类都会缓存失效)
|
|
|
|
*/
|
|
|
|
public static void modify(final Runner runner, Class<? extends Configuration> configType, Class<? extends Configuration>... configTypes) {
|
|
|
|
Configurations.modify(new WorkerFacade(configType, configTypes) {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
if (runner != null) {
|
|
|
|
runner.run();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 在一个事务中修改配置
|
|
|
|
*
|
|
|
|
* @param configType 配置类
|
|
|
|
* @param runner 执行器
|
|
|
|
* @param callBack 事务回调
|
|
|
|
*/
|
|
|
|
public static void modify(Class<? extends Configuration> configType, final Runner runner, WorkerCallBack callBack) {
|
|
|
|
WorkerFacade facade = new WorkerFacade(configType) {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
if (runner != null) {
|
|
|
|
runner.run();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
if (callBack != null) {
|
|
|
|
facade.addCallBack(callBack);
|
|
|
|
}
|
|
|
|
Configurations.modify(facade);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 在一个事务中修改配置
|
|
|
|
* 支持 让 configType, configTypes 失效
|
|
|
|
*
|
|
|
|
* @param configType 配置类
|
|
|
|
* @param runner 执行器
|
|
|
|
* @param callBack 事务回调
|
|
|
|
* @param configTypes 配置类(所有的配置类都会缓存失效)
|
|
|
|
*/
|
|
|
|
public static void modify(final Runner runner, WorkerCallBack callBack, Class<? extends Configuration> configType, Class<? extends Configuration>... configTypes) {
|
|
|
|
WorkerFacade facade = new WorkerFacade(configType, configTypes) {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
if (runner != null) {
|
|
|
|
runner.run();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
if (callBack != null) {
|
|
|
|
facade.addCallBack(callBack);
|
|
|
|
}
|
|
|
|
Configurations.modify(facade);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|