Redis消息队列,获取message.getBody(),有乱码问题解决。

想要做一个redis的消息队列。运行时发现message.getBody()存在乱码
�� t{"env":"hw","group":"job"}

首先做了一下内容

java 复制代码
// Bean类
@Data
public class ADto{
  private String env;
  private String group;
}
java 复制代码
// controller类
@RestController
public class testController{
    @Resource
    private RedisTemplate redisTemplate;

    @ApiOperation("测试redis消息队列")
    @PostMapping("/redisTest")
    public String redisTest(@RequestBody ADto adto){
        redisTemplate.convertAndSend("REDIS_TEST_CHANNEL", JSONObject.toJSONString(adto));
        return "发送成功!";
    }
}
java 复制代码
//Service 类
@Service
public class AService{
    public void warnNotice(ADto adto){
    // 具体处理逻辑
        System.println.out("打印:" + adto.toString());
    }
}
java 复制代码
// redis监听器
@Component
public class RedisConfig {
    @Bean
    public ChannelTopic topic() {
        return new ChannelTopic("WARN_NOTICE_CHANNEL");
    }

    @Bean
    public MessageListenerAdapter messageListenerAdapter(WarnListener listener) {
        return new MessageListenerAdapter(listener);
    }

    @Bean
    public RedisMessageListenerContainer redisContainer(RedisConnectionFactory redisConnectionFactory,
                                                        MessageListenerAdapter messageListenerAdapter,
                                                        ChannelTopic topic) {
        RedisMessageListenerContainer container = new RedisMessageListenerContainer();
        container.setConnectionFactory(redisConnectionFactory);
        container.addMessageListener(messageListenerAdapter, topic);
        return container;
    }

@Component
public class WarnListener implements MessageListener {

    @Resource
    private AExtService AExtService;

    private final Object lock = new Object();

    @Override
    public void onMessage(Message message, byte[] pattern) {
        synchronized(lock) {
            ADto dto = JSONObject.parseObject(message.getBody(), ADto .class);
            AExtService.warnNotice(dto);
        }

    }
}

运行启动以后,代码在ADto dto = JSONObject.parseObject(message.getBody(), ADto .class);这一行,直接执行结束,打断点以后发现,message.getBody()获取到的内容,不是一个普通的json,而是有乱码的

�� t{"env":"hw","group":"job"}

解决方案:

将

@Resource

private RedisTemplate redisTemplate;

修改为

@Resource

private StringRedisTemplate stringRedisTemplate;

相关推荐
樱花落木兰1 小时前
分布式登录实战:Session 会话共享改造,Redis 存储用户登录状态
java·javascript·数据库·redis·分布式·缓存
ShineWinsu2 小时前
对于Redis:Steam、Geospatial、Hyperloglog、Bitmap、Bitfield类型的解析
数据库·c++·redis·分布式·缓存·面试·zset
海绵宝宝转agent4 小时前
基于Redis ZSet+AOP+注解实现限流注解算法
数据库·redis·算法
imDwAaY4 小时前
Redis 也能做消息队列?从 Stream 的存储讲到消费确认
数据库·redis·缓存
imDwAaY7 小时前
Redis 哈希表如何边扩容边服务?讲透渐进式 Rehash
redis·哈希算法·散列表
仍然.8 小时前
Redis---主从复制
java·数据库·redis
Flynt17 小时前
Redis Cluster主节点挂了,为什么"高可用"还全员掉线?我把三次kill的记录翻出来了
数据库·redis·分布式
写后端的胖头鱼20 小时前
一文详解Cache Aside(旁路缓存模式)
redis·缓存·延迟双删·缓存一致
Web3&Basketball20 小时前
用FastAPI+Redis 复刻常驻Agent
redis·bootstrap·fastapi
ly76891 天前
Redis 主从复制全解析:从 PSYNC 协议到复制积压缓冲区的故障切换边界
java·数据库·redis·主从复制·故障切换·psync