Spring integrated RabbitMQ- message converter

In the first two chapters, we introduced how we send messages, how to receive messages, and some of the methods used under Spring-AMQP.

In fact, there is an important part of the transmission and reception, which is the message conversion. Not all messages are like “Hello world”, which is converted into an array of bytes via a getBytes, then sent out and then converted to a string in the receiver. In actual production, we will encounter different application scenarios, so this is about the message converter between the sending end and the receiving end.

Message converter

A message conversion can be made in two directions, one is to transfer the contents of an object, such as an object, into a specified format, and the other is to convert the received message into a received object.
amqpTemplate provides two simple ways to do this

Spring integrated RabbitMQ- message converter
image.png
  • The first method is to turn a Object object into a Mesaage, and at the same time, the MessageProperties can also be converted to part of the Message
  • The second way is to turn messages into Object objects at the receiving end
  • The composition of Message has been described previously, which includes the contents of a message, a binary byte array, and the attribute information object of a message. So, in the transmission process, it is in bytes

In the Spring integrated RabbitMQ- messaging article, we said that there’s another convertAndSend method in addition to the send method. This method is simpler and more efficient than the previously introduced send and send method. We don’t need to know the object or instance of the message being sent, just pass the message to Object.

Spring integrated RabbitMQ- message converter
image.png

So why do these convertAndSend achieve this effect, we can look at the convertAndSend source code?

Spring integrated RabbitMQ- message converter
image.png

The convertAndSend method will first call the above heavy, the inside is actually a call to the send method, but the most important part in the convertMessageIfNecessary method, let’s take a look at the implementation of this method

Spring integrated RabbitMQ- message converter
image.png

Through a three yuan expression, first determine whether it is a Message type, if it is strong to Message type and return, if not, call the above described MessageConverter toMessage method to Message type.

Accordingly, the receiver also has the corresponding transformation and the accepted method

Spring integrated RabbitMQ- message converter
image.png

SimpleMessageConverter

The default implementation class for MessageConverter is SimpleMessageConverter. If RabbitTemplate does not explicitly configure the message converter, this is the default SimpleMessageConverter.

Spring integrated RabbitMQ- message converter
image.png
  • If the received message starts with “text”, such as “text/plain”, the converter checks its encoding format and converts binary bytes to strings or other forms of information according to the encoding format
  • If the encoding format above is not set, then the default will be decoded in the “UTF-8” format, which can be set by method defaultCharset
  • If the received message contains the message type “application/x-java-serialized-object”, then the converter is to serialize the binary byte array to a Java object
  • In general, Java serialization is not recommended because it relies too much on the sender and receiver, and will later say it can be more flexible through the JSON format
  • The same holds true for sending messages, transforming strings or serialized objects into binary byte arrays

Jackson2JsonMessageConverter

As mentioned above, you can use JSON to serialize and de – serialize, and accordingly we have Jackson2JsonMessageConverter message converters.

Spring integrated RabbitMQ- message converter
image.png

We can set the &lt in the XML configuration file; rabbit:template> the message-converter attribute in the tag is our declared message converter, which will be carefully described in the later practice series.