Search code examples
rabbitmqspring-amqpspring-rabbit

Spring amqp: How can I read MessageProperties in MessageListenerAdapter


handleMessage method does not get the message from queue if I add MessageProperties in its signature. It works fine if there is no MessageProperties.

How can I get MessageProperties in handleMessage of MessageListenerAdapter?

public class EventMessageAdapter {

  public void handleMessage(MessageProperties messageProperties, Event event)    {
  ...
  String id = messageProperties.getHeaders().get("key");
}

Solution

  • You can't do it with the listener adapter.

    Use the newer-style @RabbitListener mechanism docs here.

    You can use various signatures...

    @RabbitListener(queues = "foo")
    public void foo(Event event, @Header("foo") String fooHeader, 
               @Header("bar") Integer barHeader) {...}
    

    or

    @RabbitListener(queues = "bar")
    public void bar(Event event, Message message) {...}
    

    In the second case you can get all the message properties via message.getMessageProperties().

    You need a container factory. Spring Boot creates one automatically for you if the starter is on the classpath.