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?)
You should return immediately with a status of 202 Accepted. Along with this, the HTTP spec agrees with Jonathan’s answer:
You can set the status (in the servlet, for example) using:
Where
responseis theHttpServletResponseprovided 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:
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.