gpt4 book ai didi

tomcat - CometProcessor 结束事件循环

转载 作者:行者123 更新时间:2023-11-28 22:14:56 26 4
gpt4 key购买 nike

我有一个使用 CometProcessor 接口(interface)的简单 servlet。

package cc.co.sqeezer;

import java.io.IOException;

public class TestServlet extends HttpServlet implements CometProcessor {
private static final long serialVersionUID = 1L;

public TestServlet() {
super();
}

public void event(CometEvent event) {
if (event.getEventType() == CometEvent.EventType.BEGIN)
{
System.out.println("Begin");
event.getHttpServletRequest().setAttribute("org.apache.tomcat.comet.timeout", new Integer(0xFFFFFFFF));
send(event);
}
else if (event.getEventType() == CometEvent.EventType.READ)
{
System.out.println("Read");
send(event);
}
else if (event.getEventType() == CometEvent.EventType.END)
{
System.out.println("End: " + event.getEventSubType());
send(event);
}
else if (event.getEventType() == CometEvent.EventType.ERROR)
{
System.out.println("Error: " + event.getEventSubType());
send(event);
}

}

private void send(CometEvent event) {
HttpServletResponse response = event.getHttpServletResponse();
response.setHeader("Pragma", "no-cache");
response.setHeader("Cache-Control", "must-revalidate");
response.setHeader("Cache-Control", "no-cache");
response.setHeader("Cache-Control", "no-store");
response.setDateHeader("Expires", 0);

String eventType = event.getEventType().toString();
String receivedText = (String) event.getHttpServletRequest().getParameter("mytext");
try {
response.getWriter().write(eventType + " " + receivedText);
response.getWriter().flush();
} catch (IOException e) {
e.printStackTrace();
}
}

public void init(ServletConfig config) throws ServletException {
}

}

连接器是

    <Connector port="8080" protocol="org.apache.coyote.http11.Http11NioProtocol" 
connectionTimeout="20000"
URIEncoding="UTF-8"
redirectPort="8443" />

jsp页面是

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Test</title>
</head>
<body>
<script type="text/javascript"><!--

function createRequestObject() {
if (typeof XMLHttpRequest === 'undefined') {
XMLHttpRequest = function() {
try { return new ActiveXObject("Msxml2.XMLHTTP.6.0"); }
catch(e) {}
try { return new ActiveXObject("Msxml2.XMLHTTP.3.0"); }
catch(e) {}
try { return new ActiveXObject("Msxml2.XMLHTTP"); }
catch(e) {}
try { return new ActiveXObject("Microsoft.XMLHTTP"); }
catch(e) {}
throw new Error("This browser does not support XMLHttpRequest.");
};
}
return new XMLHttpRequest();
}

var req = createRequestObject();


function sendData()
{


if (req) {
req.open("POST", "TestServlet", true);
req.setRequestHeader("If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT");
req.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
var params = "mytext=" + document.getElementById("mytext").value;
//req.setRequestHeader("Content-length", params.length);

req.onreadystatechange = processReqChange;
req.send(params);
}
}

function processReqChange()
{
try
{
if (req.readyState == 4) {
alert(req.status + " ReadyState=4 " + req.responseText);
}
if (req.readyState == 3) {
alert(req.status + " ReadyState=3 " + req.responseText);
}
if (req.readyState == 2) {
alert(req.status + " ReadyState=2 " + req.responseText);
}
if (req.readyState == 1) {
alert(req.status + " ReadyState=1 " + req.responseText);
}
}
catch( e ) {
//alert("Error: " + e.description);
}

}

--></script>
<input id="mytext" type="text"></input>
<input type="button" value="send" onclick="sendData()"></input>
</body>
</html>

我正在使用 Eclipse 启动 servlet。
我期望的是将“文本”发送到服务器并返回具有相同文本和 CometEvent 名称的回显。
当我首先将数据发送到服务器时,我得到“200 ReadyState=3 BEGIN 文本”。这是预期的情况。如果我等待大约 25 秒,我将在控制台输出中收到“错误:超时”。错误事件将每 25 秒触发一次,因此在浏览器中我看到类似“200 ReadyState=3 BEGIN textERROR textERROR textERROR textERROR textERROR text”的内容,其中“text”是输入的文本。 5 textERRORs 表示发生了 5 个错误事件。我不知道为什么会出现这些textERROR。主要问题是为什么错误事件(超时)每 25 秒触发一次?
如果我在浏览器中关闭客户端,我将得到一个结束事件的无限循环。为什么会这样我也不知道?如何防止这种循环?
Tomcat 版本在 Ubuntu 10.10 上是 Apache Tomcat/6.0.28
提前谢谢你。

最佳答案

作为开始,您可以像这样设置请求的超时时间,

if (event.getEventType() == CometEvent.EventType.BEGIN) {
event.setTimeout(60 * 1000);
...
}
...

25 秒可能是默认值。

发生超时时发送错误,因此每 25 秒发送一次

response.getWriter().write(eventType + " " + receivedText);

在您的代码中发送“ERROR text”,而不是 textERROR,在您的响应末尾添加一个新行以验证这一点。

无限循环的原因是您没有关闭事件。当调用 CometEvent ERROR 或 END 时,必须调用 event.close(),

else if (event.getEventType() == CometEvent.EventType.END){
event.close();
....

这很可能是无限循环的原因。

去阅读 documentation ,它解释了您遇到的大部分问题。

关于tomcat - CometProcessor 结束事件循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4845151/

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