Spring cloud stream RabbitMQ routing messages dynamically
See original GitHub issueI have implemented the example as shown here Dynamic Destinations Spring Dynamic Destination
In the rabbitmq, it is creating an exchange dynamically, but there is no option to provide binding or routing key. My requirement is to send a message to this dynamically created exchange with a routing key. How would i need to implement this to setup the routing key?
@Component public class DDProducerBean {
@Autowired
private BinderAwareChannelResolver poChannelResolver = null;
public void publish(DDSocketVO ddSocketVO) throws Exception {
this.poChannelResolver.resolveDestination(ddSocketVO.getDestination()).send(MessageBuilder.withPayload(new ObjectMapper().
setVisibility(PropertyAccessor.FIELD, Visibility.ANY).
writeValueAsString(ddSocketVO)).build());
}
My requirement is to route messages dynamically at runtime based on the payload. So i need a way to set the routing key at run time. As a result i can’t use producer.routingKeyExpression, which is done in config or properties or yml, and not dynamically at run time, so that won’t serve the purpose.
This implementation is specific for spring.cloud.stream.dynamicDestinations where the BinderAwareChannelResolver takes care of dynamically creating/binding the outbound channel for these dynamic destinations. But it doesn’t talk about how to bind the outbound channel to a queue and how to specify a routing key expression. As these are dynamic channels/ producers, we can’t use producer.routingKeyExpression as the producer is created dynamically.
Issue Analytics
- State:
- Created 6 years ago
- Comments:9 (5 by maintainers)

Top Related StackOverflow Question
Based on your suggestion we have now created the binding using AmqpAdmin and a routing key and it is working as expected, thank you.
Just curious as why routing key api is not available in dynamic destinations? In this scenario every time a new exchange is created and then using AmqpAdmin bind it to the defualt exchange with the routing key, which seems like a workaround. Ideally when we send a message, if we can send the routing key, then exchange can route it according without having to create new exchange for each request and then bind them using AmqpAdmin.
Producers are not responsible for binding queues; consumers are.
There is some support for a producer binding queues using the
requiredGroupsproperty, but that is for fixed destinations; there is no such support for dynamic destinations.You can programmatically declare queues and bindings using the
AmqpAdminbean after you have resolved the destination and before sending messages.