marco ughetti
marco ughetti

Reputation: 1

Spring AMQP: Unable to set MessageProperties using RabbitMessagingTemplate

I have tried this to set expiration time of a message and converting and sending it using RabbitMessagingTemplate:

Map<String,Object> headers = new HashMap<>();
headers.put("expiration", "20000");
rabbitMessagingTemplate.convertAndSend(exchange.getName(),routingKey, event, headers);

but it does not work because expiration shall be set as a property and NOT as a header. Unfortunately RabbitMessagingTemplate does not provide a way to pass message properties but only headers. On the other hand I need to convert the message becasue I use JecksonMessageConverter. How can I add message properties before sending the message with RabbitMessagingTemplate?

Upvotes: 0

Views: 1574

Answers (1)

Gary Russell
Gary Russell

Reputation: 174574

Add a MessagePostProcessor to the underlying RabbitRemplate's beforePublishPostProcessors.

I can't look at the code right now but I am surprised it's not mapped.

EDIT

Use header name amqp_expiration. See AmqpHeaders.EXPIRATION. It is mapped to the message property.

Unrecognized headers are mapped to headers.

EDIT2

In any case, given your requirements, you might be better off not using the RabbitMessagingTemplate but use the RabbitTemplate and a MessagePostProcessor instead; it will be a little more efficient...

rabbitTemplate.convertAndSend(exchange.getName(), routingKey, event, m -> {
    m.getMessageProperties().setExpiration(...);
    ...
    return m;
};

Upvotes: 2

Related Questions