what is JMS good for?

JavaJmsMessaging

Java Problem Overview


I'm looking for (simple) examples of problems for which JMS is a good solution, and also reasons why JMS is a good solution in these cases. In the past I've simply used the database as a means of passing messages from A to B when the message cannot necessarily be processed by B immediately.

A hypothetical example of such a system is where all newly registered users should be sent a welcome e-mail within 24 hours of registration. For the sake of argument, assume the DB does not record the time when each user registered, but instead a reference (foreign key) to each new user is stored in the pending_email table. The e-mail sender job runs once every 24 hours, sends an e-mail to all the users in this table, then deletes all the pending_email records.

This seems like the kind of problem for which JMS should be used, but it's not clear to me what benefit JMS would have over the approach I've described. One advantage of the DB approach is that the messages are persistent. I understand that JMS message queues can also be persisted, but in that case there seems to be little difference between JMS and the "database as message queue" approach I've described?

What am I missing?

  • Don

Java Solutions


Solution 1 - Java

JMS and messaging is really about 2 totally different things.

  • publish and subscribe (sending a message to as many consumers as are interested - a bit like sending an email to a mailing list, the sender does not need to know who is subscribed
  • high performance reliable load balancing (message queues)

See more info on how a queue compares to a topic

The case you are talking about is the second case, where yes you can use a database table to kinda simulate a message queue.

The main difference is a JMS message queue is a high performance highly concurrent load balancer designed for huge throughput; you can send usually tens of thousands of messages per second to many concurrent consumers in many processes and threads. The reason for this is that a message queue is basically highly asynchronous - a good JMS provider will stream messages ahead of time to each consumer so that there are thousands of messages available to be processed in RAM as soon as a consumer is available. This leads to massive throughtput and very low latency.

e.g. imagine writing a web load balancer using a database table :)

When using a database table, typically one thread tends to lock the whole table so you tend to get very low throughput when trying to implement a high performance load balancer.

But like most middleware it all depends on what you need; if you've a low throughput system with only a few messages per second - feel free to use a database table as a queue. But if you need low latency and high throughput - then JMS queues are highly recommended.

Solution 2 - Java

In my opinion JMS and other message-based systems are intended to solve problems that need:

  • Asynchronous communications : An application need to notify another that an event has occurred with no need to wait for a response.
  • Reliability. Ensure once-and-only-once message delivery. With your DB approach you have to "reinvent the wheel", specially if you have several clients reading the messages.
  • Loose coupling. Not all systems can communicate using a database. So JMS is pretty good to be used in heterogeneous environments with decoupled systems that can communicate over system boundaries.

Solution 3 - Java

The JMS implementation is "push", in the sense that you don't have to poll the queue to discover new messages, but you register a callback that gets called as soon as a new message arrives.

Solution 4 - Java

to address the original comment. what was originally described is the gist of (point-to-point) JMS. the benefits of JMS are, however:

  1. you don't need to write the code yourself (and possibly screw up the logic so that it's not quite as persistent as you think it is). also, third-party impl might be more scalable than simple database approach.

  2. jms handles publish/subscribe, which is a bit more complicated that the point-to-point example you gave

  3. you are not tied to a specific implementation, and can swap it out if your needs change in the future, w/out messing w/ your java code.

Solution 5 - Java

One advantage of JMS is to enable asynchronous processing which can by done by database solution as well. However following are some other benefit of JMS over database solution

a) The consumer of the message can be in a remote location. Exposing database for remote access is dangerous. You can workaround this by providing additional service for reading messages from database, that requires more effort.

b) In the case of database the message consumer has to poll the database for messages where as JMS provides callback when a message is arrived (as sk mentioned)

c) Load balancing - if there are lot of messages coming it is easy to have pool of message processors in JMS.

d) In general implementation via JMS will be simpler and take less effort than database route

Solution 6 - Java

JMS is an API used to transfer messages between two or more clients. It's specs are defined under JSR 914.

>The major advantage of JMS is the decoupled nature of communicating entities - Sender need not have information about the receivers. Other advantages include the ability to integrate heterogeneous platforms, reduce system bottlenecks, increase scalability, and respond more quickly to change.

JMS are just kind of interfaces/APIs and the concrete classes must be implemented. These are already implemented by various organizations/Providers. they are called JMS providers. Example is WebSphere by IBM or FioranoMQ by Fiorano Softwares or ActiveMQ by Apache, HornetQ, OpenMQ etc. .Other terminologies used are Admin Objects(Topics,Queues,ConnectionFactories),JMS producer/Publisher, JMS client and the message itself.

So coming to your question - what is JMS good for? I would like to give a practical example to illustrate it's importance.

Day Trading

There is this feature called LVC(Last value cache)

In Trading share prices are published by a publisher at regular intervals. Each share has an associated Topic to which it is published to. Now if you know what a Topic is then you must know messages are not saved like queues. Messages are published to the subscribers alive at the time the message was published(Exception being Durables subscribers which get all the messages published from the time it was created but then again we don't want to get too old stock prices which discard the possibility of using it). So if a client want to know a stock price he create a subscriber and then he has to wait till next stock price is published(which is again not what we want). This is where LVC comes into picture. Each LVC message has an associated key. If a messages is sent with a LVC key(for a particular stock) and then another update message with same key them the later overrides the previous one. When ever a subscriber subscribes to a topic(which has LVC enabled) the subscriber will get all the messages with distinct LVC keys. If we keep a distinct key per listed company then when client subscribes to it it will get the latest stock price and eventually all the updates.

Ofcourse this is one of the factors other that reliability,security etc which makes JMS so powerful.

Solution 7 - Java

Guido has the full definition. From my experience all of these are important for a good fit.

One of the uses I've seen is for order distribution in warehouses. Imagine an office supply company that has a fair number of warehouses that supply large offices with office supplies. Those orders would come into a central location and then be batched up for the correct warehouse to distribute. The warehouses don't have or want high speed connections in most cases so the orders are pushed down to them over dialup modems and this is where asynchronous comes in. The phone lines are not really that important either so half the orders may get in and this is where reliability is important.

Solution 8 - Java

The key advantage is decoupling unrelated systems rather than have them share comon databases or building custom services to pass data around.

Banks are a keen example, with intraday messaging being used to pass around live data changes as they happen. It's very easy for the source system to throw a message "over the wall"; the downside is there's very little in the way of contract between these systems, and you normally see hospitalisation being implemented on the consumer's side. It's almost too loosly coupled.

Other advantages are down to the support for JMS out of the box for many application servers, etc. and all the tools around that: durability, monitoring, reporting and throttling.

Solution 9 - Java

There's a nice write-up with some examples here: http://www.winslam.com/laramee/jms/index.html

Solution 10 - Java

The 'database as message queue' solution may be heavy for the task. The JMS solution is less tightly coupled in that the message sender does not need to know anything about the recipient. This could be accomplished with some additional abstraction in the 'database as message queue' as well so it is not a huge win...Also, you can use the queue in a 'publish and subscribe' way which can be handy depending on what you are trying to accomplish. It is also a nice way to further decouple your components. If all of your communication is within one system and/or having a log that is immediately available to an application is very important, your method seems good. If you are communicating between separate systems JMS is a good choice.

Solution 11 - Java

JMS in combination with JTA (Java Transaction API) and JPA (Java persistence API) can be very useful. With a simple annotation you can put several database actions + message sending/receiving in the same transaction. So if one of them fails everything gets rolled back using the same transaction mechanism.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDónalView Question on Stackoverflow
Solution 1 - JavaJames StrachanView Answer on Stackoverflow
Solution 2 - JavaGuidoView Answer on Stackoverflow
Solution 3 - Javask.View Answer on Stackoverflow
Solution 4 - JavajamesView Answer on Stackoverflow
Solution 5 - JavaRejeev DivakaranView Answer on Stackoverflow
Solution 6 - JavaAniket ThakurView Answer on Stackoverflow
Solution 7 - JavacarsonView Answer on Stackoverflow
Solution 8 - JavaPEELYView Answer on Stackoverflow
Solution 9 - JavaKennyGView Answer on Stackoverflow
Solution 10 - JavaIchorusView Answer on Stackoverflow
Solution 11 - JavaCyberroadieView Answer on Stackoverflow