|
@@ -1,13 +1,23 @@
|
|
|
package cn.iocoder.dashboard.framework.redis.config;
|
|
|
|
|
|
+import cn.iocoder.dashboard.framework.redis.core.listener.AbstractMessageListener;
|
|
|
import com.alibaba.fastjson.support.spring.GenericFastJsonRedisSerializer;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
import org.springframework.context.annotation.Bean;
|
|
|
import org.springframework.context.annotation.Configuration;
|
|
|
import org.springframework.data.redis.connection.RedisConnectionFactory;
|
|
|
import org.springframework.data.redis.core.RedisTemplate;
|
|
|
+import org.springframework.data.redis.listener.ChannelTopic;
|
|
|
+import org.springframework.data.redis.listener.RedisMessageListenerContainer;
|
|
|
import org.springframework.data.redis.serializer.RedisSerializer;
|
|
|
|
|
|
+import java.util.List;
|
|
|
+
|
|
|
+/**
|
|
|
+ * Redis 配置类
|
|
|
+ */
|
|
|
@Configuration
|
|
|
+@Slf4j
|
|
|
public class RedisConfig {
|
|
|
|
|
|
@Bean
|
|
@@ -23,4 +33,20 @@ public class RedisConfig {
|
|
|
return template;
|
|
|
}
|
|
|
|
|
|
+ @Bean
|
|
|
+ public RedisMessageListenerContainer redisMessageListenerContainer(RedisConnectionFactory factory,
|
|
|
+ List<AbstractMessageListener<?>> listeners) {
|
|
|
+ // 创建 RedisMessageListenerContainer 对象
|
|
|
+ RedisMessageListenerContainer container = new RedisMessageListenerContainer();
|
|
|
+ // 设置 RedisConnection 工厂。
|
|
|
+ container.setConnectionFactory(factory);
|
|
|
+ // 添加监听器
|
|
|
+ listeners.forEach(listener -> {
|
|
|
+ container.addMessageListener(listener, new ChannelTopic(listener.getChannel()));
|
|
|
+ log.info("[redisMessageListenerContainer][注册 Channel({}) 对应的监听器({})]",
|
|
|
+ listener.getChannel(), listener.getClass().getName());
|
|
|
+ });
|
|
|
+ return container;
|
|
|
+ }
|
|
|
+
|
|
|
}
|