what is JMS good for? [closed]

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.


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.

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.


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.


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