1

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?

Naj
  • 11
  • 3
  • java servlet YOur program doesn't work in a war because it is a console application. Do you have to consume message from a java servlet? – Gabriele Santomaggio May 15 '15 at 13:28
  • Hi Gas, no it doesn't have to be a java servlet. All I'm looking to do is consume messages off a queue using some code on a tomact server, but not sure what the best way to do this is – Naj May 15 '15 at 13:46
  • Tomcat is a servlet container, so you have to have a servlet. – Gabriele Santomaggio May 16 '15 at 07:56
  • I would like to run this code on a Tomcat server. What do I need to add so that this code is run? – Naj May 17 '15 at 22:13

1 Answers1

0

Create a war application using eclipse:

you can see this video: https://www.youtube.com/watch?v=fk22hQz9L_M

or googling then copy you code inside a servlet. You should use the init method.

an pseudo code:

    public class YourServlet extends HttpServlet {

      int count;

      public void init(ServletConfig config) throws ServletException {
        super.init(config);
         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);

    /// you shoud put the consumer inside a thread.
    channel.basicConsume("q1", true, new DefaultConsumer(){
    /// here you should use Default consumer and not QueueingConsumer because is blocking 
    });
    }
  }  
      public void doGet(HttpServletRequest req, HttpServletResponse res) 
                               throws ServletException, IOException {
        res.setContentType("text/plain");
        PrintWriter out = res.getWriter();
        .....


      }
    }

See also DefaultConsumer

Hope it helps

Community
  • 1
  • 1
Gabriele Santomaggio
  • 21,656
  • 4
  • 52
  • 52