How to send correlation id, into message, from sender and retrieval from receive into message header with Rabbit MQ by java

六眼飞鱼酱① 提交于 2019-12-24 00:39:46

问题


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()";
}

The question is:

  • Why the message.getMessageProperties().getCorrelationId() is NULL ? Into the send method I have inserted the correlation.
  • Correlation Id is the same thing of Correlation Data that I have inserted into convertAndSend method ?
  • How can I retrieval the correlation Id into receiver method ?

Thanks


回答1:


No; correlation data is for correlating publisher confirms for a send; it has nothing to do with the correlation Id property.

Use a MessagePostProcessor:

rabbitTemplate.convertAndSend(exchangeNameOut, message.getString(PERSISTENCE_MESSAGE_ROUTING_KEY),
                message.getString(PERSISTENCE_MESSAGE_BODY), 
    m -> {
        m.getMessageProperties().setCorrelationIdString(""+analyticRuntime.getId());
        return m;
    });

If you are not using Java8, use new MessagePostProcessor() { ... }



来源:https://stackoverflow.com/questions/41015120/how-to-send-correlation-id-into-message-from-sender-and-retrieval-from-receive

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!