Sunday, 5 July 2015

Understanding xml attributes in rabbit config


In enterprise applications, we will not be creating the queues. We will have Rabbit admin who will create the queues with the required attributes and we will be using them.

In the rabbit configuration xml file, we declare connection factory, rabbit template, queues, bindings, listener etc, in order to transfer messages from producer to consumer application. Among these few attributes are optional depending on the design or development approach of your application. Lets discuss each of them in detail here

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
 <property name="location">
  <value>classpath:RabbitConfig.properties</value>
 </property>
</bean>

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

<rabbit:admin connection-factory="rabbitConnectionFactory" />

<rabbit:template id="rabbitTemplate"
 connection-factory="rabbitConnectionFactory" exchange="${rabbit.exchange}" />

<rabbit:queue name="${rabbit.queue}" />

<rabbit:fanout-exchange name="${rabbit.exchange}">
 <rabbit:bindings>
  <rabbit:binding queue="${rabbit.queue}" />
 </rabbit:bindings>
</rabbit:fanout-exchange>

No matter what the approach the following are mandatory:
  1. rabbit connection factory
  2. rabbit template
  3. rabbit queue declaration
Rabbit Connection factory: Creates an instance of rabbit CachingConnectionFactory (org.springframework.amqp.rabbit.connection)

Rabbit Template: Creates a rabbit template (org.springframework.amqp.rabbit.core) for convenient access to the message broker.

Rabbit Queue: Creates a queue. Uses an existing queue with the same name if it exists on the broker, or else declares a new one

Note that in order for rabbit queue attribute to create a new queue, we would need rabbit admin attribute
Rabbit Admin: Creates an instance of rabbit RabbitAdmin (org.springframework.amqp.rabbit.core)
So if we already have queues configured, then we need not have to rabbit admin attribute. Our application will work just fine. But if we add this attribute then we need to very careful while creating queues, because if we enter wrong queue, a new queue will be created which may cause errors


No comments:

Post a Comment