java - 如何使用Rabbit MQ将相关ID从发送方发送到消息中,并从接收方检索到消息头中 [英] How to send correlation id, into message, from sender and retrieval from receive into message header with Rabbit MQ by java
问题描述
我使用 Rabbit MQ 发送和接收 JSON 消息.
我已经使用 RabbitTemplate 实例和 convertAndSend 方法实现了一个应用程序来向交换发送消息,如下所示:
I have used Rabbit MQ to send and receive a JSON message.
I have implemented an application using the RabbitTemplate instance with convertAndSend method to send a message to the exchange as below:
rabbitTemplate.convertAndSend(exchangeNameOut, message.getString(PERSISTENCE_MESSAGE_ROUTING_KEY),
message.getString(PERSISTENCE_MESSAGE_BODY), new CorrelationData(""+analyticRuntime.getId()));
在检索消息时,接收方,我实现了以下方法:
While to retrieval a message, side receiver, I have implemented the followed method:
@RabbitListener(queues = "${rabbit.queue.mail.name}",containerFactory = "rabbitListenerContainerFactory")
public void processMailMessage(Message message) {
log.info("ENTER [processMailMessage] ");
Mail mail;
JSONObject messageBody = new JSONObject(new String(message.getBody()));
String asset = "" + messageBody.get(Constants.PERSISTENCE_MESSAGE_ASSET_ID_KEY);
String body = "" + messageBody.get(Constants.PERSISTENCE_MESSAGE_EVENT_KEY);
String alarms = "";
log.info("[processMailMessage] message.getMessageProperties().getCorrelationId() : " + message.getMessageProperties().getCorrelationId()";
}
问题是:
- 为什么 message.getMessageProperties().getCorrelationId() 是 NULL ?我在发送方法中插入了相关性.
- Correlation Id 与我插入到 convertAndSend 方法中的 Correlation Data 相同吗?
- 如何将相关 ID 检索到接收器方法中?
谢谢
推荐答案
否;相关数据用于关联发布者确认发送;它与相关 Id 属性无关.
No; correlation data is for correlating publisher confirms for a send; it has nothing to do with the correlation Id property.
使用 MessagePostProcessor
:
rabbitTemplate.convertAndSend(exchangeNameOut, message.getString(PERSISTENCE_MESSAGE_ROUTING_KEY),
message.getString(PERSISTENCE_MESSAGE_BODY),
m -> {
m.getMessageProperties().setCorrelationIdString(""+analyticRuntime.getId());
return m;
});
如果您没有使用 Java8,请使用 new MessagePostProcessor() { ... }
If you are not using Java8, use new MessagePostProcessor() { ... }
这篇关于java - 如何使用Rabbit MQ将相关ID从发送方发送到消息中,并从接收方检索到消息头中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!