Search code examples
rabbitmqspring-amqpspring-cloud-streamspring-rabbit

Bind exchange to exchange through Spring cloud stream with rabbitmq binder


I'm looking for a way to bind a RabbitMQ exchange to another through Spring cloud stream. I know I can bind a queue to an exchange by setting the producer.requiredGroups property:

spring.cloud.stream.bindings.foo.producer.requiredGroups=queueA queueB

Which property can I use to create an exchange-to-exchange binding?


Solution

  • Instead of adding a required group, add @Beans for the two exhanges and a @Bean for the binding.

    See the Spring AMQP documentation.

    @Bean
    public TopicExchange destinatioExchange() {
        return new TopicExchange("myDest");
    }
    
    @Bean
    public DirectExchange boundExchange() {
        return new DirectExchange("bound");
    }
    
    @Bean
    public Binding binding() {
        return BindingBuilder
                .bind(boundExchange())
                .to(destinatioExchange())
                .with("myRoutingKey");
    }
    

    and

    spring.cloud.stream.bindings.output.destination=myDest
    spring.cloud.stream.rabbit.bindings.output.producer.routing-key-expression='myRoutingKey'