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;

相关推荐
h79971015 分钟前
redis lua脚本(go)调用教程以及debug调试
redis·golang·lua
虫师c3 小时前
分布式缓存实战:Redis集群与性能优化
redis·分布式·缓存·redis集群·高可用架构·生产环境·数据分片
我真的是大笨蛋16 小时前
Redis的String详解
java·数据库·spring boot·redis·spring·缓存
zhengzizhe18 小时前
Redssion出现attempt to unlock lock, not locked by current thread by node id
redis
兜兜风d'1 天前
redis字符串命令
数据库·redis·缓存
西瓜er1 天前
Docker 一键部署指南:GitLab、Nacos、Redis、MySQL 与 MinIO 全解析
redis·docker·gitlab
道可到1 天前
别再瞎拼技术栈!Postgres 已经能干 Redis 的活了
redis·后端·postgresql
野犬寒鸦1 天前
从零起步学习Redis || 第十二章:Redis Cluster集群如何解决Redis单机模式的性能瓶颈及高可用分布式部署方案详解
java·数据库·redis·后端·缓存
悟能不能悟1 天前
redis的红锁
数据库·redis·缓存
qq_5470261792 天前
SpringBoot+Redis实现电商秒杀方案
spring boot·redis·后端