Search code examples
springrabbitmqspring-rabbit

Configure RabbitMQ host and port from a config.properties file


In this page, I can see that the host and port of the RabbitMQ can be configured in the XML Application Context file ass following:

<rabbit:connection-factory
id="connectionFactory" host="somehost" port="5672"/>

I want to take the host and port values from config.properties file that exist in the project for configured parameters.

Is it possible? How?

P.S. my project is in scala but I don't think this is different from Java project for the specific question


Solution

  • I found in this documentation, that it is possible by putting the config parameter in ${ configParameter }. As following:

    <rabbit:connection-factory id="rabbitConnectionFactory"    
                               host="${myproject.rabbitmq.connections.host}"
                               port="${myproject.rabbitmq.connections.port}"/>
    

    In my case this is all I needed, but as the document show, you can add some other settings:

    <rabbit:connection-factory id="rabbitConnectionFactory"    
                               host="${host}"
                               port="${port}"
                               virtual-host="${vhost}"
                               username="${username}" password="${password}" />
    

    I hope it will help some other people.