Skip to content

Spring cloud stream RabbitMQ routing messages dynamically #1009

@abhdeb

Description

@abhdeb

I 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.

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions