Spring AMQP同步回复和多线程

Spring AMQP synchronous reply and multithreading

我有一个项目,我将 AMQP 消息发送到 RabbitMQ 服务器。此消息是同步消息(我使用 sendAndReceive 方法)。所以,我有一个 SimpleMessageListenerContainer 配置了 RabbitTemplate 作为 MessageListener,并且响应队列是固定的(RabbitTemplate 中的 setReplyAddress)。

如果我有一个多线程服务器 (Tomcat),可以同时发送一些消息,如果响应没有按顺序到达或应用程序在已收到对另一条消息的回复?

响应与使用 correlationId header 的请求配对。客户端必须为每个请求将其设置为唯一值,并且服务器必须为响应设置相同的值 queue。这样,即使消息以任意顺序到达,客户端也可以配对消息。

来自RabbitMQ tutorial

In the method presented above we suggest creating a callback queue for every RPC request. That's pretty inefficient, but fortunately there is a better way - let's create a single callback queue per client.

That raises a new issue, having received a response in that queue it's not clear to which request the response belongs. That's when the correlationId property is used. We're going to set it to a unique value for every request. Later, when we receive a message in the callback queue we'll look at this property, and based on that we'll be able to match a response with a request. If we see an unknown correlationId value, we may safely discard the message - it doesn't belong to our requests.