I am trying to understand RabbitMQ- Spring AMQP codes. I have few doubts in the case where RabbitMQ is configured in XML files.
The sender part of xml is as shown below-
<?xml version="1.0" encoding="utf-8" ?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:rabbit="http://www.springframework.org/schema/rabbit"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/rabbit
http://www.springframework.org/schema/rabbit/spring-rabbit-1.0.xsd">
<rabbit:connection-factory id="connectionFactory"
host="localhost" username="guest" password="guest"/>
<rabbit:admin connection-factory="connectionFactory"/>
<rabbit:template id="amqpTemplate" connection-factory="connectionFactory"
exchange="tpExchange"/>
</beans>
The sender code is as shown below-
public class SpringAMQPRabbitSender {
private final static String SENDER_XML = "springamqp-rabbit-sender- context.xml";
public static void main(String[] args) throws Exception {
AmqpTemplate amqpTemplate = (AmqpTemplate)(new ClassPathXmlApplicationContext(SENDER_XML)).getBean("amqpTemplate");
int messagCount = 0;
while (messagCount < 10){
amqpTemplate.convertAndSend("tp.routingkey.1", "Message # " + messagCount++);
}
System.out.println( messagCount + " message(s) sent successfully.");
}
The receiver xml part is as shown
<?xml version="1.0" encoding="utf-8" ?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:rabbit="http://www.springframework.org/schema/rabbit"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/rabbit
http://www.springframework.org/schema/rabbit/spring-rabbit-1.0.xsd">
<rabbit:connection-factory id="connectionFactory" host="localhost"
username="guest" password="guest"/>
<rabbit:admin connection-factory="connectionFactory"/>
<rabbit:queue id ="tpQueue"/>
<rabbit:topic-exchange id="tpExchange" name="tpExchange">
<rabbit:bindings>
<rabbit:binding queue="tpQueue" pattern="tp.routingkey.1">
</rabbit:binding>
</rabbit:bindings>
</rabbit:topic-exchange>
<bean id="asyncListener" class="com.tp.spring_amqp_rabbitmq.SpringAMQPRabbitAyncListener"/>
<rabbit:listener-container id="myListenerContainer" connection-factory="connectionFactory">
<rabbit:listener ref="asyncListener" queue-names="tpQueue"/>
</rabbit:listener-container>
</beans>
The argument to convertAndSend() in sender code gives the routing key used by sender and the message to be sent. But what exchange is it being sent to?? My doubts are :
Which exchange is used by sender to send the message? Is it the one specified in tag in xml file?
If yes, then in tag - exchange = "tpExchange" specifies the name of the exchange or the id of the exchange?
In the tag - in receiver xml, is "tpQueue" the id of the queue or name of the queue?
4.Also, since the tag - in receiver xml has no name, is this code using anonymous queues??
Please help. Looked for it in few websites...couldn't get much info. Thanks.
Yes.
In the <template/>
element; the exchange name is needed, not the id.
<rabbit:queue id ="tpQueue"/>
Since the queue has no name, an auto-delete anonymous queue is used.
In the <listener/>
element, you must refer to such queues with the queues
attribute, not the the queue-names
attribute. If you want to use the queue-names
attribute you must give the queue a name.
Refer to the XSD documentation.