- Introduction to Messaging
- Components of the JMS Architecture
- The Two JMS Message Models
- The JMS Interfaces
- The Details of a JMS Message
- Message Selection and Filtering
- Using the JMS Point-to-Point Model
- Using the JMS Publish/Subscribe Model
- Synchronous Versus Asynchronous Messaging
- Message Persistence
- Using Transactions with JMS
- Using JMS with Enterprise JavaBeans
The Two JMS Message Models
JMS supports two different ways of using messaging between a producer and consumer. The two messaging models are known as
PTP (Point-to-Point)Message delivered to one recipient.
Pub/Sub (Publish/Subscribe)Message delivered to multiple recipients.
JMS supports these two models by using distinct interfaces within the API for each messaging model. The javax.jms.Queue interface handles the PTP messaging model, while the Pub/Sub model is handled by the javax.jms.Topic interface. Other interfaces work closely with one or the other or both of these primary interfaces to help implement the messaging model. Both the Queue and Topic interfaces extend the javax.jms.Destination interface. The JMS interfaces are covered in detail in "The JMS Interfaces," later in this chapter. For now, let's describe how each messaging model is unique.
The PTP model is used to enable an application to send a message and have the message received by a single consumer. The PTP model is supported in JMS by a queue. A queue allows messages to come in and be consumed by a single receiver. After a particular consumer takes the message off the Queue, the message is no longer available for other consumers. Figure 10.3 illustrates the PTP messaging model.
Figure 10.3 A single consumer receives the message when a Point-to-Point (PTP) messaging model is used.
With the PTP model, a message producer is referred to as a QueueSender and a message consumer is referred to as a QueueReceiver. A messaging application can have multiple senders and receivers, but a message from a sender will only be delivered to a single receiver in the PTP model. You'll see that this is not the case with the Pub/Sub model described next.
The Pub/Sub message model in JMS is provided by a Topic. In the JMS Pub/Sub model, producers and consumers connect to a topic and send and receive messages respectively. A message producer in the Pub/Sub model is called a TopicPublisher and a message consumer is referred to as a TopicSubscriber. Figure 10.4 illustrates the Pub/Sub message model.
Figure 10.4 Multiple consumers can receive a copy of the message using a Publish/Subscribe messaging model.
Unlike the PTP message model, the Pub/Sub message model allows multiple recipients to receive the same message. The subscribers that are interested in a particular topic can subscribe to that topic and receive a copy of the message when one arrives at the destination.