0

Hi am trying since a long time make a chat application using comet in tomcat. The problem is that i have deployed the application in cloud where there is a cap on 30 sec for a open connection. So every time the server takes more than 30 sec to respond (like when there is no chat message left to push ) client receives a 504 exception.

So does this mean the comet technique does not work in the scenarios where we have a time cap ?

Attaching the code snippet

package com.cumulations.clique.ChatHandler;
public class AsynchronousGetChatHandler extends HttpServlet implements
    CometProcessor {

public static HashMap consumerConnectionQueue = new HashMap();
public static HashMap consumerPoolingQueue = new HashMap<String, String>();
public static HashMap consumerSessionQueue = new HashMap<String, Date>();

public static ConnectionFactory factory;
public static Connection connection;

public void event(CometEvent event) throws IOException, ServletException {

    HttpServletRequest request = event.getHttpServletRequest();
    HttpServletResponse response = event.getHttpServletResponse();

    String userName = request.getParameter("userName");
    String sessionId = request.getParameter("accesskey");

    AsynchronousGetChatHandler.consumerPoolingQueue.put(userName, "ON");

    try {

        if (event.getEventType() == CometEvent.EventType.BEGIN) {

            String str = fromRabitQ(userName);

            if (str != null) {


                    System.out.println("delivering a message: " + str);

                    PrintWriter writer = response.getWriter();
                    writer.println(str);
                    writer.flush();
                    writer.close();
                }

             else {
                PrintWriter writer = response.getWriter();
                writer.println("");
                writer.flush();
                writer.close();
            }
        }
    }

    catch (Exception e) {
        throw new ServletException("Recieving exception");

        // TODO: handle exception
    }

}

public static String fromRabitQ(String userName) throws Exception {
    try {

        Channel channel;
        QueueingConsumer consumer;

        String QUEUE_NAME = userName;
        String message = "";

        connection = RabbitMqConnection.getConnection();

        channel = connection.createChannel();

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

        QueueingConsumer.Delivery delivery;

        delivery = consumer.nextDelivery(27000);

        if (delivery != null) {
            message = new String(delivery.getBody());

        } else {
            message = null;
        }

        channel.basicCancel(consumer.getConsumerTag());
        channel.close();

        return message;
    }

    catch (Exception e) {
        System.out.println("Exception occured while receiveing " + e);
        throw e;
    }

}

}

praveena_kd
  • 525
  • 5
  • 15
  • see this response: http://stackoverflow.com/questions/11430841/what-is-the-best-way-to-automatically-reestablish-long-polling-request/11441711#11441711 – Alessandro Alinone Jul 19 '12 at 17:47
  • Thanks alessandro.. I have done the sending of empty response after waiting for 30 sec.(Actually i am waiting for a message in rabbitMq for 28 sec and then returning a empty response). However when i tried with some load from jMeter i could see many of the requests still returning 504. Because of these 504's i get my log entries grow in size. is there any way to avoid atleast the logging of 504 ? – praveena_kd Jul 19 '12 at 17:55
  • Have you tried a shorter window than 28 seconds? Perhaps 20 seconds? If that doesn't help, can you post the source to your application along with directions on reproducing the load with JMeter so we can troubleshoot further? – Glenn Oppegard Jul 19 '12 at 20:36
  • Hi Glenn Oppegard i have attached the sample code . let me know if i am doing anything wrong. – praveena_kd Jul 20 '12 at 07:02

1 Answers1

1

I would say go ahead and modify the

delivery = consumer.nextDelivery(27000);

to:

 delivery = consumer.nextDelivery(20000);

Also if you can also provide your implementation of RabbitMqConnection that would be great.

Ali Moghadam
  • 1,270
  • 8
  • 17