com.alibaba.tmq.common.util.DiamondUtil Maven / Gradle / Ivy
package com.alibaba.tmq.common.util;
import com.taobao.diamond.client.Diamond;
import com.taobao.diamond.manager.ManagerListenerAdapter;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import java.io.IOException;
import java.util.*;
/**
* diamond
*
* @author 陆昆
* @create 2018-07-26 下午10:29
**/
public class DiamondUtil {
private static final Log logger = LogFactory.getLog(DiamondUtil.class);
public static Properties properties = new Properties();
private static final String DEFAULT_GROUP = "DEFAULT_GROUP";
public static void addListener(final String key) {
try {
String value = Diamond.getConfig(key, DEFAULT_GROUP, 400);
properties.setProperty(key, value);
logger.info("init key: " + key + " val: " + value);
Diamond.addListener(key, DEFAULT_GROUP,
new ManagerListenerAdapter() {
public void receiveConfigInfo(String configInfo) {
logger.warn("diamond config update, dataId=" + key + ", newValue=" + configInfo);
properties.setProperty(key, configInfo);
}
});
} catch (IOException e) {
logger.error("[DiamondUtil]addListener error", e);
}
}
public static boolean getBooleanVal(String key) {
boolean result = false;
try {
result = Boolean.parseBoolean(properties.getProperty(key, "false"));
} catch (Exception e) {
logger.error("[DiamondUtil]getBooleanVal error, key: " + key, e);
}
return result;
}
public static String getStringVal(String key) {
String result = null;
try {
result = properties.getProperty(key);
} catch (Exception e) {
logger.error("[DiamondUtil]getBooleanVal error, key: " + key, e);
}
return result;
}
}