我有一个使用CometProcessor接口的简单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 text”。这是预期的情况。如果等待约25秒,我将在控制台输出中得到“错误:超时”。错误事件将每25秒触发一次,因此在浏览器中,我看到类似“ 200 ReadyState = 3 BEGIN textERROR textERROR textERROR textERROR textERROR textERROR text”的内容,其中“ text”是输入中的文本。 5个textERRORs表示发生5个错误事件。我不知道为什么这些textERRORs出现。主要问题是为什么错误事件(超时)每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);
在您的代码中发送的是“错误文本”,而不是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/