Messaging
A message is a unit of serializable data exchanged between two or more distributed components running in the same machine or different machine. By using a message-oriented middleware (MOM) infrastructure, an application can create, send, and receive messages. This lets you combine separate business components into a reliable, yet flexible system. Several vendors provide MOM, and in J2EE, Java Messaging Service (JMS) offers a generic way to access these systems. In this section, let's explore the different messaging modes and models and learn how to choose the correct one for a given scenario.
There are two modes of communication depending on the level of coupling between the sender and receiver:
- Synchronous
- Asynchronous
A distributed component sends a message to another active component and waits for the reply (also known as blocking call) to proceed further. The synchronous communication is tightly coupled because both the sender and receiver have knowledge about each other. The sender is responsible for retries in case of failures.
The benefits of synchronous communication are:
- This mode is fail-safe and is used for transaction processing.
- Sender can receive the response immediately in realtime.
- When multiple messages are sent, they reach the destination in the same order in which they are sent.
- It is a reliable communication mode.
You can use this mode when the sender:
- Wants to have more control over the message.
- Needs real-time response.
- Wants to maintain the order of message processing.
- Wants to retry in case of message failure.
A distributed component can send messages to any other component via MOM and continue its processes without waiting for the response. This communication mechanism is loosely coupled, where sender and receiver need not have knowledge about each other because a central intermediary, the MOM, exists. Messages can arrive at the destination in any sequence and not necessarily in same order in which they are sent. MOM is responsible for retry in case of failure in the communication.
The benefits of asynchronous communication are:
- Sender need not wait till the message gets processed. The responsibility is delegated to the MOM.
- Messages can be queued. Sender and receiver need not be always available to receive the messages.
- It is loosely coupled because the sender and receiver do not directly communicate; they communicate through MOM.
You can use this mode when the sender:
- Wants to broadcast the message.
- Needs no response, or the response is not needed immediately.
- Wants to use the system hardware efficiently.
- Wants to do transaction processing in high volume.
There are two types of messaging models depending on whether you require one-to-one message delivery or a one-to-many broadcast delivery. The models are:
- Point-to-point messaging. In this model, the sender sends the messages to a destination known as queue, and the receiver consumes the message from the queue. A queue is designated to only one receiver. More than one sender can send the messages to a queue, but only one consumer receives that message from the queue. The messages in the queue are processed on a first-in-first-out (FIFO) basis. The messages stay in the queue until they are consumed by the consumer or until the messages' expiry time. A sender can also send the message directly to the consumer instead of placing it in the queue. A consumer can acknowledge the successful processing of the message.
- Publish-subscribe messaging. In this model, publishers publish the messages to a topic. The subscribers who subscribe to that topic then receive the message. The message stays in the topic until it is sent to the active subscribers. If some subscribers are not active, the messages are not delivered to them. There is a special type of subscription known as a durable subscription in which the messages will not be lost if the subscriber is not active. Rather, the messages are delivered once the subscriber becomes active.
The following table summarizes the technologies you can use for different scenarios.
| Technology | Scenarios |
|---|---|
| Messaging |
|
| EJB |
|
| Messaging and EJB |
|
In this section, we discussed the different types of communication modes and messaging models used in enterprise systems for messaging. We also discussed their benefits and when to use these different communication modes. In the exam, given a scenario, you should be able to identify which technologies you can use for the particular scenario. Remember that if the scenario calls for one-to-one messaging then it is a candidate for the point-to-point messaging model. On the other hand, if there are numerous receivers, the best method for implementation is the publish-subscribe model.
Which of the following choices describe asynchronous messaging?
Choices:
- A. Loose coupling between sender and receiver
- B. Blocks until message is processed
- C. Suitable for transaction processing
- D. The network is not required to be available
Correct choice:
A and D
Explanation:
Choices A and D are the correct answers.
Asynchronous messaging is loosely coupled because the sender and receiver do not directly communicate; they use MOM for communication. Hence, choice A is correct.
Because MOM takes care of delivering the messages if the receiver is not available, the network is not required to be constantly running. Thus, choice D is correct.
Only synchronous messaging blocks the sender until the receiver processes the message. Therefore, choice B is incorrect.
Because transaction processing requires immediate response, only synchronous messaging is suitable for implementing it. Hence, choice C is incorrect.




