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;

相关推荐
海梨花2 小时前
【从零开始学习Redis】项目实战-黑马点评D2
java·数据库·redis·后端·缓存
鼠鼠我捏,要死了捏19 小时前
生产环境Redis缓存穿透与雪崩防护性能优化实战指南
redis·cache
曾经的三心草1 天前
微服务的编程测评系统11-jmeter-redis-竞赛列表
redis·jmeter·微服务
努力努力再努力wz1 天前
【c++深入系列】:万字详解模版(下)
java·c++·redis
2301_793086871 天前
Redis 04 Reactor
数据库·redis·缓存
AAA修煤气灶刘哥1 天前
搞定 Redis 不难:从安装到实战的保姆级教程
java·redis·后端
青鱼入云1 天前
redis怎么做rehash的
redis·缓存
考虑考虑1 天前
Redis事务
redis·后端
陈天cjq2 天前
Redis 实用型限流与延时队列:从 Lua 固定/滑动窗口到 Streams 消费组(含脚本与压测)
redis·junit·lua
Warren982 天前
Lua 脚本在 Redis 中的应用
java·前端·网络·vue.js·redis·junit·lua