spring amqp消息CorrelationIdString为null

时间:2018-01-31 07:08:14

标签: rabbitmq spring-amqp

代码如下所示我发送消息并将其设置为messageid和CorrelationIdString

rabbitTemplate.send(RabbitMQConfig.EXCHANGE_NAME, "aaa.orange.bbb",new Message(messageBody, MessagePropertiesBuilder.newInstance().setCorrelationIdString(uuid3).
                        setMessageId(uuid3).setContentType("text/x-json").build()),
                new CorrelationData(uuid3)

                );

和接收者代码

public void processMessage (Message msg) throws Exception {
//        Thread.sleep(100000);
        System.out.println("Receiver1 got message" + msg);

和日志

Receiver1 got message(Body:'hello,world1 2' MessageProperties [headers={spring_listener_return_correlation=93fbcc71-b0eb-4d33-a187-d4b27122a663}, timestamp=null, messageId=5f779051-12c5-43f1-a589-6d14430d3a52, userId=null, receivedUserId=null, appId=null, clusterId=null, type=null, correlationId=null, correlationIdString=null, replyTo=null, contentType=text/x-json, contentEncoding=null, contentLength=0, deliveryMode=null, receivedDeliveryMode=PERSISTENT, expiration=null, priority=0, redelivered=false, receivedExchange=first_exchange, receivedRoutingKey=aaa.orange.bbb, receivedDelay=null, deliveryTag=1, messageCount=0, consumerTag=amq.ctag-JbtjvUYYqlWOIsgKkOe-8A, consumerQueue=queue_a])

我的问题是为什么CorrelationIdString为null是一个问题还是没有

2 个答案:

答案 0 :(得分:1)

我们在1.6和1.7中从byte[] for correlationId迁移到String,但我们默认情况下必须坚持byte[]才能向后兼容;迁移在2.0(目前为2.0.2)中完成,correlationIdString不再是属性。

我建议转到2.0。

或者,如果您必须使用旧版本,请参阅CorrelationIdPolicy here以从byte[]切换为StringBOTH

答案 1 :(得分:0)

对我来说,我被迫重写createContainerInstance方法来更改侦听器侧的策略。

 final DefaultMessagePropertiesConverter messagePropertiesConverter = new DefaultMessagePropertiesConverter();
    messagePropertiesConverter.setCorrelationIdPolicy(DefaultMessagePropertiesConverter.CorrelationIdPolicy.STRING);

    return new SimpleRabbitListenerContainerFactory() {
        @Override
        protected SimpleMessageListenerContainer createContainerInstance() {
            final SimpleMessageListenerContainer result = new SimpleMessageListenerContainer();
            result.setMessagePropertiesConverter(messagePropertiesConverter);
            return result;
        }
    };