Is there any way to get routing key in listener

Viewed 2540

I have a Spring Boot application which uses RabbitMQ for async calls. In listener(consumer), I would like to get routing key from somewhere ? Is it possible ? Could you please help me ?

Thanks.

3 Answers

You can get it from MessageProperties. There is a link to full documentation(find getReceivedRoutingKey method): https://docs.spring.io/spring-amqp/api/org/springframework/amqp/core/MessageProperties.html

Also a short piece of code:

@RabbitListener(queues = "${spring.rabbitmq.template.queue}")
public void listen(Message message) {
    String receivedRoutingKey = message.getMessageProperties().getReceivedRoutingKey();
    log.info("Routing key: {}", receivedRoutingKey);
}
Related