Chapter 23
872
q
A more complicated JMS example
q
Message-Driven EJBs
Comparison of message-driven beans and ordinary JMS message consumers
Lifecycle of a message-driven bean
Example of a message-driven bean
Message-driven bean clients
Messaging in General
E-mail is a type of person-to-person communication. When you send an e-mail message to your friend
your e-mail server makes sure your message arrives safely. Whereas e-mail is a method of
communication between people, messaging, in the context of this chapter, is a method of
communication between software applications or software components.
Message-Oriented Middleware (MOM) is the equivalent of the servers in our e-mail example. A MOM
product enables messaging between applications or components of an application that are connected by
a network. MOM products are responsible for delivering messages within the system, as well as
sometimes providing transactional support, tolerance, load balancing, and scalability services for large
enterprise systems that use messaging.
In distributed systems, different parts of the application usually communicate with synchronous
procedure calls. The client invokes a method on an object on a server and must wait until the method
invocation returns to continue processing. Imagine if, whenever you sent an e-mail message, you had to
wait until the recipient opened the message and responded before you could use your computer to do
anything else. This is what it would be like if e-mail messages were like synchronous procedure calls.
Asynchronous Messaging
In contrast to the above scenario, MOM systems provide asynchronous messaging (some, including JMS,
also provide synchronous messaging). When a client sends a message to the common agent it continues
processing, without waiting for a response to its message. The message recipient retrieves the message
from the common agent without ever coming into direct contact with the client who sent the message.
Luckily e-mail systems are asynchronous. When you send an e-mail message you can work on a
document, write some code, or send another e-mail message while you are waiting for a response to the
original message. The messaging system architecture generally replaces the client-server model with a
peer-to-peer relationship among individual components, where each peer can send and receive
messages to and from other peers.
In enterprise applications there are many situations in which messaging is useful. Any situation in which
one part of an application wants to communicate with another part of the application without waiting
for a response is a good situation in which to use messaging.
For example, consider a typical online store application, written on the J2EE platform, which has a
ShoppingCart session bean and an InventoryManagement session bean. When a customer makes a
purchase, the ShoppingCart alerts the InventoryManagement bean. In an application without
messaging, the ShoppingCart bean would make a function call to the InventoryManagement bean to
tell it what products the customer has purchased. Before the ShoppingCart can continue to process the
customer's purchase order it must wait for the procedure call to the InventoryManagement bean to return.