J2EE Messaging
873
Within the procedure the InventoryManagement bean has to update the data source (most likely it
uses entity beans to access a database), which takes time. The method invocation itself also takes time.
Waiting for the procedure to return may take quite a few seconds, and most likely the customer doesn't
want to wait.
In a messaging-enabled application the ShoppingCart needs only to send off a message to the
InventoryManagement bean. Once the ShoppingCart sends the message it can continue to process
the customer's order without waiting for a response from the InventoryManagement bean. The
following figure shows the difference between communication by synchronous procedure calls and
asynchronous messaging in this example. The figure uses the concept of a TeddyBearStock entity
bean that represents the teddy bear stock at the online store's warehouse:
Communication via synchronous procedure calls
Communication via asynchronous messaging
1. A customer
purchased two
teddy bears
2. Remove two
teddy bears
from stock
3. Remove two
teddy bears from
inventory table
4. Success
5. Success
6. Success
Database
Shopping Cart
Teddy Bear
Stock
Inventory
Management
1. A customer
purchased two
teddy bears
2. Remove two
teddy bears
from stock
3. Remove two
teddy bears from
inventory table
4. Success
5. Success
Database
Shopping Cart
Teddy Bear
Stock
Inventory
Management
In the top diagram the ShoppingCart invokes a method on the InventoryManagement bean. The
ShoppingCart must wait until the procedure call returns in Step 6 to continue processing. In the
bottom diagram the ShoppingCart sends a message to the InventoryManagement bean and can
continue processing right after Step 1.
You might notice that in the messaging version the ShoppingCart never gets confirmation that the
purchase is correctly registered in the database. This might be appropriate system behavior. If it is not
appropriate, for example if business logic dictates that the customer may not finish the checkout until the
database has been updated then the ShoppingCart needs to be alerted when the database is updated.
In such a case we may choose to use procedure calls or synchronous messaging to communicate
between the beans or we can send a confirmation message from the InventoryManagement bean.
This way the ShoppingCart can continue to process the order while waiting for the success message
from the InventoryManagement bean.