how can i get correlationId?

↘锁芯ラ 提交于 2019-12-10 18:49:27

问题


On my project(spring-rabbit..), Set fixed ReplyTo queue on template, I use convertSendAndReceive method for RPCs.

I understand that mekes correlationId automatically.

Can I set correlationId before using that method?

Here is Template.

@Bean   
public RabbitTemplate rabbitTemplate() {
    RabbitTemplate template = new RabbitTemplate(connectionFactory());
    template.setMessageConverter(jsonMessageConverter());       
    template.setRoutingKey(AmqpConstants.JOB_QUEUE_NAME);
    template.setExchange(AmqpConstants.JOB_EXCHANGE_NAME);
    template.setQueue(AmqpConstants.JOB_QUEUE_NAME);    
    template.setReplyQueue(new Queue(AmqpConstants.JOB_REPORT_QUEUE_NAME)); 

    template.setReplyTimeout(replyTimeoutMilliseoconds);

    return template;
}

Code

jobReport = (ApiJobReport)rabbitTemplate.convertSendAndReceive(
                AmqpConstants.JOB_EXCHANGE_NAME, 
                AmqpConstants.JOB_QUEUE_NAME, 
                jobMessage, new MessagePostProcessor() {

                    @Override
                    public Message postProcessMessage(Message message) throws AmqpException {
                        message.getMessageProperties().setCorrelationId("correlationid1234".getBytes());
                        return message;
                    }
                }); 

In postProcessMessage, setting correlationId as "correlationid1234". But RabbitMQ Management shows below.

Message properties :

correlation_id: 23316fe6-0c15-46f6-9bed-5f3abf22a594

priority: 0 delivery_mode: 2 headers:
TypeId: com.example.model.apijob content_encoding: UTF-8 content_type: application/json

As shown result, set correlationId has changed to RabbitTemplate messageTag value(UUID). Im watching RabbitTemplate source but I dont understand why it makes change to correlationId if correlationKey is null.

RabbitMQ Management


回答1:


If you use sendAndReceive() (rather then convertSendAndReceive()); if you set a correlationId message property, the template saves it off; uses its own correlationId in the outbound message and restores the original correlationId in the reply message.

It's not clear what you mean in the context of convertSendAndReceive(); you can't set the correlationId before you call it because there is no message until the conversion takes place.

You could set it in a MessagePostProcessor but it won't achieve very much.

Perhaps if you could explain what you are trying to do, I can make some other suggestion.



来源:https://stackoverflow.com/questions/35053098/how-can-i-get-correlationid

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