aspose file tools*
The moose likes Servlets and the fly likes Separate thread for a complex tasks in a servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Separate thread for a complex tasks in a servlet" Watch "Separate thread for a complex tasks in a servlet" New topic
Author

Separate thread for a complex tasks in a servlet

Andrei Antonescu
Ranch Hand

Joined: Jul 08, 2010
Posts: 75
Hello,

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())).

Thanks in advance
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

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.


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42277
    
  64
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.


Ping & DNS - my free Android networking tools app
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Separate thread for a complex tasks in a servlet