gpt4 book ai didi

tomcat - cometd 和超时

转载 作者:行者123 更新时间:2023-11-28 23:04:50 27 4
gpt4 key购买 nike

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

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

附上代码片段

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

}

最佳答案

我会说继续修改

delivery = consumer.nextDelivery(27000);

到:

 delivery = consumer.nextDelivery(20000);

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

关于tomcat - cometd 和超时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11566348/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com