Service asynchronous communication-message converter

Please add image description
Personal business card:

Blogger: Drunkardᝰ.
Personal profile: Indulge in wine, and use the energy of wine to fight for a future.
This article is inspirational: When three of us travel together, we must be my teacher.

Please add image description
This project is based on Java "SpringCloud Microservice Technology Stack" by the dark horse programmer of station B , SpringCloud+RabbitMQ+Docker+Redis+search+distributed

[SpringCloud+RabbitMQ+Docker+Redis+search+distributed, detailed system explanation springcloud microservices technology stack course | Dark horse programmer Java microservices] Click to watch

3. SpringAMQP

6.Message converter

In Spring AMQP, the message converter is an important component that handles the serialization and deserialization of messages. Spring AMQP supports multiple message formats, such as JSON, XML, etc., so a message converter is required to convert messages between producers and consumers.

In Spring AMQP, the MessageConverter interface is usually used to implement message converters. Spring AMQP provides several default message converters, such as StringMessageConverter, MappingJackson2MessageConverter, XmlMessageConverter, etc.

The role of using message converters in Spring AMQP:

  • Implement serialization and deserialization of messages: In RabbitMQ, producers need to convert objects into messages and write them into the message queue, and consumers need to convert messages back into objects for reading. Therefore, through message converters, mutual conversion between objects and messages can be achieved.
  • Supports multiple message formats: Spring AMQP provides multiple message converters by default, such as MappingJackson2MessageConverter for processing JSON format, and XmlMessageConverter for processing XML format. This means that Spring
    AMQP can support multiple message formats to meet different business needs.
  • Improve code maintainability and readability: Using a message converter, you can separate the object's serialization and deserialization logic from the business code and encapsulate it into a separate class. Doing so can improve the maintainability and readability of the code, because the serialization and deserialization logic is extracted into a specialized class to facilitate subsequent modification and maintenance.
  • Simplify the development process: By using message converters, developers do not need to care about the underlying serialization and deserialization details and only need to focus on business logic. This can greatly simplify the development process and improve development efficiency.

Therefore, the main role of using message converters in Spring AMQP is to implement message serialization and deserialization, support multiple message formats, improve code maintainability and readability, and simplify the development process.

Test sending Object type message

Note: In the sending method of SpringAMQP, the type of received message is Object, which means that we can send messages of any object type, and SpringAMQP will help us serialize them into bytes and send them.

We use @Bean to declare a queue in the consumer:

@Bean
public Queue objectQueue(){
    
    
    return new Queue("object.queue");
}

Send a message in publisher to test:

@Test
public void testObject() {
    
    
    String queueName = "object.queue";
    Map<String, Object> map = new HashMap<>();
    map.put("name", "jack");
    map.put("age", 2);
    rabbitTemplate.convertAndSend(queueName, map);
}

message converter

Spring's processing of message objects is handled by org.springframework.amqp.support.converter.MessageConverter. The default implementation is SimpleMessageConverter, which completes serialization based on JDK's ObjectOutputStream.

If you want to modify it, you only need to define a Bean of type MessageConverter. It is recommended to use JSON serialization. The steps are as follows:
Because both publisher and consumer require Json dependencies, we introduce dependencies in the parent class service.

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

We declare MessageConverter in the publisher service:

@Bean
public MessageConverter jsonMessageConverter() {
    
    
    return new Jackson2JsonMessageConverter();
}

We define MessageConverter in the consumer service:

@Bean
public MessageConverter jsonMessageConverter() {
    
    
    return new Jackson2JsonMessageConverter();
}

Then define a consumer to listen to the object.queue queue and consume messages:

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

Insert image description here

Summarize:

How is message serialization and deserialization implemented in SpringAMQP?
Implemented using MessageConverter, the default is JDK serialization.
Note that the sender and receiver must use the same MessageConverter.

Guess you like

Origin blog.csdn.net/m0_65144570/article/details/133106769