springboot:整合redis消息队列

整合redis消息队列

项目依赖

		<!-- RedisTemplate -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>
        <!-- Redis-Jedis -->
        <dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
            <version>2.9.0</version>
        </dependency>

application.yml配置文件配置redis信息

spring:
  # Redis配置
  redis:
    timeout: 10s
    lettuce:
      pool:
        max-active: 200
        max-idle: 8
        max-wait: 10s
        min-idle: 2
      shutdown-timeout: 3s
    database: 0
    port: 6379
    host: 127.0.0.1
    password:

配置redis监听器(配置订阅的频道)

package com.sinosoft.springbootplus.common.config;

import com.sinosoft.springbootplus.common.service.MySubscribe;
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.PatternTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;

/**
 * redis配置
 * @author lsh
 * @date 2022/8/9
 */
@Configuration
public class RedisConfig {
    /**
     * redis消息监听器容器
     */
    @Bean
    public RedisMessageListenerContainer container(RedisConnectionFactory connectionFactory) {
        RedisMessageListenerContainer container = new RedisMessageListenerContainer();
        container.setConnectionFactory(connectionFactory);

        //订阅频道,通配符*表示任意多个占位符
        container.addMessageListener(new MySubscribe(), new PatternTopic("websocket"));

        return container;
    }
}

接收订阅的消息

1、此处是接收订阅的消息,然后处理订阅的消息,处理与redis消息本身没有关系。所以可以转换成对象,再调具体处理的接口(处理的接口不需要关心消息本身)
2、存入redis消息队列时,用了genericJackson2JsonRedisSerializer的序列化方式,需要使用genericJackson2JsonRedisSerializer的反序列化方式反序列化成对象。

package com.sinosoft.springbootplus.common.service;

import com.sinosoft.springbootplus.common.api.DealMySubscribeMessageApi;
import com.sinosoft.springbootplus.common.vo.WebSocketMessageVo;
import com.sinosoft.springbootplus.util.MapperUtils;
import com.sinosoft.springbootplus.util.SpringContextUtil;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.connection.MessageListener;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;

/**
 * 我的订阅
 * @author lsh
 * @date 2022/8/9
 */
@Slf4j
public class MySubscribe implements MessageListener {
    private DealMySubscribeMessageApi dealMySubscribeMessageApi;
    GenericJackson2JsonRedisSerializer genericJackson2JsonRedisSerializer = new GenericJackson2JsonRedisSerializer();
    @SneakyThrows
    @Override
    public void onMessage(Message message, byte[] bytes) {
        if(null == dealMySubscribeMessageApi){
            dealMySubscribeMessageApi = SpringContextUtil.getBean(DealMySubscribeMessageApi.class);
        }
        log.debug("订阅频道是【{}】接收的数据是【{}】",new String(message.getChannel()),new String(message.getBody()));
        String channel = new String(message.getChannel());
        String macAddress = StringUtils.substring(channel,9);
        //此处将订阅的消息统一转换成了对象,后面处理具体的消息与redis消息本身没有关系
        WebSocketMessageVo webSocketMessageVo =(WebSocketMessageVo) (genericJackson2JsonRedisSerializer.deserialize(message.getBody()));
        dealMySubscribeMessageApi.dealMySubscribeMessage(macAddress,webSocketMessageVo);

    }
}

处理订阅的消息api

package com.sinosoft.springbootplus.common.api;

import com.sinosoft.springbootplus.common.vo.WebSocketMessageVo;
import org.springframework.data.redis.connection.Message;

/**
 * 处理我订阅的消息接口
 * @author lsh
 * @date 2022/8/9
 */
public interface DealMySubscribeMessageApi {
    /**
     * 处理我订阅的消息
     */
    void dealMySubscribeMessage(String macAddress, WebSocketMessageVo webSocketMessageVo);
}

实现处理订阅的消息api接口

package com.sinosoft.springbootplus.common.service;

import com.alibaba.fastjson.JSONObject;
import com.sinosoft.springbootplus.common.api.DealMySubscribeMessageApi;
import com.sinosoft.springbootplus.common.vo.RedisMessageVo;
import com.sinosoft.springbootplus.common.vo.WebSocketMessageVo;
import com.sinosoft.springbootplus.system.vo.SessionVo;
import com.sinosoft.springbootplus.util.Jackson;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.redis.connection.Message;
import org.springframework.stereotype.Service;

/**
 * 处理我订阅的消息实现类
 * @author lsh
 * @date 2022/8/9
 */
@Service
@Slf4j
public class DealMySubscribeMessageImpl implements DealMySubscribeMessageApi {
    private  MyWebsocketImpl myWebsocket;

    public DealMySubscribeMessageImpl(MyWebsocketImpl myWebsocket) {
        this.myWebsocket = myWebsocket;
    }

    @Override
    public void dealMySubscribeMessage(String macAddress,WebSocketMessageVo webSocketMessageVo) {
        log.info("给设备【{}】发消息【{}】",macAddress,Jackson.toJsonString(webSocketMessageVo));

        myWebsocket.send(Jackson.toJsonString(webSocketMessageVo),macAddress);
    }
}


在这里插入图片描述

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值