简体   繁体   English

Spring AMQP:CorrelationId在发送和接收消息的时刻之间发生变化

[英]Spring AMQP : CorrelationId changes between moment of sending and receiving a message

While I was testing the send and receive methods which I created for my project I ran into a strange problem. 当我测试我为项目创建的发送和接收方法时遇到了一个奇怪的问题。 When I send a certain message using a correlationId that is based on a UUID object, the receiving party gets a slightly modified version of this correlationId (which cannot be deserialised). 当我使用基于UUID对象的correlationId发送某个消息时,接收方获得此correlationId的略微修改版本(不能反序列化)。

On the sending side I do this: 在发送方面,我这样做:

MessageProperties properties = new MessageProperties();
properties.setCorrelationId(MessageSerializer.serialize(UUID.randomUUID().toString()));

On my last test the UUID generated was: " d4170243-9e7e-4c42-9168-f9da4debc5bb " 在我的最后一次测试中,生成的UUID是:“ d4170243-9e7e-4c42-9168-f9da4debc5bb

This generates the following correlationId (when serialized): 这会生成以下correlationId(序列化时):

[-84, -19, 0, 5, 116, 0, 36, 100, 52, 49, 55, 48, 50, 52, 51, 45, 57, 101, 55, 101, 45, 52, 99, 52, 50, 45, 57, 49, 54, 56, 45, 102, 57, 100, 97, 52, 100, 101, 98, 99, 53, 98, 98]

When I receive the message on the other side this id is slightly changed: 当我收到另一方的消息时,这个ID略有改变:

[-17, -65, -67, -17, -65, -67, 0, 5, 116, 0, 36, 100, 52, 49, 55, 48, 50, 52, 51, 45, 57, 101, 55, 101, 45, 52, 99, 52, 50, 45, 57, 49, 54, 56, 45, 102, 57, 100, 97, 52, 100, 101, 98, 99, 53, 98, 98]

When using the RabbitMQ management plugin I noticed that the id already changed upon arrival at the queue. 当使用RabbitMQ管理插件时,我注意到id已经在到达队列时发生了变化。

在此输入图像描述

Tracing my code on the sending side brings me to the send option of the RabbitTemplate class. 在发送端跟踪我的代码将我带到RabbitTemplate类的send选项。

RabbitTemplate template = new RabbitTemplate(connection);
template.setExchange("amq.direct");
template.setRoutingKey("some.route");
template.send(message);

But I can't figure out what's causing this problem. 但我无法弄清楚导致这个问题的原因。 I guess it's just me using the correlationId option the wrong way. 我想这只是我错误的方式使用correlationId选项。 Could someone help me out? 有人可以帮帮我吗?

Appreciate it. 欣赏它。

Explanation is the following: 说明如下:

  1. You serialize the UUID string to a byte array 您将UUID字符串序列化为字节数组
  2. Your serialization prepends non ascii character to this array ( [-17, -65, -67, -17, -65, -67, 0, 5, 116, 0, 36,...] ) 你的序列化为这个数组添加非ascii字符( [-17, -65, -67, -17, -65, -67, 0, 5, 116, 0, 36,...]
  3. The reference documentation states that the correlation id is a shortstr. 参考文档声明相关id是shorttr。 RabbitMQ client converts this byte array to a string using new String(yourArray , "UTF-8") . RabbitMQ客户端使用new String(yourArray , "UTF-8")将此字节数组转换为字符串。
  4. The non ascii character "corrupt" the conversion from byte[] to string 非ascii字符“损坏”从byte []到string的转换

You can get the same result with the following code: 您可以使用以下代码获得相同的结果:

new String(MessageSerializer.serialize(UUID.randomUUID().toString()) , "UTF-8").getByte("UTF-8")

Which will return: 哪个将返回:

[-17, -65, -67, -17, -65, -67, 0, 5, 116, 0, 36, 100, 52, 49, 55, 48, 50, 52, 51, 45, 57, 101, 55, 101, 45, 52, 99, 52, 50, 45, 57, 49, 54, 56, 45, 102, 57, 100, 97, 52, 100, 101, 98, 99, 53, 98, 98]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM