Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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");
}
like image 220
user3400371 Avatar asked Aug 29 '16 20:08

user3400371


People also ask

What is Springframework AMQP?

org.springframework.amqp » spring-amqpApache. Supports for AMQP-based messaging applications. It provides high-level abstractions for sending and receiving messages. Last Release on Sep 19, 2022.

What is @RabbitListener?

Annotation that marks a method to be the target of a Rabbit message listener on the specified queues() (or bindings() ). The containerFactory() identifies the RabbitListenerContainerFactory to use to build the rabbit listener container.

What is RabbitTemplate?

Helper class that simplifies synchronous RabbitMQ access (sending and receiving messages). The default settings are for non-transactional messaging, which reduces the amount of data exchanged with the broker. To use a new transaction for every send or receive set the channelTransacted flag.

What is RabbitAdmin?

public RabbitAdmin​(RabbitTemplate rabbitTemplate) Construct an instance using the provided RabbitTemplate . Use this constructor when, for example, you want the admin operations to be performed within the scope of the provided template's invoke() method.


1 Answers

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.

like image 132
Gary Russell Avatar answered Oct 10 '22 12:10

Gary Russell