Reputation: 711
I'm struggling to understand an efficient way to notify clients without wasting threads. When a client connects to a CometD servlet, I want to start monitoring server-side events for the client, and publish them to the client when they are available. This should be done in a threadpool or something that doesn't use the servlet thread. All the example I see are for client originated events, and listeners on the server. I need the inverse... Do I just hand off the Client/ServerSession object to my own machinery? It seems like this would be a common pattern, but I can't find any examples.
Upvotes: 2
Views: 525
Reputation: 18998
Look at the Servlet 3.0 Async API. In particular, ServletRequest.startAsync()
will return you an AsyncContext
object which you can "hand off to your own machinery", and then return from your servlet doGet()/doPost()/etc. without terminating the connection.
There are then various patterns for sending data back to the client. I believe your "own machinery" can just fetch the ServletResponse
from the AsyncContext
and write to it. Another approach is to call dispatch()
on the AsyncContext
which will cause your doGet()/doPost() method to be called again.
Upvotes: 0