Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Separate thread for a complex tasks in a servlet

 
Andrei Antonescu
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic