2
votes

I want to make a service that basically holds a connection till a second connection is made to tell it to stop holding the first one.

Using NIO Http Connector with all default settings (in Tomcat and the connector).

I use a thread (much like the "messageSender" class in the tomcat aio doc) to hold the first response and write/flush a character every 5 seconds. This is working fine - the thread holds it and releases it on cue. The only problem is that the service ('event' method) does not get a second call (for the second connection's 'begin' event) till I close the first connection.

So to repeat, this is what happens:

connection 1 - > tomcat creates thread 'http-nio-8081-exec-2' which calls myService.event(event);

myService.event gives the response to 'myDaemonThread' (which holds the reference and writes to the response every 5 seconds). exits method.

'myDaemonThread' uses "synchronized (this){ this.wait(5000); }" for waiting (initially used Thread.sleep() but in an attempt to make this work, I changed to wait() )

connection 2 - > initiated but no thread calls myService.event

connection 1 is stopped in the browser, tomcat creates thread 'http-nio-8081-exec-4' which calls myService.event(event.END). exits method.

tomcat (creates ?? now or earlier, cant tell) thread 'http-nio-8081-exec-6' calls myService.event(event.BEGIN) for connection two.

...... the rest is unimportant.

So this seems to defeat the entire purpose of using NIO... anyone have any idea why this is happening?

1
Why? If you're starting a thread and then just waiting for it to complete, why not just run the method that the thread runs? And why do you want to keep the connection open for 5 seconds in the first place? - user207421
Without seeing some actual code it is impossible to determine what you doing wrongly. - Mark Thomas
I went through such lengths to describe what was happening exactly because you dont need the code to solve this. This was (I suspected) a Tomcat/Comet (settings) based problem - not code based, though I suppose I should have said as much. - shadybones

1 Answers

1
votes

Possible Reasons

  1. Tomcat isn't allowing two identical request from the same client at the same time
  2. The client queues identical requests 'behind the scenes'.

Whatever, the result is, if I change the client for the second request, everything worked fine. If I added junk parameters to the second request on the same client, it worked fine.

Solution

Add junk params to the requests