views:

49

answers:

3

I want to make an AJAX call to my Java webapp. The Java webapp will in turn make an asynchronous return call elsewhere. The result of that call will then be returned as the result of AJAX request.

The crux of my question is what would I do with the HttpRequest whilst I'm waiting for the second call to return?

Do I just block and wait for the call within the AJAX handler method or do I store the request somewhere and wait for a callback? How would I handle errors / timeouts?

For those who care further information as to how I arrived at this situation follows:

This is part of an XMPP based instant messaging system. There is one global support user which is displayed as an icon on every page in our webapp. I also want to display the presence of this user, so, I could just use the IM system to request this users presence on every single page load for every user and eventually DDOS myself. Instead I want to have a single user query the presence from the webapp periodically and cache the result.

The AJAX call is therefore to the server which will then either return the cached presence or query the XMPP server asynchronously.

A: 

You shouldn't have to block and wait for the AJAX call. That is, don't make the call synchronously. What you should do on the Java side is figure out a way to block while you wait for the response to come back from your asynchronous call (i.e., figure out to a way to make the request synchronously. The performance hit will be on the first call for any new data. Subsequent calls will hit the cache, so you should be good). You can maintain a cache for this data, so you can check the cache first to see if the data exists. If it doesn't make the call and store the result in the cache. Otherwise, grab the data from the cache and send it back to the view. Since AJAX is asynchronous, your callback will be called as soon as the data comes back from the server.

Vivin Paliath
I never intended to block on the AJAX call but other than that this is pretty much what I ended up doing so the rep is yours. Thanks all.
Ollie Edwards
A: 

here is what i would do:

  • when the page startup, init an job to retrieve data array you need for that specific page, you need to identify the job and the job result for later usage
  • use ajax from the page to poll for the job result, once the job is done, the poll finishes and returned with data
  • cache the entries you have requested as Vivin indicated
  • cache the job result on your server and give it a time-out option
Andy Lin
A: 

HTTP requests, i.e. HttpServletRequest objects are not serializable. Therefore you cannot store them in a persistent store of any sort, for the duration of the call. It doesn't make sense anyway to store the request, for its life is limited to the duration of the HTTP request itself, given the stateless nature of the HTTP protocol.

This effectively means that you have to hold on to the HttpServletResponse object for the duration of the call. The HttpServletRequest object is no longer needed, once the parsing of the HTTP request is performed, and once all the data is available to your application; it is the response object that is of importance in your context.

The response could be populated with the cached copy of the user status. If the copy in the cache is stale, you might want to refresh it synchronously from the XMPP server (after all, it affects the performance of just one page load). You could query asynchronously from within the application server, but some result must be returned to the browser (so there might be a few edges cases that need to be taken care of).

Vineet Reynolds