7

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");
}
Gary Russell
  • 166,535
  • 14
  • 146
  • 179
user3400371
  • 109
  • 1
  • 9

1 Answers1

18

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.

Gary Russell
  • 166,535
  • 14
  • 146
  • 179
  • The second option is exactly what I needed. Thank you, sir. – LethalLima Jul 20 '18 at 20:45
  • How to get the headers from the message (not the properties, but the actual headers)? `message.getMessageProperties()` returns the properties. – Maroun Apr 16 '19 at 12:13
  • When I publish a message from the RabbitMQ Management UI, I put in the "Headers" a simple header, but can't get it in my listener. – Maroun Apr 16 '19 at 12:14
  • 1
    You shouldn't ask new questions in comments; it doesn't help others find questions and answers. `message.getMessageProperties().getHeaders()`. – Gary Russell Apr 16 '19 at 12:35
  • 1
    just in case someone stumbles on this answer, the link to the documentation is broken and one should use this instead: https://docs.spring.io/spring-amqp/reference/html/#async-annotation-driven – Nuno Nelas Jul 20 '21 at 07:21