package com.aliyun.openservices.ons.api.impl.rocketmq;
import java.util.Properties;
import com.alibaba.rocketmq.common.MixAll;
import com.alibaba.rocketmq.common.UtilAll;
import com.alibaba.rocketmq.common.namesrv.TopAddressing;
import com.aliyun.openservices.ons.api.PropertyKeyConst;
import com.aliyun.openservices.ons.api.exception.ONSClientException;
public abstract class ONSClientAbstract {
protected final Properties properties;
protected final SessionCredentials sessionCredentials = new SessionCredentials();
protected static final String WS_ADDR = System.getProperty("com.aliyun.openservices.ons.addr",
"http://onsaddr.aliyun.com:8080/rocketmq/nsaddr");
protected String nameServerAddr = System.getProperty(MixAll.NAMESRV_ADDR_PROPERTY,
System.getenv(MixAll.NAMESRV_ADDR_ENV));
public ONSClientAbstract(Properties properties) {
this.properties = properties;
this.sessionCredentials.updateContent(properties);
// 检测必须的参数
if (null == this.sessionCredentials.getAccessKey()
|| "".equals(this.sessionCredentials.getAccessKey())) {
throw new ONSClientException("please set access key");
}
if (null == this.sessionCredentials.getSecretKey()
|| "".equals(this.sessionCredentials.getSecretKey())) {
throw new ONSClientException("please set secret key");
}
/**
* 优先级 1、Name Server设置优先级最高 2、其次是地址服务器
*/
if (null == this.nameServerAddr) {
String addr = this.fetchNameServerAddr();
if (null != addr) {
this.nameServerAddr = addr;
}
}
if (null == this.nameServerAddr) {
throw new ONSClientException("Can not find name server");
}
}
protected String buildIntanceName() {
return Integer.toString(UtilAll.getPid())//
+ "#" + this.nameServerAddr.hashCode() //
+ "#" + this.sessionCredentials.getAccessKey().hashCode();
}
protected String fetchNameServerAddr() {
String wsAddr = WS_ADDR;
String property = this.properties.getProperty(PropertyKeyConst.ONSAddr);
if (property != null) {
wsAddr = property;
}
TopAddressing top = new TopAddressing(wsAddr);
return top.fetchNSAddr();
}
public String getNameServerAddr() {
return nameServerAddr;
}
}