Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of Springboot 2.x RabbitTemplate default message persistence

2025-01-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly shows you the "Springboot 2.x RabbitTemplate default message persistence example analysis", the content is easy to understand, well-organized, hope to help you solve your doubts, the following let the editor lead you to study and study the "Springboot 2.x RabbitTemplate default message persistence example analysis" this article.

Preface

Previously, when Java directly tested mq message persistence, the following configuration was adopted to achieve message persistence:

/ / message persistence test Builder builder = new Builder (); builder.deliveryMode (2); BasicProperties properties = builder.build (); channel.basicPublish (", queue_name, properties, string.getBytes ())

The source code information in BasicProperties is:

Public static class BasicProperties extendscom.rabbitmq.client.impl.AMQBasicProperties {private String contentType;// message type such as: text/plain private String contentEncoding;// encoded private Map headers; private Integer deliveryMode;//1:nonpersistent non-persistent 2:persistent persistent private Integer priority;// priority private String correlationId; private String replyTo;// feedback queue private String expiration;//expiration expiration time private String messageId Private Date timest private String type; private String userId; private String appId; private String clusterId;...

Refer to blogs: message response (autoAck), queue persistence (durable), and message persistence

Springboot test

The above configuration is the code logic that needs to be written when Java is tested directly. If springboot configuration is adopted, the default message persistence will occur.

For test cases, you can refer to the following blogs:

SpringBoot integrates RabbitMQ to implement message confirmation mechanism

Testing phenomenon

First comment the message consumer code. Execute the interface to create messages and store them in the queue.

Source code analysis

If you want to know why messages are automatically persisted, you need to focus on the rabbitTemplate.convertAndSend (exchange,routingKey,msg) method.

From the source code execution logic, you can see:

The message provided by rabbitTemplate is loaded into the queue, and the data type is Object, but in its source code logic, the Object message type is processed by this.convertMessageIfNecessary (object), and the object object type is transformed into Message object type.

It can be seen here that rabbitTemplate configures operations such as message persistence by default in order to make it easier for developers to deal with data.

Now, let's see what convertMessageIfNecessary (object) has done.

Determine whether the current data type is a Message type.

If it is a Message type, it is forcibly converted to Message directly.

If not, the new method is executed and converted once.

The conversion process is as follows:

The rest won't go any further. Where is the default configuration for persistence?

Go back to the original convertAndSend execution method.

This parameter is generated when the class is created and loaded, and its data is as follows:

Association

That is, in convertMessageIfNecessary, it is determined whether the passed parameter type is of type Message, and if not, it needs to be wrapped again.

If you do not want to set message persistence, the data type passed is Message!

The above is all the content of the article "sample Analysis of Springboot 2.x RabbitTemplate default message persistence". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report