Nov. 6, 2003 12:00 AM
In the case of J2EE applications that rely on the services provided by the application server containers, implementing parallel and concurrent processing is an uphill battle. Over time, the Java messaging services (JMS) API has become a de facto solution for parallel business logic processing by utilizing the asynchronous messaging features offered by the API.
Although JMS provides a solid foundation for developing J2EE applications that use asynchronous messaging, it does not automatically produce the desired end result. JMS poses the following challenges:
Best practices disapprove of the scattering of complex JMS code throughout an application. Because of these challenges, a reusable, flexible, and robust framework must be built to enable J2EE applications to exploit the asynchronous JMS features in order to achieve efficient parallel business logic processing. One such framework can be implemented by using JMS 1.0.2 and support for EJB 2.0 message-driven beans in the BEA WebLogic 6.1 Application Server.
A real-world example of this design strategy resulted in a reusable framework that can be used in any J2EE application development scenario that requires an asynchronous mode of execution. This particular instance employed a black-box strategy by utilizing traditional proxy and command patterns.
Typical Approach
When possible solutions to parallel business logic processing are considered, the first approach that comes to mind is spawning of threads. Multiple threads can be created to run each business task. This may work fine for simple applications; however, for enterprise applications that involve critical customer transactions and the exchange of huge chunks of data between multiple diverse systems, thread spawning may not be a robust solution. This type of design can lead to an interminable pursuit of perfecting the implementation itself because of the challenges involved in the management of thread life cycle, maintenance of the thread pool, management of transaction atomicity, and elimination of concurrency issues. An ounce of prevention is worth a pound of cure, so eschew this approach in the first place and follow a robust design from the outset.
Design Summary
Multiple different strategies were considered for implementing such a framework. Certainly there could be myriad implementation approaches, but the best approach is the one that takes advantage of the underlying application server. One such approach was based on leveraging BEA WebLogic Server's high-performance JMS implementation.
The design strategy was based on sending messages containing action commands asynchronously to a JMS queue. Based on the implementation, an action command can perform any business task on execution. The main components in the design are:
- Action command
- Action message
- Facade bean
- JMS handler as a message producer
- Message-driven bean as a message consumer
- Message queue
- Temporary queue
The bean diagram visually depicts the participating components, the flow of request messages from the client of the framework to the message-driven bean, and the flow of response messages from the message-driven bean back to the client (see Figure 1).
Figure 1: J2EE environment
Implementation Details
Action Command
The action command represents the actual business logic to be executed. A standard interface was created for the action command so that different implementations of the command can be plugged in based on different requirements. The command interface defines two important methods, shown in the following code snippet:
public interface Action extends Serializable{
public void execute() throws Throwable;
public Object getResult();
}
The action command is designed as a selfcontained entity and is expected to contain the results of business logic execution. The getResult method returns the resultant object. This approach guarantees that the business logic and the results of business logic execution are completely isolated from the framework. The framework itself acts like a black box and only provides the means to execute the business logic, leaving intricate business logic details to the actual implementation of the action command.
Action Message
The action message acts as an adapter to the action command. The purpose of the action message is to shield the message-driven bean from dealing with the error conditions and the status of action command execution. Action message also helps the JMS handler correlate the messages sent to the message queue and the messages received from the temporary queue. The framework client can check for the status of the action command execution by inspecting the corresponding action message. The following code shows the important methods in the ActionMessage class:
public class ActionMessage implements
Serializable {
...............................
public boolean hasThrowable() {
return (throwable !=
null);
}
public void execute() {
try {
theAction.exe
cute();
} catch(Throwable t) {
setThrowable(t);
}
}
}
Facade Bean
A facade was incorporated into the framework to prevent the client from knowing the intricate details of the JMS implementation. This facade is implemented as a stateless session bean. The facade bean is an entry point into the framework - the client of the framework creates a list of action commands and sends the list to the facade bean by invoking the sendAll method. The facade bean internally delegates the handling of action commands to a JMS message handler. It is responsible for looking up the JNDI context for the JMS message queue and connection factory and uses this information to create its own JMS message handler.
The timeout value on the sendAll method is used by the JMS message handler to set the timeout on the messages to be received from the temporary queue.
There are three main advantages to using the facade bean. First, it shields the clients from the intricacies of the JMS implementation. Second, the method invocation on the facade bean can take part in transaction management. Finally, because the EJB container pools the facade bean instances, the JMS resources such as temporary queues, queue sessions, and queue connections used internally by each bean instance will automatically get the pool effect, ensuring economical utilization of the resources. The facade session bean supports both remote and local invocations by defining remote and local interfaces (see Listing 1).
JMS Message Handler
The JMS message handler is the actual component that encapsulates the nitty-gritty details of JMS implementation. The AsyncMessenger class represents a JMS handler. The facade bean delegates the list of action commands to the AsyncMessenger instance by invoking the sendAll method. The AsyncMessenger instance first wraps each action command in an action message object. It then creates a JMS message instance of javax.jms.ObjectMessage type. The AsyncMessenger sets the action message object as a serializable payload in the JMS ObjectMessage instance. It also creates a temporary queue and registers a JMS queue receiver for receiving the response messages from the temporary queue. It includes a reference to the temporary queue in the JMS message. Finally, it sends the JMS message to the message queue asynchronously.
Since the execution time for each action command can vary, the sequence in which the response messages are received by the AsyncMessenger may not exactly match the sequence in which the request messages were sent. In order to guarantee the exact mapping of the response message to the corresponding request message, a unique multiplexing ID is associated with each request message. This ID is nothing but the index of the original action command in the list. The AsyncMessenger sets this ID as an attribute in the request action message, retrieves it from the response action message, and uses it to set the action message in the response list at the same location as the original request message was in the input list.
Message Driven Bean (MDB)
An MDB is used as the message consumer to retrieve and process the messages transported via a message queue. The EJB container is responsible for managing the life cycle of MDBs. At runtime, the EJB container creates many MDB instances and keeps those instances in a pool. After creating an instance in the pool, the EJB container takes care of subscribing the instance to a specified message topic or connecting the instance to the specified message queue, based on the JMS information provided in the MDB deployment descriptor. The main advantage of using MDBs over custom JMS message consumers is that MDBs are inherently JMS message consumers and the EJB container provides a robust environment for MDBs. Because MDB instances in the pool can concurrently consume and process hundreds of messages, MDBs are capable of providing higher throughput and scalability than most traditional JMS message consumers.
In the current implementation, on notification of a JMS message, an MDB retrieves the action message from the JMS Object message and invokes the execute method on the action message. This invocation will trigger the actual execution of the action command contained by the action message. After execution is completed, the MDB creates a new JMS Object message and sets the action message as a payload. It then retrieves the reference to the temporary queue from the input JMS message and sends the newly created JMS message to the temporary queue of the JMS handler. This point-to-point messaging assures guaranteed delivery of the response to the authentic original caller.
Message Queue
Point-to-point (P2P) messaging was chosen for the example implementation because it offers a guarantee of the message being processed once-and-only-once by one-and-only-one message consumer. In the example case, the business logic should be executed once and only once, but in tandem. The main component that makes P2P work is the message queue, which basically decouples the message producer from the message consumer. Several message consumers can connect to the same queue, but the underlying JMS server guarantees that the message in the queue is delivered to one and only one message consumer. In the example implementation, a message queue is configured as the primary destination for all the action messages (the MDB instances being the primary message consumers).
Normally, a message in the queue is delivered to the consumer in the order it was placed in the queue. The BEA WebLogic 6.1 JMS implementation provides the flexibility of sorting the delivery order of the queue messages by configuring destination keys. It enables "LIFO" and "FIFO" ordering of message queues. To enable FIFO (first-in-first-out retrieval of the messages in the queue), the sorting order of the JMS messages is configured by setting an ascending destination key in the BEA WebLogic 6.1 server JMS configuration.
Temporary Queue
A temporary queue is typically used by JMS clients to participate in requestresponse messaging using the JMS point-to- point protocol. A temporary queue belongs uniquely to the creating JMS queue session, and its life spans the entire duration of the queue session's connection unless the temporary queue is deleted. Unless its reference is transferred to other JMS clients using the JMSReplyTo header, the temporary queue is only accessible by the JMS client that created it. Although multiple different JMS clients can send messages to a temporary queue, JMS messages from the temporary queue can only be consumed by the JMS queue session associated with the JMS connection that created the temporary queue.
The BEA WebLogic 6.1 Server enables easy creation of temporary queues. A JMS template has to be configured on a BEA JMS server to enable the creation of temporary queues in that JMS server. A JMS template provides an efficient means of defining multiple destinations with similar attribute settings.
In the current framework, the JMS handler creates a temporary queue in order to receive responses from the MDB that executes the action messages.
Complete Picture
Figures 2 and 3 depict the relationship and message flow between the component classes.
 Figure 2: Class diagram |
 Figure 3: Class diagram |
Deployment Details
Deployment Descriptors
The first step in deployment is to create the deployment descriptors for the facade session bean and MDBs. The XML fragments shown in Listings 2 and 3 ( due to space limitations, Listing 3 and 4 are online at www.sys-con.com/weblogicsourcec.cfm). are a sample deployment description. The next step is to JAR up the EJBs and run the BEA WebLogic ejbc utility. The final JAR file is ready to be deployed using the BEA WebLogic Admin Console.
BEA WebLogic 6.1 Setup
1. Configure the WebLogic JMS connection factory (see Figure 4).
Figure 4: JMS connection factory
a. In the left pane of the Admin Console, click on Connection Factory node and then click on the "Configure a new JMS Connection Factory" link in the right pane.
b. On the General tab, give the connection factory a name and a JNDI name. Fill in other attributes as appropriate, and then click on Create.
c. Fill in the Transactions and Flow Control tabs, as appropriate. Click on the Apply button on each tab when changes are completed.
d. On the Targets tab, target a WebLogic server instance or a server cluster on which to deploy the connection factory by selecting either Servers tab or Clusters tab.
2. Configure the WebLogic JMS template.
a. In the left pane of the Admin Console, click on the Templates node and then click on the "Configure a new JMS Template" link in the right pane.
b. On the General tab, specify a name for the template and click on the Create button.
c. Fill in the Thresholds & Quotas, Override, and Redelivery tabs, as appropriate. Click on the Apply button on each tab when changes are completed.
3. Configure the WebLogic destination key.
a. In the left pane of the Admin Console, click on the Destination Keys node and then click on the "Create a new JMS Destination Key" link in the right pane.
b. On the Configuration tab, specify a name, sorting property, key type, and sorting order direction.
c. After specifying all the attributes, click on the Apply button.
4. Configure the WebLogic JMS Server.
a. In the left pane of the Admin Console, click on the Server node and then click on the "Configure a new JMS Server" link in the right pane.
b. On the General tab, specify the server name, a store if one has been created, a paging store if one has been created, and select the previously created template as a temporary template for the server.
c. Click on the Create button to create the server.
d. Fill in the Thresholds & Quotas tab as appropriate, and then click on the Apply button to finalize the changes.
e. On the Targets tab, target a WebLogic Server instance and apply the changes. 5. Configure the WebLogic queue (see Figure 5).
Figure 5: AsyncJMSQueue
a. Under the Servers node in the left pane of the Admin Console, expand the newly created JMS server instance and click on the Destinations node.
b. Click on the "Configure a new JMSQueue" link in the right pane.
c. On the General tab, specify the queue name and the queue JNDI name.
d. Choose the previously created destination key from the available list of destination keys and move it to the chosen list.
e. Click on the Create button to create the queue.
f. Fill in the Thresholds & Quotas, Override, and Redelivery tabs as appropriate; click on the Apply button on each tab to finalize the changes.
6. Configure the facade session bean and MDBs.
a. Under the Deployments node on the left pane of Admin Console, click on the EJB node.
b. Click on the "Configure a new EJB link" on the right pane.
c. On the General tab in EJB component pane, specify the EJB name, path to the EJB jar file and deployment order.
d. Click on the Deployed checkbox to set the deployment status for the component.
e. Click on the Create button to create the component.
f. On the Targets tab, target a WebLogic Server instance or cluster instance and apply the changes
Usage
Listing 4 shows a sample usage of the framework.
Benchmarking
Performance benchmarking was conducted with and without the framework on an airline e-commerce application that displays passenger itinerary information. The application retrieves data by interacting with many heterogeneous data sources, ranging from relational databases to mainframe legacy systems. Distinct data access objects are used to access data from different data sources and an aggregator service component was put into place to interact with a diverse set of data access objects and to assemble the retrieved data.
Without using the framework, the aggregator service would assemble data by invoking data access objects in a synchronous fashion. In this case, it was anticipated that the overall response time would be very high, and the application performance would be very poor. This expectation was proved accurate by conducting a load test on the application: if there are five itineraries on average per passenger, the average response time to retrieve complete information is about 35 seconds. The load test on the application proved that unless a parallel and concurrent data retrieval mechanism is implemented, the optimization of response times will be exceptionally difficult.
The framework was incorporated into the application such that the aggregator service would simultaneously invoke data access objects asynchronously, and another round of load testing was conducted on the application. This test yielded positive results indicating an average response time of about seven seconds. Thus, application performance increased almost five fold by using the framework.
Conclusion
A reusable, extensible, and robust framework that utilizes the asynchronous features offered by JMS and the robust runtime environment provided by the EJB container can solve the problem of parallel and concurrent business logic processing in enterprise and mission-critical applications. The benchmarking of an airline application running in a BEA WebLogic 6.1 Application Server yielded very positive response times and higher performance when the framework was incorporated into the application for parallel and concurrent data access from multiple data sources. The implementation of the framework proved to be much more robust than the typical thread-based implementation.
Acknowledgments
We would like to thank Virgil Bistriceanu, managing director, and Helen Agulnik, manager, for their support and inspiration in writing this article. We would also like to extend our special thanks to our colleague Kelly Oliver, who contributed her time by reviewing and editing the article.