With the help of http://ndpar.blogspot.com/2010/08/wo...in-spring.html and Mark, I am able to run a sample finally
Here is the code
Admin.java
=========
package rabbitMQSampleApp;
import org.springframework.amqp.core.AmqpAdmin;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.context.ConfigurableApplicatio nContext;
import org.springframework.context.support.ClassPathXmlAp plicationContext;
public class Admin {
public static void main(String[] args) {
ConfigurableApplicationContext context = new ClassPathXmlApplicationContext("rabbitMQSampleApp/applicationContext-admin.xml");
new Admin().declareEverything(context.getBean(RabbitAd min.class));
}
private void declareEverything(AmqpAdmin admin) {
Queue queue = new Queue("sampleQueue");
DirectExchange exchange = new DirectExchange("myExchange");
Binding binding = new Binding(queue, exchange, "myRoutingKey");
admin.declareQueue(queue);
admin.declareExchange(exchange);
admin.declareBinding(binding);
}
}
Consumer.java
===========
package rabbitMQSampleApp;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageListener;
public class Consumer implements MessageListener {
@Override
public void onMessage(Message message) {
System.out.println("Received message: " + message);
}
}
Producer.java
===========
package rabbitMQSampleApp;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.context.ConfigurableApplicatio nContext;
import org.springframework.context.support.ClassPathXmlAp plicationContext;
public class Producer {
public static void main(String[] args) throws Exception {
ConfigurableApplicationContext context = new ClassPathXmlApplicationContext("rabbitMQSampleApp/rabbitConfiguration.xml");
AmqpTemplate amqpTemplate = context.getBean(AmqpTemplate.class);
/* We have retrieved an amqpTemplate from the context.
* Producer can simply use that amqp template to publish anything.
* Question is that how and why this amqp template object
* sends the notification only on queue which we have declared through Admin class
* where is this mapping present ? */
amqpTemplate.convertAndSend("Hello World");
System.out.println("Message Sent");
}
}
applicationContext-admin.xml
=======================
<?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="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<!-- Define a connectionFactory -->
<bean id="connectionFactory" class="org.springframework.amqp.rabbit.connection. SingleConnectionFactory">
<constructor-arg value="localhost" />
<property name="username" value="guest" />
<property name="password" value="guest" />
</bean>
<!-- Tell the Admin bean about that connectionFactory and initialize it, create a queue and an exchange on Rabbit Broker using the RabbitTemplate provided by Spring framework-Rabbit APIs -->
<bean id="Admin" class="org.springframework.amqp.rabbit.core.Rabbit Admin">
<constructor-arg ref="connectionFactory" />
</bean>
</beans>
rabbitConfiguration.xml
=================
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns="http://www.springframework.org/schema/p"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schem...-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<!-- Define a connectionFactory -->
<bean id="connectionFactory" class="org.springframework.amqp.rabbit.connection. SingleConnectionFactory">
<constructor-arg value="localhost" />
<property name="username" value="guest" />
<property name="password" value="guest" />
</bean>
<bean id="rabbitTemplate" class="org.springframework.amqp.rabbit.core.Rabbit Template"
p:connectionFactory-ref="connectionFactory"
p:routingKey="myRoutingKey"
p:exchange="myExchange" />
<!-- Declare a messageListener bean -->
<bean id="messageListener" class="rabbitMQSampleApp.Consumer" />
<!-- pass the above messageListener object and other settings to SimpleMessageListenerContainer in Rabbit API-->
<bean class="org.springframework.amqp.rabbit.listener.Si mpleMessageListenerContainer"
p:connectionFactory-ref="connectionFactory"
p:queueName="sampleQueue"
p:messageListener-ref="messageListener" />
</beans>



="http://www.springframework.org/schema/p"
Reply With Quote
V