redis发布订阅

记录学习
redis发布订阅

1.创建配置类

package com.live.server.config;

import org.springframework.cache.annotation.EnableCaching;
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.StringRedisTemplate;
import org.springframework.data.redis.listener.PatternTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.data.redis.listener.adapter.MessageListenerAdapter;

/**
 * 作者:Lelouch
 * 时间:2020/11/1214:20
 */
@Configuration
@EnableCaching
public class RedisSubListenerConfig {

 //初始化监听器
    @Bean
    public RedisMessageListenerContainer container(RedisConnectionFactory connectionFactory,
                                                   MessageListenerAdapter listenerAdapter,
                                                   MessageListenerAdapter messageListenerAdapter) {
        RedisMessageListenerContainer container = new RedisMessageListenerContainer();
        container.setConnectionFactory(connectionFactory);
        container.addMessageListener(listenerAdapter, new PatternTopic("live-server/chat/websocket"));
        container.addMessageListener(messageListenerAdapter, new PatternTopic("live-server/chat/userMap"));
        return container;
    }

    //利用反射来创建监听到消息之后的执行方法
    //WebSocket 类 下的receiveMessage
    @Bean
    public MessageListenerAdapter listenerAdapter(WebSocket webSocket) {
        return new MessageListenerAdapter(webSocket, "receiveMessage");
    }
    //利用反射来创建监听到消息之后的执行方法
    //WebSocket 类 下的receiveLivePeople
    @Bean
    public MessageListenerAdapter messageListenerAdapter(WebSocket webSocket) {
        return new MessageListenerAdapter(webSocket, "receiveLivePeople");
    }

    @Bean
    StringRedisTemplate stringRedisTemplate(RedisConnectionFactory connectionFactory) {
        return new StringRedisTemplate(connectionFactory);
    }

}

2.发布消息

		
        String channel = "live-server/chat/userMap";
        stringRedisTemplate.convertAndSend(channel,s);


3.接收消息

    public void receiveLivePeople(String s)  {
        //这里是收到通道的消息之后执行的方法
        try {
            if(s!=null){
                Gson gson = new Gson();
                Map<String, Long> map = new HashMap<String, Long>();
                map = gson.fromJson(s, map.getClass());

                Long roomId = Long.parseLong(String.valueOf(map.get("roomId")));
                if(rooms.get(roomId)!=null){
                    for (Session session : rooms.get(roomId)) {
                        session.getBasicRemote().sendText(s);
                    }
                }

            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值