|
|
|
package com.zhonglai.luhui.central.control.comm;
|
|
|
|
|
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
|
|
|
import java.lang.reflect.Field;
|
|
|
|
import java.util.Optional;
|
|
|
|
|
|
|
|
public class Topic {
|
|
|
|
private static final Logger log = LoggerFactory.getLogger(Topic.class);
|
|
|
|
|
|
|
|
private String roleid;
|
|
|
|
private String username;
|
|
|
|
private String clientid;
|
|
|
|
private String topicType;
|
|
|
|
private String messageid;
|
|
|
|
private String payloadtype;
|
|
|
|
|
|
|
|
public Topic() {
|
|
|
|
}
|
|
|
|
|
|
|
|
public Topic(String roleid, String username, String clientid, String topicType, String payloadtype) {
|
|
|
|
this.roleid = roleid;
|
|
|
|
this.username = username;
|
|
|
|
this.clientid = clientid;
|
|
|
|
this.topicType = topicType;
|
|
|
|
this.payloadtype = payloadtype;
|
|
|
|
}
|
|
|
|
|
|
|
|
public Topic(String roleid, String username, String clientid, String topicType, String messageid, String payloadtype) {
|
|
|
|
this.roleid = roleid;
|
|
|
|
this.username = username;
|
|
|
|
this.clientid = clientid;
|
|
|
|
this.topicType = topicType;
|
|
|
|
this.messageid = messageid;
|
|
|
|
this.payloadtype = payloadtype;
|
|
|
|
}
|
|
|
|
|
|
|
|
public Topic(String topic)
|
|
|
|
{
|
|
|
|
topic = Optional.ofNullable(topic).orElseThrow(()->new MyException("topic为空"));
|
|
|
|
String[] sts = topic.split("/");
|
|
|
|
String[] config = SysParameter.topicconfig.split("/");
|
|
|
|
int number = sts.length;
|
|
|
|
if(number>config.length)
|
|
|
|
{
|
|
|
|
number = config.length;
|
|
|
|
}
|
|
|
|
for(int i=1;i<number;i++)
|
|
|
|
{
|
|
|
|
String cf = config[i].replace("{{","").replace("}}","");
|
|
|
|
try {
|
|
|
|
Field field = this.getClass().getDeclaredField(cf);
|
|
|
|
field.set(this,sts[i]);
|
|
|
|
} catch (NoSuchFieldException e) {
|
|
|
|
log.info("{}生成topic时没有属性{}",topic,cf);
|
|
|
|
} catch (IllegalAccessException e) {
|
|
|
|
log.info("{}生成topic时无法给{}赋值{}",topic,cf,sts[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if("ONLINE".equals(topicType.toUpperCase()))
|
|
|
|
{
|
|
|
|
this.payloadtype = "String";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 生成缓存关键字
|
|
|
|
* @return
|
|
|
|
*/
|
|
|
|
public String generateRedicKey()
|
|
|
|
{
|
|
|
|
return generate(":");
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 生成发送消息的topic
|
|
|
|
* @return
|
|
|
|
*/
|
|
|
|
public String generateSendMessageTopic()
|
|
|
|
{
|
|
|
|
return "/"+generate("/");
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 生成客户端关键字
|
|
|
|
* @return
|
|
|
|
*/
|
|
|
|
public String generateClienKey()
|
|
|
|
{
|
|
|
|
return "/"+generate("/");
|
|
|
|
}
|
|
|
|
|
|
|
|
private String generate(String division)
|
|
|
|
{
|
|
|
|
String str = SysParameter.topicconfig;
|
|
|
|
if(StringUtils.isEmpty(roleid))
|
|
|
|
{
|
|
|
|
roleid = "2";
|
|
|
|
}
|
|
|
|
str = str.replace("/{{roleid}}",roleid+division);
|
|
|
|
|
|
|
|
if(StringUtils.isEmpty(username))
|
|
|
|
{
|
|
|
|
username = "+";
|
|
|
|
}
|
|
|
|
str = str.replace("/{{username}}",username+division);
|
|
|
|
|
|
|
|
if(StringUtils.isEmpty(clientid))
|
|
|
|
{
|
|
|
|
clientid = "+";
|
|
|
|
}
|
|
|
|
str = str.replace("/{{clientid}}",clientid+division);
|
|
|
|
|
|
|
|
if(StringUtils.isEmpty(payloadtype))
|
|
|
|
{
|
|
|
|
payloadtype = "String";
|
|
|
|
}
|
|
|
|
str = str.replace("/{{payloadtype}}",payloadtype+division);
|
|
|
|
|
|
|
|
if(StringUtils.isEmpty(topicType))
|
|
|
|
{
|
|
|
|
topicType = "PUT";
|
|
|
|
}
|
|
|
|
str = str.replace("/{{topicType}}",topicType+division);
|
|
|
|
|
|
|
|
if(StringUtils.isNotEmpty(messageid))
|
|
|
|
{
|
|
|
|
str = str.replace("/{{messageid}}",messageid);
|
|
|
|
}
|
|
|
|
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
|
|
|
public String getRoleid() {
|
|
|
|
return roleid;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setRoleid(String roleid) {
|
|
|
|
this.roleid = roleid;
|
|
|
|
}
|
|
|
|
|
|
|
|
public String getUsername() {
|
|
|
|
return username;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setUsername(String username) {
|
|
|
|
this.username = username;
|
|
|
|
}
|
|
|
|
|
|
|
|
public String getClientid() {
|
|
|
|
return clientid;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setClientid(String clientid) {
|
|
|
|
this.clientid = clientid;
|
|
|
|
}
|
|
|
|
|
|
|
|
public String getTopicType() {
|
|
|
|
return topicType;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setTopicType(String topicType) {
|
|
|
|
this.topicType = topicType;
|
|
|
|
}
|
|
|
|
|
|
|
|
public String getMessageid() {
|
|
|
|
return messageid;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setMessageid(String messageid) {
|
|
|
|
this.messageid = messageid;
|
|
|
|
}
|
|
|
|
|
|
|
|
public String getPayloadtype() {
|
|
|
|
return payloadtype;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setPayloadtype(String payloadtype) {
|
|
|
|
this.payloadtype = payloadtype;
|
|
|
|
}
|
|
|
|
} |