0

嗨,很长一段时间以来我一直在尝试在 tomcat 中使用彗星制作聊天应用程序。问题是我已经在云中部署了应用程序,其中有 30 秒的开放连接上限。因此,每次服务器响应时间超过 30 秒时(例如当没有聊天消息要推送时),客户端都会收到 504 异常。

那么这是否意味着彗星技术在我们有时间上限的情况下不起作用?

附上代码片段

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

}

}

4

1 回答 1

1

我会说继续修改

delivery = consumer.nextDelivery(27000);

至:

 delivery = consumer.nextDelivery(20000);

此外,如果您还可以提供 RabbitMqConnection 的实现,那就太好了。

于 2012-08-04T01:05:41.163 回答