I am using Eclipse, Tomcat and Rabbit MQ.
I want to be able to consume messages of a queue as soon as they hit the queue. I have managed to do this using a Java class in Eclipse (see below), but have not been able to get this working when deploying the WAR file on the Tomcat server.
package org.com.hello;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.QueueingConsumer;
public class HelloRecv {
public static void main(String[] argv)
throws java.io.IOException,
java.lang.InterruptedException {
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("172.24.3.53");
factory.setPort(6672);
factory.setUsername("user");
factory.setPassword("password");
Connection connection = factory.newConnection();
Channel channel = connection.createChannel();
channel.queueDeclare("q1", true, false, false, null);
System.out.println(" [*] Waiting for messages. To exit press CTRL+C");
QueueingConsumer consumer = new QueueingConsumer(channel);
channel.basicConsume("q1", true, consumer);
while (true) {
QueueingConsumer.Delivery delivery = consumer.nextDelivery();
String message = new String(delivery.getBody());
System.out.println(" [x] Received '" + message + "'");
}
}
}
Is there something I need to add like a web.xml file and if so, what should I add to this file?