优化 mq starter 组件,增加 RedisMQTemplate 模板类

pull/2/head
YunaiV 2021-12-05 23:00:41 +08:00
parent ade55d89a4
commit d29b0beb9b
26 changed files with 137 additions and 75 deletions

View File

@ -1,13 +1,13 @@
package cn.iocoder.yudao.adminserver.modules.infra.mq.message.config; package cn.iocoder.yudao.adminserver.modules.infra.mq.message.config;
import cn.iocoder.yudao.framework.mq.core.pubsub.ChannelMessage; import cn.iocoder.yudao.framework.mq.core.pubsub.AbstractChannelMessage;
import lombok.Data; import lombok.Data;
/** /**
* Message * Message
*/ */
@Data @Data
public class InfConfigRefreshMessage implements ChannelMessage { public class InfConfigRefreshMessage extends AbstractChannelMessage {
@Override @Override
public String getChannel() { public String getChannel() {

View File

@ -1,8 +1,7 @@
package cn.iocoder.yudao.adminserver.modules.infra.mq.producer.config; package cn.iocoder.yudao.adminserver.modules.infra.mq.producer.config;
import cn.iocoder.yudao.adminserver.modules.infra.mq.message.config.InfConfigRefreshMessage; import cn.iocoder.yudao.adminserver.modules.infra.mq.message.config.InfConfigRefreshMessage;
import cn.iocoder.yudao.framework.mq.core.util.RedisMessageUtils; import cn.iocoder.yudao.framework.mq.core.RedisMQTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import javax.annotation.Resource; import javax.annotation.Resource;
@ -14,14 +13,14 @@ import javax.annotation.Resource;
public class InfConfigProducer { public class InfConfigProducer {
@Resource @Resource
private StringRedisTemplate stringRedisTemplate; private RedisMQTemplate redisMQTemplate;
/** /**
* {@link InfConfigRefreshMessage} * {@link InfConfigRefreshMessage}
*/ */
public void sendConfigRefreshMessage() { public void sendConfigRefreshMessage() {
InfConfigRefreshMessage message = new InfConfigRefreshMessage(); InfConfigRefreshMessage message = new InfConfigRefreshMessage();
RedisMessageUtils.sendChannelMessage(stringRedisTemplate, message); redisMQTemplate.send(message);
} }
} }

View File

@ -1,13 +1,17 @@
package cn.iocoder.yudao.adminserver.modules.system.mq.message.dept; package cn.iocoder.yudao.adminserver.modules.system.mq.message.dept;
import cn.iocoder.yudao.framework.mq.core.pubsub.ChannelMessage; import cn.iocoder.yudao.framework.mq.core.pubsub.AbstractChannelMessage;
import lombok.Data; import lombok.Data;
import lombok.EqualsAndHashCode;
/** /**
* Message * Message
*
* @author
*/ */
@Data @Data
public class SysDeptRefreshMessage implements ChannelMessage { @EqualsAndHashCode(callSuper = true)
public class SysDeptRefreshMessage extends AbstractChannelMessage {
@Override @Override
public String getChannel() { public String getChannel() {

View File

@ -1,13 +1,15 @@
package cn.iocoder.yudao.adminserver.modules.system.mq.message.dict; package cn.iocoder.yudao.adminserver.modules.system.mq.message.dict;
import cn.iocoder.yudao.framework.mq.core.pubsub.ChannelMessage; import cn.iocoder.yudao.framework.mq.core.pubsub.AbstractChannelMessage;
import lombok.Data; import lombok.Data;
import lombok.EqualsAndHashCode;
/** /**
* Message * Message
*/ */
@Data @Data
public class SysDictDataRefreshMessage implements ChannelMessage { @EqualsAndHashCode(callSuper = true)
public class SysDictDataRefreshMessage extends AbstractChannelMessage {
@Override @Override
public String getChannel() { public String getChannel() {

View File

@ -1,7 +1,8 @@
package cn.iocoder.yudao.adminserver.modules.system.mq.message.mail; package cn.iocoder.yudao.adminserver.modules.system.mq.message.mail;
import cn.iocoder.yudao.framework.mq.core.stream.StreamMessage; import cn.iocoder.yudao.framework.mq.core.stream.AbstractStreamMessage;
import lombok.Data; import lombok.Data;
import lombok.EqualsAndHashCode;
import javax.validation.constraints.NotNull; import javax.validation.constraints.NotNull;
import java.util.Map; import java.util.Map;
@ -12,7 +13,8 @@ import java.util.Map;
* @author * @author
*/ */
@Data @Data
public class SysMailSendMessage implements StreamMessage { @EqualsAndHashCode(callSuper = true)
public class SysMailSendMessage extends AbstractStreamMessage {
/** /**
* *

View File

@ -1,13 +1,17 @@
package cn.iocoder.yudao.adminserver.modules.system.mq.message.permission; package cn.iocoder.yudao.adminserver.modules.system.mq.message.permission;
import cn.iocoder.yudao.framework.mq.core.pubsub.ChannelMessage; import cn.iocoder.yudao.framework.mq.core.pubsub.AbstractChannelMessage;
import lombok.Data; import lombok.Data;
import lombok.EqualsAndHashCode;
/** /**
* Message * Message
*
* @author
*/ */
@Data @Data
public class SysMenuRefreshMessage implements ChannelMessage { @EqualsAndHashCode(callSuper = true)
public class SysMenuRefreshMessage extends AbstractChannelMessage {
@Override @Override
public String getChannel() { public String getChannel() {

View File

@ -1,13 +1,17 @@
package cn.iocoder.yudao.adminserver.modules.system.mq.message.permission; package cn.iocoder.yudao.adminserver.modules.system.mq.message.permission;
import cn.iocoder.yudao.framework.mq.core.pubsub.ChannelMessage; import cn.iocoder.yudao.framework.mq.core.pubsub.AbstractChannelMessage;
import lombok.Data; import lombok.Data;
import lombok.EqualsAndHashCode;
/** /**
* Message * Message
*
* @author
*/ */
@Data @Data
public class SysRoleMenuRefreshMessage implements ChannelMessage { @EqualsAndHashCode(callSuper = true)
public class SysRoleMenuRefreshMessage extends AbstractChannelMessage {
@Override @Override
public String getChannel() { public String getChannel() {

View File

@ -1,13 +1,17 @@
package cn.iocoder.yudao.adminserver.modules.system.mq.message.permission; package cn.iocoder.yudao.adminserver.modules.system.mq.message.permission;
import cn.iocoder.yudao.framework.mq.core.pubsub.ChannelMessage; import cn.iocoder.yudao.framework.mq.core.pubsub.AbstractChannelMessage;
import lombok.Data; import lombok.Data;
import lombok.EqualsAndHashCode;
/** /**
* Message * Message
*
* @author
*/ */
@Data @Data
public class SysRoleRefreshMessage implements ChannelMessage { @EqualsAndHashCode(callSuper = true)
public class SysRoleRefreshMessage extends AbstractChannelMessage {
@Override @Override
public String getChannel() { public String getChannel() {

View File

@ -1,13 +1,17 @@
package cn.iocoder.yudao.adminserver.modules.system.mq.message.sms; package cn.iocoder.yudao.adminserver.modules.system.mq.message.sms;
import cn.iocoder.yudao.framework.mq.core.pubsub.ChannelMessage; import cn.iocoder.yudao.framework.mq.core.pubsub.AbstractChannelMessage;
import lombok.Data; import lombok.Data;
import lombok.EqualsAndHashCode;
/** /**
* Message * Message
*
* @author
*/ */
@Data @Data
public class SysSmsChannelRefreshMessage implements ChannelMessage { @EqualsAndHashCode(callSuper = true)
public class SysSmsChannelRefreshMessage extends AbstractChannelMessage {
@Override @Override
public String getChannel() { public String getChannel() {

View File

@ -1,13 +1,17 @@
package cn.iocoder.yudao.adminserver.modules.system.mq.message.sms; package cn.iocoder.yudao.adminserver.modules.system.mq.message.sms;
import cn.iocoder.yudao.framework.mq.core.pubsub.ChannelMessage; import cn.iocoder.yudao.framework.mq.core.pubsub.AbstractChannelMessage;
import lombok.Data; import lombok.Data;
import lombok.EqualsAndHashCode;
/** /**
* Message * Message
*
* @author
*/ */
@Data @Data
public class SysSmsTemplateRefreshMessage implements ChannelMessage { @EqualsAndHashCode(callSuper = true)
public class SysSmsTemplateRefreshMessage extends AbstractChannelMessage {
@Override @Override
public String getChannel() { public String getChannel() {

View File

@ -1,8 +1,7 @@
package cn.iocoder.yudao.adminserver.modules.system.mq.producer.dept; package cn.iocoder.yudao.adminserver.modules.system.mq.producer.dept;
import cn.iocoder.yudao.framework.mq.core.util.RedisMessageUtils;
import cn.iocoder.yudao.adminserver.modules.system.mq.message.dept.SysDeptRefreshMessage; import cn.iocoder.yudao.adminserver.modules.system.mq.message.dept.SysDeptRefreshMessage;
import org.springframework.data.redis.core.StringRedisTemplate; import cn.iocoder.yudao.framework.mq.core.RedisMQTemplate;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import javax.annotation.Resource; import javax.annotation.Resource;
@ -14,14 +13,14 @@ import javax.annotation.Resource;
public class SysDeptProducer { public class SysDeptProducer {
@Resource @Resource
private StringRedisTemplate stringRedisTemplate; private RedisMQTemplate redisMQTemplate;
/** /**
* {@link SysDeptRefreshMessage} * {@link SysDeptRefreshMessage}
*/ */
public void sendDeptRefreshMessage() { public void sendDeptRefreshMessage() {
SysDeptRefreshMessage message = new SysDeptRefreshMessage(); SysDeptRefreshMessage message = new SysDeptRefreshMessage();
RedisMessageUtils.sendChannelMessage(stringRedisTemplate, message); redisMQTemplate.send(message);
} }
} }

View File

@ -1,8 +1,7 @@
package cn.iocoder.yudao.adminserver.modules.system.mq.producer.dict; package cn.iocoder.yudao.adminserver.modules.system.mq.producer.dict;
import cn.iocoder.yudao.framework.mq.core.util.RedisMessageUtils;
import cn.iocoder.yudao.adminserver.modules.system.mq.message.dict.SysDictDataRefreshMessage; import cn.iocoder.yudao.adminserver.modules.system.mq.message.dict.SysDictDataRefreshMessage;
import org.springframework.data.redis.core.StringRedisTemplate; import cn.iocoder.yudao.framework.mq.core.RedisMQTemplate;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import javax.annotation.Resource; import javax.annotation.Resource;
@ -14,14 +13,14 @@ import javax.annotation.Resource;
public class SysDictDataProducer { public class SysDictDataProducer {
@Resource @Resource
private StringRedisTemplate stringRedisTemplate; private RedisMQTemplate redisMQTemplate;
/** /**
* {@link SysDictDataRefreshMessage} * {@link SysDictDataRefreshMessage}
*/ */
public void sendDictDataRefreshMessage() { public void sendDictDataRefreshMessage() {
SysDictDataRefreshMessage message = new SysDictDataRefreshMessage(); SysDictDataRefreshMessage message = new SysDictDataRefreshMessage();
RedisMessageUtils.sendChannelMessage(stringRedisTemplate, message); redisMQTemplate.send(message);
} }
} }

View File

@ -1,8 +1,7 @@
package cn.iocoder.yudao.adminserver.modules.system.mq.producer.permission; package cn.iocoder.yudao.adminserver.modules.system.mq.producer.permission;
import cn.iocoder.yudao.framework.mq.core.util.RedisMessageUtils;
import cn.iocoder.yudao.adminserver.modules.system.mq.message.permission.SysMenuRefreshMessage; import cn.iocoder.yudao.adminserver.modules.system.mq.message.permission.SysMenuRefreshMessage;
import org.springframework.data.redis.core.StringRedisTemplate; import cn.iocoder.yudao.framework.mq.core.RedisMQTemplate;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import javax.annotation.Resource; import javax.annotation.Resource;
@ -14,14 +13,14 @@ import javax.annotation.Resource;
public class SysMenuProducer { public class SysMenuProducer {
@Resource @Resource
private StringRedisTemplate stringRedisTemplate; private RedisMQTemplate redisMQTemplate;
/** /**
* {@link SysMenuRefreshMessage} * {@link SysMenuRefreshMessage}
*/ */
public void sendMenuRefreshMessage() { public void sendMenuRefreshMessage() {
SysMenuRefreshMessage message = new SysMenuRefreshMessage(); SysMenuRefreshMessage message = new SysMenuRefreshMessage();
RedisMessageUtils.sendChannelMessage(stringRedisTemplate, message); redisMQTemplate.send(message);
} }
} }

View File

@ -1,8 +1,7 @@
package cn.iocoder.yudao.adminserver.modules.system.mq.producer.permission; package cn.iocoder.yudao.adminserver.modules.system.mq.producer.permission;
import cn.iocoder.yudao.framework.mq.core.util.RedisMessageUtils;
import cn.iocoder.yudao.adminserver.modules.system.mq.message.permission.SysRoleMenuRefreshMessage; import cn.iocoder.yudao.adminserver.modules.system.mq.message.permission.SysRoleMenuRefreshMessage;
import org.springframework.data.redis.core.StringRedisTemplate; import cn.iocoder.yudao.framework.mq.core.RedisMQTemplate;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import javax.annotation.Resource; import javax.annotation.Resource;
@ -14,14 +13,14 @@ import javax.annotation.Resource;
public class SysPermissionProducer { public class SysPermissionProducer {
@Resource @Resource
private StringRedisTemplate stringRedisTemplate; private RedisMQTemplate redisMQTemplate;
/** /**
* {@link SysRoleMenuRefreshMessage} * {@link SysRoleMenuRefreshMessage}
*/ */
public void sendRoleMenuRefreshMessage() { public void sendRoleMenuRefreshMessage() {
SysRoleMenuRefreshMessage message = new SysRoleMenuRefreshMessage(); SysRoleMenuRefreshMessage message = new SysRoleMenuRefreshMessage();
RedisMessageUtils.sendChannelMessage(stringRedisTemplate, message); redisMQTemplate.send(message);
} }
} }

View File

@ -1,8 +1,7 @@
package cn.iocoder.yudao.adminserver.modules.system.mq.producer.permission; package cn.iocoder.yudao.adminserver.modules.system.mq.producer.permission;
import cn.iocoder.yudao.framework.mq.core.util.RedisMessageUtils;
import cn.iocoder.yudao.adminserver.modules.system.mq.message.permission.SysRoleRefreshMessage; import cn.iocoder.yudao.adminserver.modules.system.mq.message.permission.SysRoleRefreshMessage;
import org.springframework.data.redis.core.StringRedisTemplate; import cn.iocoder.yudao.framework.mq.core.RedisMQTemplate;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import javax.annotation.Resource; import javax.annotation.Resource;
@ -16,14 +15,14 @@ import javax.annotation.Resource;
public class SysRoleProducer { public class SysRoleProducer {
@Resource @Resource
private StringRedisTemplate stringRedisTemplate; private RedisMQTemplate redisMQTemplate;
/** /**
* {@link SysRoleRefreshMessage} * {@link SysRoleRefreshMessage}
*/ */
public void sendRoleRefreshMessage() { public void sendRoleRefreshMessage() {
SysRoleRefreshMessage message = new SysRoleRefreshMessage(); SysRoleRefreshMessage message = new SysRoleRefreshMessage();
RedisMessageUtils.sendChannelMessage(stringRedisTemplate, message); redisMQTemplate.send(message);
} }
} }

View File

@ -1,15 +1,12 @@
package cn.iocoder.yudao.adminserver.modules.system.mq.producer.sms; package cn.iocoder.yudao.adminserver.modules.system.mq.producer.sms;
import cn.iocoder.yudao.framework.common.core.KeyValue;
import cn.iocoder.yudao.framework.mq.core.util.RedisMessageUtils;
import cn.iocoder.yudao.adminserver.modules.system.mq.message.sms.SysSmsChannelRefreshMessage; import cn.iocoder.yudao.adminserver.modules.system.mq.message.sms.SysSmsChannelRefreshMessage;
import cn.iocoder.yudao.adminserver.modules.system.mq.message.sms.SysSmsTemplateRefreshMessage; import cn.iocoder.yudao.adminserver.modules.system.mq.message.sms.SysSmsTemplateRefreshMessage;
import cn.iocoder.yudao.framework.mq.core.RedisMQTemplate;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import javax.annotation.Resource; import javax.annotation.Resource;
import java.util.List;
/** /**
* Sms Producer * Sms Producer
@ -22,14 +19,14 @@ import java.util.List;
public class SysSmsProducer { public class SysSmsProducer {
@Resource @Resource
private StringRedisTemplate stringRedisTemplate; private RedisMQTemplate redisMQTemplate;
/** /**
* {@link SysSmsChannelRefreshMessage} * {@link SysSmsChannelRefreshMessage}
*/ */
public void sendSmsChannelRefreshMessage() { public void sendSmsChannelRefreshMessage() {
SysSmsChannelRefreshMessage message = new SysSmsChannelRefreshMessage(); SysSmsChannelRefreshMessage message = new SysSmsChannelRefreshMessage();
RedisMessageUtils.sendChannelMessage(stringRedisTemplate, message); redisMQTemplate.send(message);
} }
/** /**
@ -37,7 +34,7 @@ public class SysSmsProducer {
*/ */
public void sendSmsTemplateRefreshMessage() { public void sendSmsTemplateRefreshMessage() {
SysSmsTemplateRefreshMessage message = new SysSmsTemplateRefreshMessage(); SysSmsTemplateRefreshMessage message = new SysSmsTemplateRefreshMessage();
RedisMessageUtils.sendChannelMessage(stringRedisTemplate, message); redisMQTemplate.send(message);
} }
} }

View File

@ -1,8 +1,9 @@
package cn.iocoder.yudao.coreservice.modules.system.mq.message.sms; package cn.iocoder.yudao.coreservice.modules.system.mq.message.sms;
import cn.iocoder.yudao.framework.common.core.KeyValue; import cn.iocoder.yudao.framework.common.core.KeyValue;
import cn.iocoder.yudao.framework.mq.core.stream.StreamMessage; import cn.iocoder.yudao.framework.mq.core.stream.AbstractStreamMessage;
import lombok.Data; import lombok.Data;
import lombok.EqualsAndHashCode;
import javax.validation.constraints.NotNull; import javax.validation.constraints.NotNull;
import java.util.List; import java.util.List;
@ -13,7 +14,8 @@ import java.util.List;
* @author * @author
*/ */
@Data @Data
public class SysSmsSendMessage implements StreamMessage { @EqualsAndHashCode(callSuper = true)
public class SysSmsSendMessage extends AbstractStreamMessage {
/** /**
* *

View File

@ -2,9 +2,8 @@ package cn.iocoder.yudao.coreservice.modules.system.mq.producer.sms;
import cn.iocoder.yudao.coreservice.modules.system.mq.message.sms.SysSmsSendMessage; import cn.iocoder.yudao.coreservice.modules.system.mq.message.sms.SysSmsSendMessage;
import cn.iocoder.yudao.framework.common.core.KeyValue; import cn.iocoder.yudao.framework.common.core.KeyValue;
import cn.iocoder.yudao.framework.mq.core.util.RedisMessageUtils; import cn.iocoder.yudao.framework.mq.core.RedisMQTemplate;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import javax.annotation.Resource; import javax.annotation.Resource;
@ -21,7 +20,7 @@ import java.util.List;
public class SysSmsCoreProducer { public class SysSmsCoreProducer {
@Resource @Resource
private StringRedisTemplate stringRedisTemplate; private RedisMQTemplate redisMQTemplate;
/** /**
* {@link SysSmsSendMessage} * {@link SysSmsSendMessage}
@ -36,7 +35,7 @@ public class SysSmsCoreProducer {
Long channelId, String apiTemplateId, List<KeyValue<String, Object>> templateParams) { Long channelId, String apiTemplateId, List<KeyValue<String, Object>> templateParams) {
SysSmsSendMessage message = new SysSmsSendMessage().setLogId(logId).setMobile(mobile); SysSmsSendMessage message = new SysSmsSendMessage().setLogId(logId).setMobile(mobile);
message.setChannelId(channelId).setApiTemplateId(apiTemplateId).setTemplateParams(templateParams); message.setChannelId(channelId).setApiTemplateId(apiTemplateId).setTemplateParams(templateParams);
RedisMessageUtils.sendStreamMessage(stringRedisTemplate, message); redisMQTemplate.send(message);
} }
} }

View File

@ -1,12 +1,12 @@
package cn.iocoder.yudao.framework.mq.config; package cn.iocoder.yudao.framework.mq.config;
import cn.hutool.system.SystemUtil; import cn.hutool.system.SystemUtil;
import cn.iocoder.yudao.framework.mq.core.RedisMQTemplate;
import cn.iocoder.yudao.framework.mq.core.pubsub.AbstractChannelMessageListener; import cn.iocoder.yudao.framework.mq.core.pubsub.AbstractChannelMessageListener;
import cn.iocoder.yudao.framework.mq.core.stream.AbstractStreamMessageListener; import cn.iocoder.yudao.framework.mq.core.stream.AbstractStreamMessageListener;
import cn.iocoder.yudao.framework.redis.config.YudaoRedisAutoConfiguration; import cn.iocoder.yudao.framework.redis.config.YudaoRedisAutoConfiguration;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.AutoConfigureAfter; import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory; import org.springframework.data.redis.connection.RedisConnectionFactory;
@ -15,6 +15,7 @@ import org.springframework.data.redis.connection.stream.ObjectRecord;
import org.springframework.data.redis.connection.stream.ReadOffset; import org.springframework.data.redis.connection.stream.ReadOffset;
import org.springframework.data.redis.connection.stream.StreamOffset; import org.springframework.data.redis.connection.stream.StreamOffset;
import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.listener.ChannelTopic; import org.springframework.data.redis.listener.ChannelTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer; import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.data.redis.stream.StreamMessageListenerContainer; import org.springframework.data.redis.stream.StreamMessageListenerContainer;
@ -31,6 +32,13 @@ import java.util.List;
@Slf4j @Slf4j
public class YudaoMQAutoConfiguration { public class YudaoMQAutoConfiguration {
@Bean
public RedisMQTemplate redisMQTemplate(StringRedisTemplate stringRedisTemplate) {
return new RedisMQTemplate(stringRedisTemplate);
}
// ========== 消费者相关 ==========
/** /**
* Redis Pub/Sub 广 * Redis Pub/Sub 广
*/ */
@ -71,7 +79,6 @@ public class YudaoMQAutoConfiguration {
// 第二步,注册监听器,消费对应的 Stream 主题 // 第二步,注册监听器,消费对应的 Stream 主题
String consumerName = buildConsumerName(); String consumerName = buildConsumerName();
// String consumerName = "110";
listeners.forEach(listener -> { listeners.forEach(listener -> {
// 创建 listener 对应的消费者分组 // 创建 listener 对应的消费者分组
try { try {

View File

@ -1,37 +1,39 @@
package cn.iocoder.yudao.framework.mq.core.util; package cn.iocoder.yudao.framework.mq.core;
import cn.iocoder.yudao.framework.mq.core.pubsub.ChannelMessage;
import cn.iocoder.yudao.framework.mq.core.stream.StreamMessage;
import cn.iocoder.yudao.framework.common.util.json.JsonUtils; import cn.iocoder.yudao.framework.common.util.json.JsonUtils;
import cn.iocoder.yudao.framework.mq.core.pubsub.AbstractChannelMessage;
import cn.iocoder.yudao.framework.mq.core.stream.AbstractStreamMessage;
import lombok.AllArgsConstructor;
import org.springframework.data.redis.connection.stream.RecordId; import org.springframework.data.redis.connection.stream.RecordId;
import org.springframework.data.redis.connection.stream.StreamRecords; import org.springframework.data.redis.connection.stream.StreamRecords;
import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.core.RedisTemplate;
/** /**
* Redis * Redis MQ
* *
* @author * @author
*/ */
public class RedisMessageUtils { @AllArgsConstructor
public class RedisMQTemplate {
private final RedisTemplate<String, ?> redisTemplate;
/** /**
* Redis Redis pub/sub * Redis Redis pub/sub
* *
* @param redisTemplate Redis
* @param message * @param message
*/ */
public static <T extends ChannelMessage> void sendChannelMessage(RedisTemplate<?, ?> redisTemplate, T message) { public <T extends AbstractChannelMessage> void send(T message) {
redisTemplate.convertAndSend(message.getChannel(), JsonUtils.toJsonString(message)); redisTemplate.convertAndSend(message.getChannel(), JsonUtils.toJsonString(message));
} }
/** /**
* Redis Redis Stream * Redis Redis Stream
* *
* @param redisTemplate Redis
* @param message * @param message
* @return * @return
*/ */
public static <T extends StreamMessage> RecordId sendStreamMessage(RedisTemplate<String, ?> redisTemplate, T message) { public <T extends AbstractStreamMessage> RecordId send(T message) {
return redisTemplate.opsForStream().add(StreamRecords.newRecord() return redisTemplate.opsForStream().add(StreamRecords.newRecord()
.ofObject(JsonUtils.toJsonString(message)) // 设置内容 .ofObject(JsonUtils.toJsonString(message)) // 设置内容
.withStreamKey(message.getStreamKey())); // 设置 stream key .withStreamKey(message.getStreamKey())); // 设置 stream key

View File

@ -0,0 +1,26 @@
package cn.iocoder.yudao.framework.mq.core.message;
import java.util.HashMap;
import java.util.Map;
/**
* Redis
*
* @author
*/
public abstract class AbstractRedisMessage {
/**
*
*/
private final Map<String, String> headers = new HashMap<>();
public String getHeader(String key) {
return headers.get(key);
}
public void addHeader(String key, String value) {
headers.put(key, value);
}
}

View File

@ -1,13 +1,14 @@
package cn.iocoder.yudao.framework.mq.core.pubsub; package cn.iocoder.yudao.framework.mq.core.pubsub;
import cn.iocoder.yudao.framework.mq.core.message.AbstractRedisMessage;
import com.fasterxml.jackson.annotation.JsonIgnore; import com.fasterxml.jackson.annotation.JsonIgnore;
/** /**
* Redis Channel Message * Redis Channel Message
* *
* @author * @author
*/ */
public interface ChannelMessage { public abstract class AbstractChannelMessage extends AbstractRedisMessage {
/** /**
* Redis Channel * Redis Channel
@ -15,6 +16,6 @@ public interface ChannelMessage {
* @return Channel * @return Channel
*/ */
@JsonIgnore // 避免序列化 @JsonIgnore // 避免序列化
String getChannel(); public abstract String getChannel();
} }

View File

@ -15,7 +15,7 @@ import java.lang.reflect.Type;
* *
* @author * @author
*/ */
public abstract class AbstractChannelMessageListener<T extends ChannelMessage> implements MessageListener { public abstract class AbstractChannelMessageListener<T extends AbstractChannelMessage> implements MessageListener {
/** /**
* *

View File

@ -3,11 +3,11 @@ package cn.iocoder.yudao.framework.mq.core.stream;
import com.fasterxml.jackson.annotation.JsonIgnore; import com.fasterxml.jackson.annotation.JsonIgnore;
/** /**
* Redis Stream Message * Redis Stream Message
* *
* @author * @author
*/ */
public interface StreamMessage { public abstract class AbstractStreamMessage {
/** /**
* Redis Stream Key * Redis Stream Key
@ -15,6 +15,6 @@ public interface StreamMessage {
* @return Channel * @return Channel
*/ */
@JsonIgnore // 避免序列化 @JsonIgnore // 避免序列化
String getStreamKey(); public abstract String getStreamKey();
} }

View File

@ -19,7 +19,7 @@ import java.lang.reflect.Type;
* *
* @author * @author
*/ */
public abstract class AbstractStreamMessageListener<T extends StreamMessage> public abstract class AbstractStreamMessageListener<T extends AbstractStreamMessage>
implements StreamListener<String, ObjectRecord<String, String>> { implements StreamListener<String, ObjectRecord<String, String>> {
/** /**
@ -39,7 +39,7 @@ public abstract class AbstractStreamMessageListener<T extends StreamMessage>
@Getter @Getter
private String group; private String group;
/** /**
* * RedisTemplate
*/ */
@Setter @Setter
private RedisTemplate<String, ?> redisTemplate; private RedisTemplate<String, ?> redisTemplate;

View File

@ -4,5 +4,11 @@
* 2. Web HTTP API Header tenant-id * 2. Web HTTP API Header tenant-id
* 3. Job JobHandler * 3. Job JobHandler
* 4. MQTODO * 4. MQTODO
* 5. Async ThreadLocal 使 TransmittableThreadLocal
* 1Spring Async
* {@link cn.iocoder.yudao.framework.quartz.config.YudaoAsyncAutoConfiguration#threadPoolTaskExecutorBeanPostProcessor()}
* 2Spring Security
* TransmittableThreadLocalSecurityContextHolderStrategy
* YudaoSecurityAutoConfiguration#securityContextHolderMethodInvokingFactoryBean()
*/ */
package cn.iocoder.yudao.framework.tenant; package cn.iocoder.yudao.framework.tenant;