aspose file tools*
The moose likes Servlets and the fly likes Creating threads in service method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Creating threads in service method" Watch "Creating threads in service method" New topic
Author

Creating threads in service method

Pooja VH
Greenhorn

Joined: Dec 19, 2003
Posts: 2
I have a problem wherein a request should trigger a background task and return the response without waiting for the task to complete. I dont want to go for JMS for this asynchronous implementation.
I am planning to implement this using threads.
Are there any overheads of this?
I am not passing any instance variables to the threads.
Billybob Marshall
Ranch Hand

Joined: Jan 27, 2004
Posts: 202
Using normal threads would be the way I'd want to do it too. But in my experience and from what I've read, there can be problems with the J2EE server's handling of those user threads. They always seem to recommend doing things the J2EE way (JMS, thread pools, etc)
john guthrie
Ranch Hand

Joined: Aug 05, 2002
Posts: 124
i know that spawning threads in an ejb is frowned upon (violates the spec, yes?) because of problems with reentrance, but i have honestly not heard of a problem spawning a thread in a servlet, especially if you do your diligence to correctly manage it.
having said that, maybe having your own threads could prevent the container from managing (i.e. destroying) the servlet class, but again, i do not know how often that becomes an issue, and if it's worth adding a whole new complex system (jms) because of that.
Billybob Marshall
Ranch Hand

Joined: Jan 27, 2004
Posts: 202
Originally posted by john guthrie:
i know that spawning threads in an ejb is frowned upon (violates the spec, yes?) because of problems with reentrance, but i have honestly not heard of a problem spawning a thread in a servlet, especially if you do your diligence to correctly manage it.

For example, all versions of Weblogic thru the current 8.1 will not properly terminate daemon threads when the web-app / ear is shut down, such as during hot-redeployment of the app, leading to incremental leaks every time the app is redeployed without restarting the server. Problems do exist with threads in J2EE regardless of EJB or servlet or whatever. Having said that, I'd think it's a bug in the J2EE container in the first place, but nevertheless you have to be wary of creating threads.
Pooja VH
Greenhorn

Joined: Dec 19, 2003
Posts: 2
Hey thanx for that. It does make sense.
Anyways I found another workaround to this. Rather than creating a new thread, I am submitting two requests. One of the requests triggers the background process and updates the session once the job is done. I dont look forward to its response . Rather the other requests keeps polling the session. Once the session is updated it returns response.
In this case I dont have to worry about managing threads and there overheads if any.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Creating threads in service method
 
Similar Threads
Last task not executed by thread from thread pool
Is there a thread that runs outside the jvm
Multiple Thread Pool Framework - HowTo
ServletContextListener (contextDestroyed) with Threads
excel and jasper reports