title: CometProcessor End Event loop
body: <p>I have a simple servlet which uses CometProcessor interface.</p>
  
  <pre><code>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 {
   }
  
  }
  </code></pre>
  
  <p>Connector is </p>
  
  <pre><code>    &lt;Connector port="8080" protocol="org.apache.coyote.http11.Http11NioProtocol" 
             connectionTimeout="20000" 
             URIEncoding="UTF-8"
             redirectPort="8443" /&gt;
  </code></pre>
  
  <p>Jsp page is </p>
  
  <pre><code>&lt;%@ page language="java" contentType="text/html; charset=UTF-8"
      pageEncoding="UTF-8"%&gt;
  &lt;!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"&gt;
  &lt;html&gt;
  &lt;head&gt;
  &lt;meta http-equiv="Content-Type" content="text/html; charset=UTF-8"&gt;
  &lt;title&gt;Test&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
  &lt;script type="text/javascript"&gt;&lt;!--
  
   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);
    }
  
   }
  
  --&gt;&lt;/script&gt;
  &lt;input id="mytext" type="text"&gt;&lt;/input&gt;
  &lt;input type="button" value="send" onclick="sendData()"&gt;&lt;/input&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>I'm using Eclipse to start the servlet.<br/>
  What I would expect is to send "text" to the server and get back the echo with the same text and CometEvent name.<br/>
  When I'm sending the data to the server the first I get "200 ReadyState=3 BEGIN text". This is expected scenario. If I wait for about 25 seconds I will get "Error: TIMEOUT" in console output. And error event will be fired every 25 seconds, so in browser I see something like "200 ReadyState=3 BEGIN textERROR textERROR textERROR textERROR textERROR text", where the "text" is the text in input. 5 textERRORs means that 5 error events occured. I don't know why these textERRORs appeared. And the main question is why the error event (timeout) fired every 25 seconds?<br/>
  If I close client in browser I will get an infinite loop of End Events. Why this happens I don't know either? How to prevent that kind of loop?<br/>
  Tomcat version is Apache Tomcat/6.0.28 on Ubuntu 10.10<br/>
  Thank you in advance.<br/></p>