0
votes

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?

1

1 Answers

0
votes

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.