2

in JSP when i reach "QueueingConsumer.Delivery delivery = consumer.nextDelivery()" the page gets stuck loading. the message is consumed but the jsp won't load nor does it give errors. please help

String QUEUE_NAME = "hello";

ConnectionFactory factory = new ConnectionFactory();
factory.setHost("localhost");
Connection connection = factory.newConnection();
Channel channel = connection.createChannel();

channel.queueDeclare(QUEUE_NAME, false, false, false, null);
out.println(" [*] Waiting for messages. To exit press CTRL+C");

QueueingConsumer consumer = new QueueingConsumer(channel);
channel.basicConsume(QUEUE_NAME, true, consumer);

while (true) {
  QueueingConsumer.Delivery delivery = consumer.nextDelivery();
  String message = new String(delivery.getBody());
  out.println(" [x] Received '" + message + "'");
}
Ever Forth
  • 23
  • 4
  • I can't see a way out of that while loop? – fvu Oct 18 '12 at 11:07
  • As pointed out by fvu, the processing of the request is going to get stuck in your while loop – Martin Wilson Oct 18 '12 at 11:21
  • even without the while loop the page still gets stuck at QueueingConsumer.Delivery delivery = consumer.nextDelivery(); – Ever Forth Oct 18 '12 at 11:27
  • 1
    That will be because there's no message - nextDelivery() blocks until there is – Martin Wilson Oct 18 '12 at 11:30
  • thanks martin. i tried loading the page after sending message to queue and page still won't load. so how do i go about displaying the messages on the page? – Ever Forth Oct 18 '12 at 11:33
  • @EverForth what Martin Wilson said, but as I understand the RabbitMQ docs there's no straightforward way to "peek" to see whether a message is waiting to be received, only a [workaround](http://stackoverflow.com/questions/4700292/using-rabbitmq-is-there-a-way-to-look-at-the-queue-contents-without-a-dequeue). Also, `QueueingConsumer` is apparently deprecated, it might be a good idea to switch to a recent solution now instead of later on. – fvu Oct 18 '12 at 11:35

1 Answers1

1

I am guessing that you are trying to implement the RabbitMQ "Hello World" tutorial in a JSP page. You can't do this as:

  • QueueingConsumer.nextDelivery will block until a message is available on the queue
  • To show all messages as they arrive at the head of the queue you need an infinite loop (as shown in the tutorial) which won't work in a JSP page.

Your JSP page is part of the request lifecycle, which needs to end as soon as possible and return the requested resource (in this case the HTML etc in your JSP page) to the user's browser. Although most browsers will start displaying results as they receive them they won't deal with a long hiatus or a never-ending response. Even if they could you don't want this as there will be a limit to the number of concurrent requests your server can handle.

If you are playing around with RabbitMQ to see how it works then I would recommend you develop a Java application (as per the tutorial), i.e. don't try to do it in a webapp. (Also, as pointed out by fvu, note that QueueingConsumer is now deprecated).

If you need to do it in a webapp then you'll need to think of a way to store messages (e.g. in a database or a singleton data structure if you have a single instance of e.g. Tomcat) as they are taken off the message queue (probably by a background thread, i.e. not one that services requests). Your JSP page can then read the messages from your data structure to display to the user.

Martin Wilson
  • 3,386
  • 1
  • 24
  • 29