tags:

views:

81

answers:

3

Are there any suggested practices for implementing long-running operations in Tomcat?

e.g. is it better to just run the operation and not yield a response until the operation is complete? Or is it better to return immediately with some kind of ticket that can be used to poll for completion? (is there a way to notify a client that completion has occurred?)

+2  A: 

For the sake of your server not falling down, and for the sake of not frustrating your user, it is definitely preferable to immediately return with, as you suggest, a "ticket" of some kind, and then to poll periodically for completion of that ticket.

If it's a very long-running operation, you might consider sending an email (such as Vimeo does, for example, when they've re-encoded your video).

Jonathan Feinberg
+1  A: 

Depend on how long you operation will be executed. I'm use both methods depend on situation. I'm have a servlet for usual browser, that wait several seconds and all ok, but another servlet for accessing from PDA fail on long timeouts and i'm return response imm. after putting request into queue. In summary i'm think second way better in general.

Alexey Sviridov
+1  A: 

You should return immediately with a status of 202 Accepted. Along with this, the HTTP spec agrees with Jonathan's answer:

The entity returned with this response SHOULD include an indication of the request's current status and either a pointer to a status monitor or some estimate of when the user can expect the request to be fulfilled.

You can set the status (in the servlet, for example) using:

response.setStatus(HttpServletResponse.SC_ACCEPTED);

Where response is the HttpServletResponse provided for the request.

Edit (re: comments):

Good browsers will treat any 2XX status as successful and will render the content provided in the response body to the user, so returning a 202 shouldn't affect web browser users and how they see pages. If the browser doesn't do this, it's probably violating Section 6.1.1 of the spec:

...applications MUST understand the class of any status code, as indicated by the first digit, and treat any unrecognized response as being equivalent to the x00 status code of that class...

Since a Tomcat server is just a specialized HTTP server, you should try to stick to the specification as much as possible (i.e. returning as appropriate a status as possible). Even if you intend your site to be accessed by a browser, there's nothing stopping other clients (curl, etc.) from requesting a page. If you provide specific statuses, clients can act more appropriately to responses.

Rob Hruska
That's good advice if serving some sort of (non-browser) client, but I believe that, for a web site that feeds people behind browsers, it's better to be less "correct" and give the user a 200, with a page describing what will happen next, and roughly how long it will take.
Jonathan Feinberg
sweet...... thanks!
Jason S
if the user is browsing interactively and sees a meaningful response page, does it matter whether the return code is 200 or 202?
Jason S
@Jonathan, @Jason - See my edits in the question.
Rob Hruska
Ah, thanks for enlightening me.
Jonathan Feinberg