views:

53

answers:

2

I know that we should not start threads in a servlet is that threads should be managed by the container. If the container is told to shutdown if there are threads that it does not know about hanging around it wont shutdown. I take care of this by making it a daemon thread...

But other than the above "unable to shutdown" situation what other reason could there be to not allow a servlet to start threads. I have seen some mentions that if the environment is clustered it will cause a problem. But no actual walk-through of what could happen that would be BAD.

EDIT: This is currently being done in a servlet and I am having trouble convincing the author of this code that is not a good idea. The argument that one has to understand complexity is not going to fly... I am looking for one specific concrete case when something bad can happen, without intending it to

In my situation: the servlet in question is launches n threads and this happens in each vm on the cluster by design. There is no transactional requirement

+1  A: 

There are a lot of issues, depending on your use case. What if the particular server in the cluster that your thread/job is running on crashes, which makes your thread go away, would that be a bad thing? Should someone be notified? Should the job move over to another server in the cluster? Should it restart once the server starts up again? All of this, you've got to implement in your thread....or you could use JMS, which will even run in Tomcat, with the addon of ActiveMQ, or some other messaging container of your choice, and just write the code that executes your logic, and let the container worry about all the rest of this. YMMV

mezmo
That is a good point: These threads generate an image based on application state and gets sent over the network elsewhere. So if the server crashes and restarts I presume that the thread just dies the work is lost.This is important because this image generation and sending can happen only once.As you say, the servlet could send a JMS message to an MDB to start the work. How does it behave when there is a server crash ?
unmaskableinterrupt
Depending on how you've set the message queue up, assuming your use case is best satisfied with a queue, and if you have multiple servers in a cluster, then if 1 server crashes, the failover server could restart generating the image, waiting for something to come get it, or it could discard it, whichever works best for your situtaion.
mezmo
+2  A: 

From the official FAQ:

Why is thread creation and management disallowed?

The EJB specification assigns to the EJB container the responsibility for managing threads. Allowing enterprise bean instances to create and manage threads would interfere with the container's ability to control its components' lifecycle. Thread management is not a business function, it is an implementation detail, and is typically complicated and platform-specific. Letting the container manage threads relieves the enterprise bean developer of dealing with threading issues. Multithreaded applications are still possible, but control of multithreading is located in the container, not in the enterprise bean.

That said, if the problem of startup and shutdown is not considered, it is partly a "philosophical" issue in the sense that thread is an implementation detail, and also the fact that multi-threading is considered a scalability concern, which should be managed by the app. server.

For instance, most app. servers allow the integrator to define pools and configure the number of threads, etc. An app that spawns thread itself escapes this configuration, and does not cooperate nicely in the scalability plan.

Also, if you want a single background thread in a clustered environment, it becomes tricky.

And finally, the app. server controls the transactions. If you spawn threads yourself, you must take care to understand all the details of what can be used safely or not (e.g. get a connection from the pool) and how to use UserTransaction if necessary. The idea is that you shouldn't worry about such detail if you use an app. server, but you will need to if you start dealing with threads yourself.

I've however seen web app spawning a background thread from a ServletContextListener, and guess what, that was fine, even if the app was deployed on more than one node. You just need to understand what it means to have several JVM running and make sure you support that correctly.

ewernli
You say: single background thread. I assume its a specific case of the "Singleton in a clustered environment" issue.Also there is no transactional requirement...
unmaskableinterrupt