• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Separate thread for a complex tasks in a servlet

 
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic