I have a service, that receives AMQP messages. This service is bound to a queue, which receives all messages which match a set of routing keys.
My set up is as follows:
... private SomeController controller; @Autowired private SimpleMessageListenerContainer receiverContainer; @Bean public IntegrationFlow inboundFlow(){ var adpater = Amqp.inboundAdapter(receiverContainer); return IntegrationFlows.from(adapter) // some transformations .handle(controller, "processMessage") .get(); }
This already works fine. However, now I want to handle a message with different controllers, depending on a header attribute. In this case I’d like to have a controller for each routing key. Is it also a good idea to use a single queue with multiple routing keys only to handle it differently for each key?
Advertisement
Answer
It is really legit to have several bindings between an exchange and a single queue.
See more info in this tutorial: https://www.rabbitmq.com/tutorials/tutorial-four-spring-amqp.html.
The Amqp.inboundAdapter()
relies on the DefaultAmqpHeaderMapper.inboundMapper()
by default which populates for us an AmqpHeaders.RECEIVED_ROUTING_KEY
message header before producing. So, you indeed can use a route(Message.class, m -> m.getHeaders().get(AmqpHeaders.RECEIVED_ROUTING_KEY))
with appropriate channelMapping()
for the routing key value.