(十九)springboot实战——springboot集成redis实现消息的订阅与发布_springboot redis消息订阅-CSDN博客
https://blog.csdn.net/yprufeng/article/details/136151115
package com.yundi.atp.config;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.RedisMessageListenerContainer;
import org.springframework.data.redis.serializer.StringRedisSerializer;@Configuration
public class RedisConfig {/*** 初始化一个Redis消息监听容器* @param connectionFactory* @return*/@Beanpublic RedisMessageListenerContainer redisMessageListenerContainer(RedisConnectionFactory connectionFactory) {RedisMessageListenerContainer container = new RedisMessageListenerContainer();container.setConnectionFactory(connectionFactory);// 添加其他配置,如线程池大小等return container;}@Beanpublic RedisTemplate<String, String> redisTemplate(RedisConnectionFactory connectionFactory) {RedisTemplate<String, String> redisTemplate = new RedisTemplate<>();redisTemplate.setConnectionFactory(connectionFactory);redisTemplate.setDefaultSerializer(new StringRedisSerializer());return redisTemplate;}
}
package com.yundi.atp.controller;import com.yundi.atp.constant.ChannelConstant;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;import javax.annotation.Resource;@RequestMapping(value = "base")
@RestController
public class BaseController {@Resourceprivate RedisTemplate redisTemplate;/*** 发布广播消息** @param msg*/@GetMapping(value = "/publish/{msg}")public void sendMsg(@PathVariable(value = "msg") String msg) {redisTemplate.convertAndSend(ChannelConstant.CHANNEL_GLOBAL_NAME, msg);}
}
package com.yundi.atp.listen;import com.yundi.atp.constant.ChannelConstant;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.connection.MessageListener;
import org.springframework.data.redis.listener.ChannelTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.stereotype.Component;import javax.annotation.PostConstruct;
import java.nio.charset.StandardCharsets;@Slf4j
@Component
public class RedisMessageSubscriber implements MessageListener {@Autowiredprivate RedisMessageListenerContainer redisMessageListenerContainer;/*** 订阅消息:将订阅者添加到指定的频道*/@PostConstructpublic void subscribeToChannel() {//广播消息redisMessageListenerContainer.addMessageListener(this, new ChannelTopic(ChannelConstant.CHANNEL_GLOBAL_NAME));}@Overridepublic void onMessage(Message message, byte[] bytes) {String channel = new String(message.getChannel(), StandardCharsets.UTF_8);String messageBody = new String(message.getBody(), StandardCharsets.UTF_8);log.info("Received message: " + messageBody + " from channel: " + channel);}
}