How can an EJB parallelize a long, CPU intensive process?
Asked Answered
J

6

14

The application has a CPU intensive long process that currently runs on one server (an EJB method) serially when the client requests it.

It’s theoretically possible (from a conceptual point of view) to split that process in N chunks and execute them in parallel, as long as the output of all parallel jobs can be collected and joined together before sending it back to the client that initiated the process. I’d like to use this parallelization to optimize performance.

How can I implement this parallelization with EJBs? I know that we should not create threads in a EJB method. Instead, we should publish messages (one per job) to be consumed by message driven beans (MDBs). But then it would not be a synchronous call anymore. And being synchronous seems to be a requirement in this case since I need to collect the output of all jobs before sending it back to the client.

Is there a solution for this?

Jammie answered 5/1, 2010 at 12:25 Comment(0)
S
6

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 Java EE 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

Spoony answered 5/1, 2010 at 15:33 Comment(2)
It seems that WorkManager is WebSphere specific, so unsuitable in general: stackoverflow.com/questions/9026516/…Sprightly
That is not referring to the commonj WorkManager, but an IBM specifig solution that predates it. As I already already mentioned commonj is not IBM specific and is available in most platforms.Spoony
A
11

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, Java EE 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 Java EE 6 container. But I imagine you're probably not going to want to change containers just for this facility.

Artemisa answered 5/1, 2010 at 16:2 Comment(0)
S
6

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 Java EE 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

Spoony answered 5/1, 2010 at 15:33 Comment(2)
It seems that WorkManager is WebSphere specific, so unsuitable in general: stackoverflow.com/questions/9026516/…Sprightly
That is not referring to the commonj WorkManager, but an IBM specifig solution that predates it. As I already already mentioned commonj is not IBM specific and is available in most platforms.Spoony
S
4

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 Java EE system architecture specs.

Swung answered 5/1, 2010 at 12:44 Comment(4)
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.Spoony
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").Swung
"request/reply semantics... an event based architecture with a messaging back end" I see your point if the request sematnics are "perform long term process" and the reply is "long term process finished". But would it fit within the JEE model if the request means "start long term process" and the reply is "long term process queued"? Then, as another answer suggests, the EJB can simply send a message using JMS to get the job done by an MDB (or even a non-JEE consumer of the message).Sprightly
The lack of support for long running processes is a long standing gap in the JEE architecture (that was not addressed by MDBs), but in fairness, it is not clear to me how one can reconcile transparent distribution, COA, and demanding SLAs with such elements. JEE overall addresses this via Connector Architecture and presumably you will supply the transactional execution engine (EIS) that handles the long running process(es), etc.Swung
J
2

Back to the Future - Java EE 7 has lot more Concurrency support via ManagedThreadFactory, ManagedExecutor service etc (JSR 236: Concurrency Utilities for Java EE) with which you can create your own 'managed'Threads .It is no longer a taboo in EE AS supporting it (Wildfly ?) via usining the ManagedThread* API's

More details

https://jcp.org/aboutJava/communityprocess/ec-public/materials/2013-01-1516/JSR236-EC-F2F-Jan2013.pdf http://docs.oracle.com/javaee/7/tutorial/doc/concurrency-utilities002.htm

Jeremyjerez answered 4/4, 2014 at 11:47 Comment(2)
In the end where I needed to work with JBoss AS 7.1 and faced with the task of parallel execution without threads, I followed automatically the message based architecture containing a master task split into subtasks; using Jboss and HorneQ clustering and load balancing; posted in GitHub the frameworkJeremyjerez
JBoss AS 7.1 already implements EJB 3.1 specification. You can use Asynchronous method invocation for simple use-cases. Don't forget to configure the thread pool on the applications server to suit your needs.Sternberg
D
1

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.

Delphinedelphinia answered 5/1, 2010 at 12:31 Comment(1)
Not "disrecommended", flat out not permitted: https://mcmap.net/q/18603/-why-is-spawning-threads-in-java-ee-container-discouragedSprightly
C
1

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...

Communication answered 5/1, 2010 at 12:41 Comment(1)
Look up the WorkManager from the CommonJ API. It allows you to do this in managed threads.Spoony

© 2022 - 2024 — McMap. All rights reserved.