parent
d5b0eaec34
commit
77c18d3ea1
@ -0,0 +1,21 @@
|
||||
package org.opsli.core.cache.pushsub.enums;
|
||||
|
||||
/**
|
||||
* @BelongsProject: opsli-boot
|
||||
* @BelongsPackage: org.opsli.core.cache.pushsub.enums
|
||||
* @Author: Parker
|
||||
* @CreateTime: 2020-09-15 16:15
|
||||
* @Description: 消息具体类型
|
||||
*/
|
||||
public enum MsgArgsType {
|
||||
|
||||
/** 字典Key */
|
||||
DICT_KEY,
|
||||
/** 字典Field */
|
||||
DICT_FIELD,
|
||||
/** 字典Value */
|
||||
DICT_VALUE,
|
||||
|
||||
;
|
||||
|
||||
}
|
@ -0,0 +1,23 @@
|
||||
package org.opsli.core.cache.pushsub.enums;
|
||||
|
||||
/**
|
||||
* @BelongsProject: opsli-boot
|
||||
* @BelongsPackage: org.opsli.core.cache.pushsub.enums
|
||||
* @Author: Parker
|
||||
* @CreateTime: 2020-09-15 16:15
|
||||
* @Description: 发布订阅类型
|
||||
*/
|
||||
public enum PushSubType {
|
||||
|
||||
/** 字典类型 */
|
||||
DICT,
|
||||
|
||||
;
|
||||
|
||||
|
||||
|
||||
PushSubType(){
|
||||
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,25 @@
|
||||
package org.opsli.core.cache.pushsub.handler;
|
||||
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import org.opsli.core.cache.pushsub.enums.PushSubType;
|
||||
|
||||
/**
|
||||
* @BelongsProject: opsli-boot
|
||||
* @BelongsPackage: org.opsli.core.cache.pushsub.handler
|
||||
* @Author: Parker
|
||||
* @CreateTime: 2020-09-15 16:24
|
||||
* @Description: 字典消息处理
|
||||
*/
|
||||
public class DictHandler implements RedisPushSubHandler{
|
||||
|
||||
@Override
|
||||
public PushSubType getType() {
|
||||
return PushSubType.DICT;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handler(JSONObject msgJson) {
|
||||
System.out.println(msgJson.toJSONString());
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,24 @@
|
||||
package org.opsli.core.cache.pushsub.handler;
|
||||
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import org.opsli.core.cache.pushsub.enums.PushSubType;
|
||||
|
||||
/**
|
||||
* @BelongsProject: opsli-boot
|
||||
* @BelongsPackage: org.opsli.core.cache.pushsub.receiver
|
||||
* @Author: Parker
|
||||
* @CreateTime: 2020-09-15 15:11
|
||||
* @Description: 标示类 用于获得 消息未知
|
||||
*/
|
||||
public interface RedisPushSubHandler {
|
||||
|
||||
|
||||
PushSubType getType();
|
||||
|
||||
/**
|
||||
* 消息处理
|
||||
* @param msgJson
|
||||
*/
|
||||
void handler(JSONObject msgJson);
|
||||
|
||||
}
|
@ -0,0 +1,44 @@
|
||||
package org.opsli.core.cache.pushsub.msgs;
|
||||
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import lombok.Data;
|
||||
import lombok.experimental.Accessors;
|
||||
import org.opsli.core.cache.pushsub.enums.MsgArgsType;
|
||||
import org.opsli.core.cache.pushsub.enums.PushSubType;
|
||||
import org.opsli.core.cache.pushsub.receiver.RedisPushSubReceiver;
|
||||
import org.opsli.plugins.redis.pushsub.entity.BaseSubMessage;
|
||||
|
||||
/**
|
||||
* @BelongsProject: opsli-boot
|
||||
* @BelongsPackage: org.opsli.core.cache.pushsub.msgs
|
||||
* @Author: Parker
|
||||
* @CreateTime: 2020-09-15 16:50
|
||||
* @Description: 字典消息
|
||||
*/
|
||||
|
||||
@Data
|
||||
@Accessors(chain = true)
|
||||
public final class DictMsgFactory extends BaseSubMessage{
|
||||
|
||||
/** 通道 */
|
||||
private static final String CHANNEL = RedisPushSubReceiver.BASE_CHANNEL+RedisPushSubReceiver.CHANNEL;
|
||||
|
||||
private DictMsgFactory(){}
|
||||
|
||||
/**
|
||||
* 构建消息
|
||||
*/
|
||||
public static BaseSubMessage createMsg(String key, String field, Object value){
|
||||
BaseSubMessage baseSubMessage = new BaseSubMessage();
|
||||
// 数据
|
||||
JSONObject jsonObj = new JSONObject();
|
||||
jsonObj.put(MsgArgsType.DICT_KEY.toString(),key);
|
||||
jsonObj.put(MsgArgsType.DICT_FIELD.toString(),field);
|
||||
jsonObj.put(MsgArgsType.DICT_VALUE.toString(),value);
|
||||
|
||||
// DICT 字典
|
||||
baseSubMessage.build(CHANNEL,PushSubType.DICT.toString(),jsonObj);
|
||||
return baseSubMessage;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,84 @@
|
||||
package org.opsli.core.cache.pushsub.receiver;
|
||||
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.opsli.common.utils.PackageUtil;
|
||||
import org.opsli.core.cache.pushsub.enums.PushSubType;
|
||||
import org.opsli.core.cache.pushsub.handler.RedisPushSubHandler;
|
||||
import org.opsli.core.msg.CoreMsg;
|
||||
import org.opsli.plugins.redis.pushsub.entity.BaseSubMessage;
|
||||
import org.opsli.plugins.redis.pushsub.receiver.BaseReceiver;
|
||||
|
||||
import java.lang.reflect.Modifier;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* @BelongsProject: opsli-boot
|
||||
* @BelongsPackage: org.opsli.plugins.redis.entity
|
||||
* @Author: Parker
|
||||
* @CreateTime: 2020-09-15 14:50
|
||||
* @Description: Redis 消息订阅 更新本地缓存
|
||||
*
|
||||
* 字典缓存更新
|
||||
*
|
||||
*/
|
||||
@Slf4j
|
||||
public class RedisPushSubReceiver extends BaseReceiver {
|
||||
|
||||
/** 监听信道 */
|
||||
public static final String CHANNEL = "opsli";
|
||||
|
||||
/** 处理方法集合 */
|
||||
private static final Map<PushSubType, RedisPushSubHandler> HANDLER_MAP = new HashMap<>();
|
||||
|
||||
static {
|
||||
// 拿到state包下 实现了 SystemEventState 接口的,所有子类
|
||||
Set<Class<?>> clazzSet = PackageUtil.listSubClazz(RedisPushSubHandler.class.getPackage().getName(),
|
||||
true,
|
||||
RedisPushSubHandler.class
|
||||
);
|
||||
|
||||
for (Class<?> aClass : clazzSet) {
|
||||
// 位运算 去除抽象类
|
||||
if((aClass.getModifiers() & Modifier.ABSTRACT) != 0){
|
||||
continue;
|
||||
}
|
||||
|
||||
try {
|
||||
RedisPushSubHandler handler = (RedisPushSubHandler) aClass.newInstance();
|
||||
|
||||
// 加入集合
|
||||
HANDLER_MAP.put(handler.getType(),handler);
|
||||
|
||||
} catch (Exception e){
|
||||
log.error(CoreMsg.REDIS_EXCEPTION_PUSH_SUB.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public RedisPushSubReceiver() {
|
||||
super(CHANNEL);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void receiveMessage(String msg) {
|
||||
if(msg == null || "".equals(msg)){
|
||||
return;
|
||||
}
|
||||
// 替换 转意符
|
||||
String replaceAll = msg.replaceAll("\\\\", "");
|
||||
String substring = replaceAll.substring(1, replaceAll.length() - 1);
|
||||
JSONObject msgJson = JSONObject.parseObject(substring);
|
||||
String type = (String) msgJson.get(BaseSubMessage.BASE_TYPE);
|
||||
PushSubType pt = PushSubType.valueOf(type);
|
||||
RedisPushSubHandler redisPushSubHandler = HANDLER_MAP.get(pt);
|
||||
if(redisPushSubHandler == null){
|
||||
return;
|
||||
}
|
||||
redisPushSubHandler.handler(msgJson);
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in new issue