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;
}
}
}