Home > Articles > Programming > Java

  • Print
  • + Share This
This chapter is from the book

This chapter is from the book

Messaging Domain Models

JMS supports two types of messaging models (also referred to as domain models) that are common in the messaging world: Point-to-Point (P2P) and Publish/Subscribe (pub/sub). Not all messaging middleware supports both models, and the JMS 1.02 specification doesn't require the JMS provider to support both models. Regardless of the messaging model, JMS clients exchange messages by sending or retrieving messages to and from a virtual address termed the "destination." JMS providers are responsible for destination services.

Point-to-Point Model

The P2P messaging model consists of message senders, receivers, queues, and the messages. A JMS client that generates messages is called the sender; a JMS client that consumes messages is called the receiver. In the P2P model, a sender sends a message to a destination called the queue; a receiver retrieves the message from the same queue, as shown in Figure 12-4.

Figure 12-4 A message sent to a queue being consumed by only one receiver

Figure 12-4 illustrates the P2P messaging model with three senders sending messages to a destination queue called OrderQueue and the two receivers each retrieving their own message. The P2P messaging model is applicable in cases where messages should be acted upon only once. For example, a company may have its distribution warehouse in a state different from its headquarters. When customers order products, the application sends the order messages to a JMS client at the remote warehouse where the order is processed and shipped.

The P2P domain model has several significant characteristics:

  • The P2P model is referred to as supporting either one-to-one or many-toone relationships. If you look at the sender-receiver relationship where many JMS senders can send messages to a particular JMSQueue destination, the message can only be consumed by a single receiver. On the other hand, if you look at the message-receiver relationship, there can only be one receiver per message, resulting in a one-to-one relationship. The P2P model can be characterized as supporting a many-to-one relationship between senders and a single receiver.

  • Each message can have only one receiver. Therefore, multiple senders can send messages to a queue and multiple receivers can access the queue, but only one receiver can consume a message from that queue.

  • Once a message is consumed by a receiver, it's removed from the queue. Thus, in the P2P model, messages are guaranteed to be delivered to a receiver once and only once.

  • Messages are ordered. For example, a queue delivers messages to consumers in the order in which they were placed by the provider.

  • Senders and receivers can be added dynamically at runtime, thus allowing the system to grow or shrink with the demand.

Publish and Subscribe Model

The pub/sub model consists of message publishers, subscribers, and topics. A message producer is called a publisher; a message consumer is called a subscriber. The destination where a publisher sends messages and the subscribers retrieve the messages is called the topic. The pub/sub model is based on the concept of nodes in a content hierarchy, where a publisher publishes messages to a destination and the messages are broadcast to all registered subscribers. The pub/sub model supports many-to-many or one-to-many relationships. When considering a publisher-subscriber relationship, there can be many publishers sending messages to many subscribers, resulting in a many-to-many relationship. On the other hand, when considering a message-subscriber relationship, a message sent by a publisher is distributed to all registered subscribers resulting in a one-to-many relationship. The pub/sub model can be characterized as supporting a many-to-many relationship between senders and receivers. Note that every client that subscribes to a topic receives its own copy of the messages published to that topic. Messages are pushed to the registered subscribers without having to request them. Also, publisher and subscribers can be dynamically added at runtime, and a message delivery in pub/sub is once or not at all. Figure 12-5 is an illustration of the pub/sub messaging model in which many publishers are publishing messages to a destination topic, StockQuoteTopic. Each of these messages is distributed to many subscribers. The pub/sub model is applicable when one wants to broadcast a message to many clients simultaneously. For example, a financial website can publish the latest stock quotes to a topic; clients that have subscribed to a specific topic can automatically receive the latest stock price information.

Figure 12-5 A message sent to a topic being broadcast to one or more registered subscribers

Message Delivery

JMS message consumers, whether P2P or pub/sub, can choose to have messages delivered to them asynchronously by having the JMS provider push messages, or they may receive the messages synchronously by connecting to the JMS provider and receiving, or pulling, the messages from the destination. Pull delivery requires the JMS consumer to either stay connected to the JMS provider and poll the destination for messages or connect to the JMS provider and retrieve messages from the destination per regularly scheduled intervals. Note that polling the destination for messages adds extra overhead to the system. Push delivery is simpler as the JMS provider automatically delivers messages to registered JMS consumers. Developers incorporate the pull or the push delivery mechanism based on the business logic requirements. Note that the communication between the producer client and the JMS provider is synchronous—a message producer (JMS client producer) connects to the JMS provider, sends messages, and receives acknowledgment from the provider to complete message delivery. In contrast, message consumers can either receive messages synchronously as they arrive at the destination (if the pull option is used) or asynchronously if the push option is used in the client code. Regardless of the options used, message delivery from producer to consumer can only be asynchronous. Figure 12-6 illustrates the push/pull model where a message producer client sends messages to a destination.

Figure 12-6 JMS architecture with interaction among clients, provider, and messages

Guaranteed Message Delivery

JMS messages can be marked as either persistent or nonpersistent. In both cases, a JMS provider will attempt to deliver the message to the consumer until the JMS consumer either receives the message and acknowledges it, or until the message expires. The difference between a persistent and a nonpersistent message is significant, yet subtle. If a message is marked as persistent, the JMS provider saves the message on the disk before acknowledging to the message producer and uses a store-and-forward mechanism. Delivery is attempted until the JMS message consumer receives the message and acknowledges it, or until the message expires. Even if the provider crashes before the message is delivered to the consumer, the provider will attempt to resend the message after the provider is rebooted. With nonpersistent messaging, the JMS provider doesn't save the message to disk before sending an acknowledgment to the message producer. For this reason, if the JMS provider were to experience a system crash before delivery of the message, the nonpersistent message would be lost.

In a pub/sub model, a subscriber can either be durable or nondurable. Messages are delivered to nondurable subscribers only if they're connected to the provider. With durable subscribers, the provider is responsible for storing the messages and will deliver unexpired messages when durable subscribers connect later. A message can be marked persistent and durable or nonpersistent and durable—there's a subtle but important difference. In the case of a nonpersistent and durable message, the provider attempts to deliver the message, and if the subscriber isn't active, then the provider will store the message for future delivery attempts. If the provider crashes before it's able to store the message, then the message is lost and will never be delivered. This won't happen with the persistent and durable message because when the message is received from the publisher it's already saved on disk and will attempt to deliver the durable message on reboot. Making a message durable and/or persistent has consequences for processing and system resources. For example, confirmation of purchased messages could be marked persistent and durable because that message cannot afford to be lost. However, stock price update broadcast messages every few minutes could be nonpersistent and nondurable, because most people (except for day traders) can afford to lose a message or two without adversely affecting their net worth.

Loose Coupling and Asynchronous Communication

In the JMS messaging architecture, the JMS clients can produce or consume messages or do both. The JMS provider is responsible for routing and delivery of messages. The JMS clients are loosely coupled and asynchronous; neither JMS client needs to be aware of the other, nor must they be running simultaneously for messages to be exchanged. JMS clients don't communicate directly but via the destination service provided by the JMS provider. Producers send messages to a destination, where they are held until consumers retrieve messages or the messages expire.

Even though the communications between JMS clients are asynchronous, the communication between producers and a JMS provider are synchronous, and the communication between consumers and a JMS provider are synchronous or asynchronous. When JMS clients send or receive messages from JMS provider destinations, they go through a receive-acknowledge handshake. Refer back to Figure 12-6 for an illustration.

Loose coupling and asynchronous communication in a JMS application give enterprise applications flexibility when implementing business workflow processes. Let's look at an e-commerce application example. During checkout, after the shopping cart component sends a persistent and durable message to a JMS provider OrderQueue destination, the component can continue servicing other customers and not worry about what happens next. An optimized order processing application retrieves the message from the OrderQueue destination and processes it. Per the application business logic that requires minimizing shipping costs, reduced delivery time, and maximum profit, the application forwards this durable message to the NYWarehouseQueue destination nearest the customer's shipping address. The shipping application then retrieves the message from the NYWareHouseQueue, extracts the necessary information, and mails the product, as well as automatically sending an e-mail with a tracking order number to the customer. As the JMS application is loosely coupled and relies on asynchronous communication, the shopping cart application is unaware of the optimized order processing application. As a result, if the shipping application and order processing go down for a while, the persistent and durable message is still delivered. When the two failed applications resume operating, the order can be completed and the products shipped.

  • + Share This
  • 🔖 Save To Your Account