2

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.

g_pass
  • 711
  • 7
  • 15
  • If you are using weblogic, you have everything built-in : http://download.oracle.com/docs/cd/E15051_01/wls/docs103/webapp/pubsub.html – RealHowTo Jun 22 '11 at 22:14

1 Answers1

0

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.

dty
  • 18,795
  • 6
  • 56
  • 82
  • Thanks. I am trying to use bayeux/cometd and it abstracts some of this, so it is less obvious how to handle it. I ended up using a threadpool of my own and using the subscribe/unsubscribe callbacks that Bayeux provides to schedule jobs in my pool. – g_pass Jul 11 '11 at 05:13