File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Tomcat and the fly likes Comet example of Tomcat 6 not working Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Products » Tomcat
Bookmark "Comet example of Tomcat 6 not working " Watch "Comet example of Tomcat 6 not working " New topic

Comet example of Tomcat 6 not working

Ritesh Srivastava

Joined: Jul 06, 2005
Posts: 26

I am trying to run the comet sample given along with tomcat. The sample is not working correctly most of the time. I am using NIO connectors and Tomcat version is 6.0.10.
I have some doubts also:

1) In the ChatServer example, a thread (using a Runnable MessageSender) is sending responses the clients. Is it possible to write multiple times to the same response. Because when we do writer.flush() then that response is sent to the client.

2) The READ event is never invoked in my example. whenever I send a request only BEGIN event is getting called. I thought, for the first time only BEGIN event should get called, and for further requests by the same client, READ event should get called.

3) I am getting exception at the line where the thread which is trying to write to response stream is calling the flush method.
The code (from tomcat example) is:

for (int i = 0; i < connections.size(); i++) {
try {
PrintWriter writer = connections.get(i).getWriter();
String toBeFlushed = "";
for (int j = 0; j < pendingMessages.length; j++) {
// FIXME: Add HTML filtering
writer.println(pendingMessages[j] + "<br/>");
} catch (IOException e) {
log("IOExeption sending message", e);

And the error message I am getting is:

Exception in thread "MessageSender[/CometTestApp]" java.lang.NullPointerException
at org.apache.coyote.http11.InternalNioOutputBuffer.addToBB(
at org.apache.coyote.http11.InternalNioOutputBuffer.commit(
at org.apache.coyote.http11.Http11NioProcessor.action(
at org.apache.coyote.Response.action(
at org.apache.coyote.Response.sendHeaders(
at org.apache.catalina.connector.OutputBuffer.doFlush(
at org.apache.catalina.connector.OutputBuffer.flush(
at org.apache.catalina.connector.CoyoteWriter.flush(
at com.headstrong.test.ChatServlet$
William Brogden
Author and all-around good cowpoke

Joined: Mar 22, 2000
Posts: 13037
If the line: writer.flush() is throwing a NPE, then the first thing to suspect is that the writer variable is null.

How does this program keep track of clients dropping the connection?

Trying to keep connections open and repeatedly writing to them sounds dangerous to me if the clients are typical browsers.

Kanchan Kumar Dey

Joined: Oct 18, 2007
Posts: 19
HI Ritesh,
Could you please share the information that how you have configured Tomcat 6.0 with NIO

You are not getting events for Read because, everytime its a new request and for that you are getting only Begin event.
Read even is fired when a message comes from the client over the same HTTP connection already established...I Guess.
I agree. Here's the link:
subject: Comet example of Tomcat 6 not working
It's not a secret anymore!