RabbitMQ之消息转换器

阿里云国内75折 回扣 微信号:monov8
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6

前言大家好我是小威24届毕业生曾经在某央企公司实习目前在某税务公司。本篇文章将记录和分享RabbitMQ消息转换器的知识点。
本篇文章记录的基础知识适合在学Java的小白也适合复习中面试中的大佬🤩🤩。
如果文章有什么需要改进的地方还请大佬不吝赐教👏👏。
小威在此先感谢各位大佬啦~~🤞🤞
在这里插入图片描述

🏠个人主页小威要向诸佬学习呀
🧑个人简介大家好我是小威一个想要与大家共同进步的男人😉😉
目前状况🎉24届毕业生曾经在某央企公司实习目前在某税务公司实习👏👏

💕欢迎大家这里是CSDN我总结知识的地方欢迎来到我的博客我亲爱的大佬😘

在这里插入图片描述

以下正文开始

在这里插入图片描述

在SpringAMQP的发送方法中接收消息的类型是Object也就是说我们可以发送任意对象类型的消息SpringAMQP会帮我们序列化为字节后发送。

我们以实际例子来做一个演示首先在项目中的FanoutConfig配置类声明一个新的队列

package cn.itcast.mq.config;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.FanoutExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class FanoutConfig {
    @Bean
    public Queue objectQueue(){
        return new Queue("object.queue");
    }
}

接着我们在生产者模块中编写发送消息的测试代码

package cn.itcast.mq.spring;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.HashMap;
import java.util.Map;

@SpringBootTest
@RunWith(SpringRunner.class)
public class SpringAmqpTest {
    @Autowired
    private RabbitTemplate rabbitTemplate;
    @Test
    public void testSendObjectQueue(){
        Map<String,Object> msg =new HashMap<>();//创建一个map集合
        msg.put("name","小威");
        msg.put("age",21);
        rabbitTemplate.convertAndSend("object.queue",msg);//发送消息
    }
}

消息发送代码编写完成后重启项目打开RabbitMQ的虚拟机对应地址可以看到我们编写的name和age出现了特殊的情况
在这里插入图片描述

这是因为RabbitMQ只支持字节的序列化方式spring支持发送object对象消息由上图可以看到content_type的转化方式spring将封装的map对象序列化了一长串字符串。

那么我们该如何解决这个问题呢

在这里插入图片描述

首先Spring的消息对象的处理是由org.springframework.amqp.support.converter.MessageConverter来处理的。默认实现是SimpleMessageConverter基于JDK的ObjectOutputStream完成序列化。

因此我们需要定义一个MessageConverter 类型的Bean可以使用json的方式序列化在父工程中引入json的依赖

        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
        </dependency>

然后在生产者模块的启动类中声明bean

package cn.itcast.mq;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.Jackson2XmlMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
@SpringBootApplication
public class PublisherApplication {
    public static void main(String[] args) {
        SpringApplication.run(PublisherApplication.class);
    }
    @Bean
    public MessageConverter jsonMessageConverter(){
        return new Jackson2JsonMessageConverter();
    }
}

再次重启生产者模块可以看到我们想要的消息对象结果了
在这里插入图片描述
对于消费者也同理首选引入依赖接着在启动类中编写和生产者一样的代码最后定义一个消费者监听该队列

    @RabbitListener(queues = "object.queue")
    public void listenObjectQueue(Map<String,Object> msg){
        System.out.println("接受到object.queue的消息"+msg);
    }

启动消费者模块可以看到控制台输出的结果
在这里插入图片描述
本篇文章就先分享到这里了后续会继续分享其他方面的知识感谢大佬认真读完支持咯~
在这里插入图片描述

文章到这里就结束了如果有什么疑问的地方请指出诸佬们一起讨论🍻
希望能和诸佬们一起努力今后进入到心仪的公司
再次感谢各位小伙伴儿们的支持🤞

在这里插入图片描述

阿里云国内75折 回扣 微信号:monov8
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6
标签: RabbitMQ