简体   繁体   中英

Sending a Message with Spring Cloud Stream and RabbitMq changes ID

I'm using Spring Cloud Stream and RabbitMq to exchange Messages between different microservices.

Thats my setup to publish a message.

public interface OutputChannels {
  static final String OUTPUT_CHANNEL = "outputChannel";

  @Output
  MessageChannel outputChannel();
}

.

@EnableBinding(OutputChannels.class)
@Log4j
public class OutputProducer {

  @Autowired
  private OutputChannels outputChannels;

  public void createMessage(MyContent myContent) {
    Message<MyContent> message = MessageBuilder
      .withPayload(myContent)
      .build();
    outputChannels.outputChannel().send(message);
    log.info("Sent message: " + message.getHeaders().getId() + myContent);
  }
}

And the setup to receive the message

public interface InputChannels {
  String INPUT_CHANNEL = "inputChannel";

  @Input
  SubscribableChannel inputChannel();
}

.

@EnableBinding(InputChannels.class)
@Log
public class InputConsumer {

  @StreamListener(InputChannels.INPUT_CHANNEL)
  public void receive(Message<MyContent> message) {

    MyContent myContent = message.getPayload();
    log.info("Received message: " + message.getHeaders().getId() + ", " + myContent);
  }
}

I am able to successfully exchange messages with this setup. I would expect, that the IDs of the sent message and the received message are equal. But they are always different UUIDs.

Is there a way that the message keeps the same ID all the way from the producer, through the RabbitMq, to the consumer?

Spring Messaging messages are immutable; they get a new ID each time they are mutated.

You can use a custom header or IntegrationMessageHeaderAccessor.CORRELATION_ID to convey a constant value; in most use cases, the correlation id header is set by the application to the ID header at the start of a message's journey.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM