This particular question has come up on multiple occasions and I will summarize that there are several possible solutions, only 1 of which I would recommend.
This particular question has come up on multiple occasions and I will summarize that there are several possible solutions, only 1 of which I would recommend. Use a WorkManager from the commonj API. It allows for managed threads in a JEE container and is specifically designed to fit your use case.
If you are using WebSphere or WebLogic, these API's are already available in your server. For others your will have to put a third party solution in yourself. WorkManager info Related questions Why Spawning threads is discouraged.
I once participated in a project where EJB transactions ran for up to 5 hours at a time. Aargh! This same application also had a BEA specialist consultant who approved that they started additional threads from the transactions.
While it's disrecommended in the specs and elsewhere, it doesn't automatically result in failure. You need to be aware that your extra threads are outside the container's control and thus if something goes wrong it's your fault. But if you can assure that the number of threads started in the worst case doesn't exceed reasonable limits, and that they all terminate cleanly within reasonable time, then it is quite possible to work like this.In fact, in your case it sounds like the almost-only solution.
There are some slightly esoteric solutions possible where your EJB app reaches out to another app for a service, which then does the multithreading in itself before returning to the EJB caller. But this is essentially just shifting the problem around. You may, however, consider a thread pooling solution to keep an upper limit on the number of threads spawned.
If you have too many threads your application will behave horribly.
You've analyzed the situation quite well, and no, there is not patern for this that match the EJB model. Creating threads is mainly forbidden because it bypass the app. Server thread management strategy and also because of the transactions.
I worked on a project with similar requireements and I decided to spawn additional threads (going against the sepc then). The operation to parallelized was read-only, so it worked regarding the transaction (the thread would basically have not transaction associated to them). I also knew that I wouldn't spawn too many threads per EJB calls, so the number of threads was not an issue.
But if your threads are supposed to modify data, then you break the transactional model of the EJB seriously. But if your operation in pure computing, that might be ok. Hope it helps...
Look up the WorkManager from the CommonJ API. It allows you to do this in managed threads. – Robin Jan 5 '10 at 15:35.
An EJB is a ultimately a transactional component for a client-server system providing request/reply semantics. If you find yourself in the position that you need to pigeonhole a long-running transaction within the bounds of a request/reply cycle, then somewhere your system architect(ure) has taken the wrong turn. The situation you describe is cleanly and correctly handled by an event based architecture with a messaging back end.
Initial event initiates the process (which can then be trivially parallelized by having the workers subscribe to the event topic) and the aggregating process itself raises an event on its completion. You can still squeeze these sequence within the bounds of a request/reply cycle, but you will by necessity violate the letter and spirit of the JEE system architecture specs.
That is a very black and white view of the world, most of us have to deal with shades of gray. You would not want to rearchitect an entire system just to provide this functionality. Not to mention that there is nothing inherently wrong with having a synchronous call that does multiple parallel operations before returning when they are all done.
This on it's own does not justify an event based architecture over a simple request/reply based one. – Robin Jan 5 '10 at 15:39 There is nothing wrong with what you describe in context of a framework or platform that is specified and designed with such operations in mind. But that is not the JEE platform.
The whole point of JEE is adoption of constraints to promote distribution and availability (and the now forgotten goal of component orientation). Once you start breaking the constraints you are effectively on an self defeating path. So we must disagree on the point of rejecting misuse of technology as a case of a dogmatic view point ("black and white").
– alphazero Jan 5 '10 at 16:12.
There are all sorts of ways to do this. One, you can use an EJB Timer to create a run-once process that will start immediately. This is a good technique to spawn processes in the background.
A EJB Timer is associated with a specific Session Bean implementation. You can either add an EJB Timer to every Session Bean that you want to be able to do this, or you can have a single Session Bean that can then call your application logic through some dispatch mechanism. For me, I pass a serializable blob of parameters along with a class name that meets a specific interface to a generic Session Bean that then executes the class.
This way I can easily background most anything. One caveat about the EJB Timer is that EJB Timers are persistent. Once you create an EJB Timer is stays in the container until its job is finished or canceled.
The gotcha on this is that if you have a long running process, and the server goes down, when it restarts the process will continue and pick back up. Mind this can be a good thing, but only if your process is prepared to be restarted. But if your have a simple process iterating through "10,000 items", if the server goes down on item 9,999, when it comes back up you can easily see it simply starting over at item 1.It's all workable, just a caveat to be aware of.
Another way to background something is you can use a JMS queue. Put a message on the queue, and the handler runs aysnchronously from the rest of your application. The clever part here, and something I has also done leveraging the work with the Timer Bean, is you can control how many "jobs" will run based on how many MDB instances you configure the system to have.
So, for the specific task of running a process in multiple, parallel chunks, I take the task, break it up in to "pieces", and then send each piece on the Message Queue, where the MDBs execute them. If I allow 10 instances of the MDB, I can have 10 "parts" of any task running simultaneously. This actually works surprisingly well.
There's a little overhead it splitting the process up and routing it through the JMS queue, but that's all basically "start up time". Once it gets going, you get a real benefit. Another benefit of using the Message Queue is you can have your actual long running processes executing on a separate machine, or you can readily create a cluster of machines to handle these processes.
Yet, the interface is the same, and the code doesn't know the difference. I've found once you've relegated a long running process to the background, you can pay the price of having less-that-instant access to that process. That is, there's no reason to monitor the executing classes themselves directly, just have them publish interesting information and statistic to the database, or JMX, or whatever rather than having something that can monitor the object directly because it shares the same memory space.
I was easily able to set up a framework that lets task run either on the EJB Timer or on the MDB scatter queue, the tasks are the same, and I could monitor their progress, stop them, etc. You could combine the scatter technique to create several EJB Timer jobs. One of the free advantages of the MDB is it acts as a thread pool which can throttle your jobs (so you don't suddenly saturate your system with too many background processes). You get this "for free" just by leveraging the EJB management features in the container.
Finally, JEE 6 has a new "asynchronous" (or something) qualifier for Session Bean methods. I do not know the details on how this works, as I've yet to play with a new JEE 6 container. But I imagine you're probably not going to want to change containers just for this facility.
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.