Search code examples
javaweb-servicesdesign-patternsasynchronousserver-side

Recommended patterns for server side web service asynchrony


I have successfully implemented both polling and callback client side asynchronous examples, but now I am interested in implementing a web service with server side asynchrony. The infrastructure is Java EE using JBoss AS 6.x.

The pattern (as I understand it) that I am trying to implement would involve two web service operations, one to initiate a request and a second to check to see if a request is done and retrieve the results.

  1. Client invokes web service endpoint operation 1 with search parameters via SOAP over HTTP.
  2. Web service endpoint sends the request over JMS queue 1 to a message driven bean.
  3. Message driven bean (MDB) picks up the request from queue 1.
  4. MDB acknowledges service request by sending a message containing a correlator ID over JMS queue 2 to the web service endpoint. (I am assuming this correlator ID will be the JMS message id generated.)
  5. MDB acknowledges the original message to remove it from queue 1.
  6. MDB begins long running database query, presumably building results to a temp table using the correlator ID as the retrieval key.
  7. Web service endpoint sends back reply containing correlator ID to the Client via SOAP over HTTP.

I am guessing that, since picking up the results doesn't involve a long query, I don't need JMS, I can simply query the database to see if results are ready. So, the second operation would be:

  1. Client invokes web service endpoint operation 2 with correlator ID via SOAP over HTTP.
  2. Web service queries database using correlator ID. Result code would be: no results found, operation still in progress, or results found.
  3. Web service responds to Client with some sort of complex structure that would combine the result code along with any results.

So, I have many questions. I have seen some references to server side support for asynchrony, but they all seem to be server specific in some way. For example, I have seen a diagram describing this first operation exactly, but it seems to be OC4J specific. If anyone can direct me to an generic example or tutorial implementing something like this, please do. Other subleties might be, should I use the JMS message ID as the correlator to return to the client? I am assuming I should use CLIENT-ACKNOWLEDGE as the JMS session mode so that the MDB can send a reply to the web service and then remove the message from the queue. Or, should I even bother? Should the web service endpoint just generate a JMS message, pop it in the queue and return the message ID directly to the service client without going through all the rigamarole of having the MDB send back a correlator via JMS queue 2? [edit: Actually, the more I consider it, it seems wrong that the web service would send a message and then, what, block on waiting for a reply on queue 2? ]


Solution

  • Correlation id can be generated at step 2 and immediately returned to the client. That reduces additional hops before responding to the client. Generating a persistent message into the queue should suffice. I also dont see the need for two queues. I prefer an application generated correlation id.