Overview
The Java Message Service (JMS) allows Java applications to send and receive messages asynchronously. It can be used for communication between server applications, but also within an application to postpone tasks or to distribute them to components.
In order to use JMS, you usually need some sort of messaging provider. This can be a stand-alone server, such as ActiveMQ or Websphere MQ, or a messaging implementation that is part of an application server like the JBoss Application Server. The latter is often the easiest solution, as long as sender and receiver both run inside the same application server.
When should JMS be used?
Use JMS when..
- you want to exchange messages reliably between servers in the same network (more)
- or, inside an application server, you want to split up work into smaller chunks (more)
- there is a direct connection between the message server and its clients (no firewalls etc)
Alternatives
The main alternative to JMS are RPC systems that allow you to invoke functions on remote systems. For client/server models they are almost always easier to use. On the other hand, making a message exchange reliable can be quite a challenge with RPCs.
- SOAP with JAX-WS is the right choice when you need to get through web proxies and firewalls, or when interoperability with non-Java systems is important.
- RMI, Java's built-in RPC mechanism, will get you started quickly and requires little initial effort. It works with Java endpoints only though, and there are some serious problems you need to consider (more).
- CORBA, either via Java's CORBA APIs or with RMI-IIOP, is old and complicated. Don't use unless you are required to.
What do you need for JMS?
For JMS you need...
- a messaging server, for example ActiveMQ or Websphere MQ. Or a messaging engine integrated into an application server such as JBoss'. There are also some peer-to-peer JMS system, e.g. Mantaray.
- a JMS provider for your server (basically a driver for your server)
Destinations are the things you send messages to and receive messages from. There are two kinds of destination defined in the JMS standard:
- Queues allow only one receiver per message. If the message is read and acknowledged, it will be removed from the queue.
- Topics allow several subscribers to receive messages. A message stays in the Topic until all subscribers read and acknowledged it (more).
Both topics and queues have in common that there can be several senders, and that messages will always be received in the order they have been sent.