An Overview of Asynchronous Programming With Java Message Service
Many WebSphere* Application Server (WAS) customers are creating leading-edge applications using the core Java*2 Platform Enterprise Edition (J2EE) technologies: servlets, Java ServerPages* (JSPs) and Enterprise JavaBeans* (EJBs). These technologies are often sufficient for creating applications that interface directly with users; however, another J2EE technology is provided in WAS 4.0 that could open a new world of application possibilities-Java Message Service (JMS) and asynchronous messaging.
JMS is a Java specification from Sun* Microsystems that defines an API for accessing asynchronous messaging systems from Java applications. This article describes the origins of asynchronous messaging and JMS, the key capabilities provided by the JMS specification, the JMS support provided by WAS 4.0 and the benefits in using JMS in your applications. WAS 4.0 supports the JMS specification, but customers must install some additional software (e.g., a JMS provider) to use JMS from WAS 4.0. This is covered in more detail later in this article.
A Messaging Example
Typical interactive applications provide a Web-based user interface via a browser. For example, an order-entry application displays a Web page allowing users to select a product, enter a quantity and specify their personal information (i.e., name, shipping address, billing address and payment information). This order-entry Web page may be implemented as a JSP. When the Submit Order button is pressed, the JSP invokes a servlet and EJB, creating a new order record in the Order database table, decrementing the available inventory in the ProductInventory table, etc. (see Figure 1). All of these tasks occur synchronously, meaning users must wait for all tasks to complete before control is returned to their browser session.
Extending this scenario to involve multiple applications illustrates a shortcoming of synchronous applications. For example, if the servlet or EJB interacts with a sales application, the order-entry application interacts with a sales application to notify a salesperson that a customer placed an order for a product in the catalog. This allows the salesperson to place a follow-up call with the customer to describe other products of potential interest.
One application design has the order-entry servlet or EJB invoke a servlet or EJB component in the sales application to create a new record in the SalesContact table. The salesperson then uses the application interface to display a list of customers and the products ordered (see Figure 2). The problem is that the customer incurs additional overhead while the sales information is inserted into the sales database, because these application interactions occur synchronously.
Using JMS and asynchronous messaging to integrate the two applications improves the design. The order-entry application places a message containing order information on a JMS queue. The application continues processing without waiting for the message to be processed by the sales application. The sales application receives the new message from the JMS queue and updates the sales database. The information then is available to the salesperson using the sales application (see Figure 3). By refreshing a browser, the salesperson sees the new contact information as it arrives on the JMS queue. In this respect, both applications are more loosely coupled than in the synchronous example. This is an example of how using JMS simplifies application integration and improves application performance by removing application blocking.
comments powered by