When is messaging (e.g. JMS) an alternative for multithreading?
Asked Answered
T

6

6

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

Trochilus answered 20/10, 2009 at 6:12 Comment(1)
in most cases, asynchronous messaging is multi-threadingPulliam
P
6

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

Pretermit answered 20/10, 2009 at 6:33 Comment(0)
P
5

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.

Prepositor answered 20/10, 2009 at 6:49 Comment(1)
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.Snooty
G
4

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.

Germangermana answered 20/10, 2009 at 6:39 Comment(1)
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.Pretermit
O
3

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.

Oswin answered 20/10, 2009 at 6:35 Comment(0)
G
1

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.

Graubert answered 20/10, 2009 at 6:37 Comment(0)
A
1

Using multi-threading you can achieve concurrency by sharing core of CPU. But if you use JMS instead you can balance the load and can delegate the task to other system. e.g. Suppose your application demands to send email on completion of certain task. And you want to send email concurrently. Either you can pull a thread and process it asynchronously. Or you can delegate this task of mail sending to other system using JMS. No of receiver threads can be configurable in jms. Also multiple nodes can listen to same JMS queue which balance the loads. And you can use further applications like persistent queue, transaction managed queue as per application.

In simple words, JMS can be better alternative to multi-threading depends on application architecture

Aquiline answered 28/1, 2019 at 12:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.