spingboot实现redis的发布订阅
生活随笔
收集整理的這篇文章主要介紹了
spingboot实现redis的发布订阅
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
配置訂閱頻道
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.data.redis.connection.RedisConnectionFactory; import org.springframework.data.redis.listener.ChannelTopic; import org.springframework.data.redis.listener.RedisMessageListenerContainer; import org.springframework.data.redis.listener.adapter.MessageListenerAdapter;/**訂閱配置 topic1,topic2是頻道,可以多個(gè)*/ @Configuration public class MyRedisMessageListenerContainer {@BeanMessageListenerAdapter messageListener() {return new MessageListenerAdapter(new MySubcribe());}@BeanRedisMessageListenerContainer redisContainer(RedisConnectionFactory factory) {final RedisMessageListenerContainer container = new RedisMessageListenerContainer();container.setConnectionFactory(factory);container.addMessageListener(messageListener(), new ChannelTopic("topic1"));container.addMessageListener(messageListener(), new ChannelTopic("topic2"));return container;} }配置訂閱者頻道
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.core.StringRedisTemplate; import org.springframework.stereotype.Service;@Service public class MySubcribe implements MessageListener {@Autowiredprivate StringRedisTemplate redisTemplate;@Overridepublic void onMessage(Message message, byte[] pattern) {System.out.println("接收數(shù)據(jù):"+message.toString());System.out.println("訂閱頻道:"+new String(message.getChannel()));} }配置發(fā)布者:
//寫(xiě)入redis 隊(duì)列redisTemplate.convertAndSend("topic1",insertSql);總結(jié)
以上是生活随笔為你收集整理的spingboot实现redis的发布订阅的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 建行生活APP如何开通龙支付
- 下一篇: 每日优鲜客服