views:

269

answers:

3

If i want to run a process (time taken to complete this process is in hours) in multi-user environment (thru tomcat-servlet) Which is better to implement this process in? 1) In Threads or 2) In JMS what are advantages and disadvantages?

+2  A: 

Not sure exactly how you envisage using JMS, ofen the way that's done is for clients to pop requests on a queue, and have some (configuranble) number of worker threads pulling the requests off and servicing them. In my case I'd have an MDB as the mechanism to get those worker threads going - but that's just an implementation detail.

So I don't see Threads and JMS as alternatives, more as JMS giving you a controllable way to use threads. You almost certainly need to avoid allowing too many requests be worked on at once so you will conceptually need queues, may as well use JMS to achieve that.

Various details to figure out, like how to communicate results, for sure the browser ain't hanging around for hours. Some kind of Ajax polling, or Comet push maybe?

One other thought - a single chunk of processing taking several hours? That may benefit from being broken up into a number steps (again mediated by JMS queues pehaps). That way a crash doesn't send everything back to square one.

djna
A: 

If I understood your options right, do the 2nd option, i.e.: Have the servlet on Tomcat send an message to a JMS client (preferably, a MDB in a JMS container). There, fire the lengthy process. Do that rather than creating a new thread in the servlet (the option 1), that is really not recommended way to do things.

david a.
A: 

I think the key factor is: what's the maximum number of simultaneous requests you expect to see and what's your strategy for dealing with overflow?

If you'll only ever have a handful of requests at a time, threads is just fine. A simple background thread which maintains a CountdownLatch could be used for handling any overflow.

If you expect lots of requests, then JMS may be more well suited for your task.

chetan