This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
I have a web app deployed in Tomcat 6 that uses a servlet to calculate something very complex.
I would like to ask you if it is better to start a separate thread inside the servlet and to process the request (In a Swing application i would do something similar so that the time consuming task will not block the interface (I would use SwingUtilities.invokeLater())).
One of the big differences between a Swing application and an application that uses HTTP is you can't perform the same sort of "conversations" with the GUI. You can't update a specific client's GUI after a response has been sent. It depends what sort of thing you need to do in this separate thread, but an asynchronous mechanism such as JMS might be an appropriate choice.
Long-running tasks should definitely run in their own threads. The web response should return quickly, and inform the user that processing has started. After that, there are several possibilities: a) there could be a page where the user can check what the current status of the background process is, b) the user could be notified via email once the processing is done (either with the actual results, or with a link to the page mentioned in case "a"), or c) the response page could periodically poll the server what the status is via AJAX, and update the page accordingly. "c" depends on the page remaining open, which may or may not be a good choice.
I’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link: http://aspose.com
subject: Separate thread for a complex tasks in a servlet