views:

576

answers:

5

I work on a data processing application in which concurrency is achieved by putting several units of work on a message queue that multiple instances of a message driven bean (MDB) listen to. Other then achieving concurrency in this manner, we do not have any specific reason to use the messaging infrastructure and MDBs.

This led me to think why the same could not have been achieved using multiple threads.

So my question is, in what situations can asynchronous messaging (e.g. JMS) be used as an alternative to mutithreading as a means to achieve concurrency ? What are some advantages/disadvantages of using one approach over another.

+2  A: 

It can't be used as an alternative to multithreading, it is a way of of implementing multithreading. There are three basic kinds of solutions here:

  1. You are responsible for both ends of the queue;
  2. You are responsible for sending data; or
  3. You are responsible for receiving data.

Receiving data is the kicker here because there's really no way of doing that without some form of multithreading/multiprocessing otherwise you'll only be processing one request at a time. Sending data without multithreading is much more viable but they're you're only really pushing the responsibility for dealing with those messages to an external system. So it's not an alternative to multithreading.

In your case with message driven beans, the container is creating and managing threads for you so it's not an alternative to multithreading, you're simply using someone else's implementation.

cletus
+2  A: 

Performance-wise multi-threading should be faster than any messaging, because you add an additional network layer with messaging.
Application-wise messaging helps you to avoid locking and data sharing issues as there is no common object.
From a scaling perspective messaging is a lot better as you can configure just more nodes on several server by configuring the message service instead of changing the application.

weismat
+1  A: 

Messaging can reduce number of errors in multithreaded applications greatly, since it reduces risk of data races. It also simplifies adding new threads without changing the rest of app.

Although I think JMS is slightly misused here. java.util.concurrent's thread-safe queues and libraries like jetlang may provide you better performance.

elder_george
+3  A: 

In an EJB container, actually, there is no alternative, since you're not allowed to create your own threads in an EJB container. JMS is doing all of that work for you, at a cost of running it through the queue processor. You could also create a Java Connector, which has a more intimate relationship with the container (and thus, can have threads), but it's a lot more work.

If the overhead of using the JMS queue isn't having a performance impact, then it's the easiest solution.

Will Hartung
technically you shouldn't create threads in a J2EE container but it's fairly common for people to do this inside Web containers. The J2EE spec should be viewed as more of a guideline than a rule. After all it disallows accessing the filesystem directly too but people do this all the time with config files and so on. Particularly when using Spring.
cletus
+3  A: 

There are two additional bonuses that I don't think has been mentioned: Transactions and durability.

While it isn't required and quite often isn't the default configuration, JMS providers can be configured to persist the messages and also to participate in a XA transaction with little or no code changes.

Gareth Davis
Yes, that is a very nice way to decouple systems/modules. It makes a system of system much more resilient to technical problems like server crashes.
Jeroen van Bergen