views:

1482

answers:

4

I am currently writing a Comet application which requires me to send chunks of data at a time on a persistent connection. However, I'm having trouble flushing the message to the client before closing the connection. Is there any reason the PrintWriter.flush() method is not behaving like I think it should?

This is my Tomcat Comet implementation:

public void event(CometEvent event) throws IOException, ServletException {
 HttpServletRequest request = event.getHttpServletRequest();
 HttpServletResponse response = event.getHttpServletResponse();
 if (event.getEventType() == EventType.BEGIN) {
  request.setAttribute("org.apache.tomcat.comet.timeout", 300 * 1000);
  PrintWriter out = response.getWriter();
  out.println("BEGIN!");
  out.flush();
  System.out.println("EventType.BEGIN");
 } else if (event.getEventType() == EventType.READ) {
  InputStream is = request.getInputStream();
        byte[] buf = new byte[512];
        do {
            int n = is.read(buf); //can throw an IOException
            if (n > 0) {
                System.out.println("Read " + n + " bytes: " + new String(buf, 0, n) 
                        + " for session: " + request.getSession(true).getId());
            } else if (n < 0) {

                return;
            }
        } while (is.available() > 0);
        System.out.println("subtype: "+event.getEventSubType());
  System.out.println("EventType.READ");
 } else if (event.getEventType() == EventType.END) {
  PrintWriter out = response.getWriter();
     out.println("END!");
     out.close();
     System.out.println("checkError: "+out.checkError());
  System.out.println(event.getEventSubType());
  System.out.println("EventType.END");
  //eventWorker.enqueue(new EndEvent(request, response));
 } else if (event.getEventType() == EventType.ERROR) {
  PrintWriter out = response.getWriter();

     out.println("ERROR!");
     out.flush();
     System.out.println("checkError: "+out.checkError());
  System.out.println("subtype: "+event.getEventSubType());

  //response.getWriter().close();
  System.out.println("EventType.ERROR");
 } else {
  (new ServletException("EXCEPTION")).printStackTrace();
 }
}

So here I'm trying to send the message "BEGIN!" and keep the connection open afterwards so I can send more data. However, it seems that the message doesn't go through until the connection is closed.

This is my ajax code: $.post('comet', function(data) { alert(data); });

After I run this code, Firebug tells me that this is the response header: Server: Apache-Coyote/1.1 Transfer-Encoding: chunked Date: Mon, 13 Jul 2009 21:16:29 GMT

This leads me to think that my browser received some data back, but how do I update something on the page before the connection is closed?

A: 

I remember there being an issue with servlets where if your page is redirected (like to an error page) the current response buffer, writer in this case, is thrown away without being written.

Kelly French
A: 

I don't really follow the punchline.

So once there is an error and the connection closes, I see all the messages that I tried to send earlier.

what does that mean? Also, you might want to check out the api for available(). I don't think it does what you think it does.

Dan.StackOverflow
So here I'm trying to send the message "BEGIN!" and keep the connection open afterwards so I can send more data. However, it seems that the message doesn't go through until the connection is closed.
jcee14
Ok, try response.flushBuffer() ? If that doesn't work try writing a ton more bytes. What connector are you using APR or NIO? Shouldn't firebug also tell you whats in the response? It looks like you are just getting the response headers.
Dan.StackOverflow
A: 

So it seems that the browser was receiving data this whole time, but because the connection wasn't closed, the JavaScript thought data was still being sent. This is why my jQuery callback function was not being called.

Looking at the W3C AJAX tutorial, I noticed that there are different ready states for the XMLHttpRequest object.

var xmlhttp;
 if (window.XMLHttpRequest)
   {
   // code for IE7+, Firefox, Chrome, Opera, Safari
   xmlhttp=new XMLHttpRequest();
   }
 else if (window.ActiveXObject)
   {
   // code for IE6, IE5
   xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
   }
 else
   {
   alert("Your browser does not support XMLHTTP!");
   }
 xmlhttp.onreadystatechange=function()
 {
  if(xmlhttp.readyState==3) {
   alert('process '+xmlhttp.responseText);
  }
  if(xmlhttp.readyState==4) {
   alert('ready '+xmlhttp.responseText);
  }
 }
 xmlhttp.open("GET","comet",true);
 xmlhttp.send(null);

Traditionally, people only handle the 4th readyState, which means transfer is complete. In my case, however, I needed to read data before the transfer ends. Hence, I needed code to handle the 3rd readyState.

jcee14
A: 

I had nearly the same problem:

Both calls and nothing going out.

writer.flush();
writer.end();

When I attempted to do a response.flushBuffer() in addition to the two above, a java.nio.charset.UnmappableCharacterException was thrown (under jboss 5.1). I then added a response.setCharacterEncoding("UTF-8); and the problem is gone.

This has probably nothing to do with your problem but since I found your post as the first in google, I answer it here for people having the same situation.