Search code examples
spring-bootrabbitmqspring-cloudspring-cloud-stream

How to make spring cloud connect to an existing RabbitMQ Exchange and queue


So I have a spring boot appplication and I am using spring cloud to connect to my RabbitMQ server.I read the documentation but some things are still vague to me .For now my application creates the queue and destroys it on app start and stop which is not what i want. So my question is: what is the binding bame in the documentatiin and how should the configuration file look like? I'm trying to avoid the use of any annotations and only doing it through the configuration file.

I tried following some examples and thoroughly read the documentation


Solution

  • See the documentation: https://docs.spring.io/spring-cloud-stream/docs/current/reference/html/spring-cloud-stream-binder-rabbit.html#_using_existing_queuesexchanges

    If you have an existing exchange/queue that you wish to use, you can completely disable automatic provisioning as follows, assuming the exchange is named myExchange and the queue is named myQueue:

    spring.cloud.stream.bindings.<binding name>.destination=myExchange
    
    spring.cloud.stream.bindings.<binding name>.group=myQueue
    
    spring.cloud.stream.rabbit.bindings.<binding name>.consumer.bindQueue=false
    
    spring.cloud.stream.rabbit.bindings.<binding name>.consumer.declareExchange=false
    
    spring.cloud.stream.rabbit.bindings.<binding name>.consumer.queueNameGroupOnly=true
    

    If you want the binder to provision the queue/exchange, but you want to do it using something other than the defaults discussed here, use the following properties. Refer to the property documentation above for more information.

    spring.cloud.stream.rabbit.bindings.<binding name>.consumer.bindingRoutingKey=myRoutingKey
    
    spring.cloud.stream.rabbit.bindings.<binding name>.consumer.exchangeType=<type>
    
    spring.cloud.stream.rabbit.bindings.<binding name>.producer.routingKeyExpression='myRoutingKey'