|
YOUR FEEDBACK
Did you read today's front page stories & breaking news?
SYS-CON.TV |
TOP THREE LINKS YOU MUST CLICK ON Feature Message-Driven Beans in WebSphere 5.0
Message-Driven Beans in WebSphere 5.0
Apr. 24, 2002 12:00 AM
With the release of the Enterprise JavaBeans (EJB) 2.0 specification a new category of enterprise beans was introduced - message-driven beans (MDBs). Message-driven beans have been specifically designed to process incoming JMS messages within an EJB container. Although it's been more than a year since the release of the EJB 2.0 specification, previous versions of IBM WebSphere Application Server didn't provide support for it. The upcoming release of WebSphere Application Server 5.0 not only supports the EJB 2.0 specification but all the other specifications that make up the J2EE 1.3 specification. At the time of writing of this article, only a technology preview of WebSphere 5.0 was publicly available. You can use the preview to check out the new features that will be available in the final release of the product. In this article we'll use it to demonstrate developing and deploying message-driven beans in WebSphere. This is the first of a two-part series in which I will introduce message-driven beans and discuss how to develop, assemble, and deploy them in WebSphere 5.0. In this article, I'll introduce concepts surrounding Java Message Service (JMS) and MDBs and present what you need to know in order to develop your first message bean. Since MDBs are designed to handle incoming JMS messages, having a prior understanding of JMS would help in understanding MDBs better. In this article I will provide a brief introduction to JMS. For a more extensive treatment of this topic, refer to the resources section at the end of this article.
JMS Overview
JMS is a standard, vendor-neutral API that Java programs can use to create, send, receive, and process messages from an enterprise messaging system. Enterprise messaging systems (sometimes referred to as message-oriented middleware) provide the infrastructure necessary to exchange messages asynchronously between distributed software applications. JMS is analogous to JDBC. Just as JDBC is a vendor-neutral API used for accessing a variety of relational databases, JMS is a vendor-neutral API used for accessing enterprise messaging systems. JMS is supported by many commercial messaging products, including IBM MQSeries, and Progress' SonicMQ. If you develop your messaging applications to use the JMS API, they become portable across messaging products that support JMS.
JMS Clients and Providers
A JMS client that sends messages is called a producer, whereas a JMS client that receives messages is called a consumer. Although from the definition it looks like you would need two programs, a single JMS client can behave as both producer and consumer. When a client wants to send or receive a message it needs to specify a JMS destination. In the case of a producer the JMS destination is where the messages are sent, while in the case of a consumer it identifies the source from which messages are received. EJBs of all types can use JMS to send messages to various destinations. Those messages are consumed by other JMS clients, including message-driven beans. An MDB is the only type of EJB that can consume messages; entity and session beans cannot consume messages. This is because entity and session beans respond to synchronous Java RMI calls from EJB clients and cannot be programmed to receive JMS messages. In order to overcome this limitation, message-driven beans were introduced in EJB 2.0.
JMS Is Asynchronous
JMS Messaging Models
In publish-subscribe messaging, one producer can send messages to multiple consumers. The destination to which a producer sends a message is called a topic. Consumers can express interest in receiving messages from a topic by subscribing to it. If one or more consumers subscribe to a topic, messages sent to the topic are delivered to each of the topic's consumers. Pub/sub is a push-based model, where consumers automatically receive messages without them having to poll the topic for new messages. In point-to-point messaging, a producer can send a message to only one consumer. The destination to which a producer sends a message is called a queue. A queue can have multiple consumers, but only one consumer receives the message. P2P can be used as either a pull- or push-based model. Consumers either poll the queue or receive messages automatically when they arrive in the queue.
Message-Driven Beans
A message-driven bean is a new type of EJB component that can receive JMS messages. An MDB is a JMS client that can consume messages sent to either a queue or a topic by another JMS client (see Figure 3). MDBs are stateless transaction-aware server-side components that run within an EJB container. While a message bean is responsible for processing messages, the EJB container is responsible for its environment, including transaction and resource management, concurrency control, security enforcement, and message acknowledgement. Message-driven beans can consume messages concurrently. That means the container can instantiate multiple instances of a message bean to process messages in a JMS destination. An MDB can receive thousands of messages and process all the messages at the same time. The EJB container manages resources, transactions, and security in a multithreaded environment and leaves the job of processing the message to the MDB. On the other hand, traditional JMS clients have to custom-implement all these functions. Message-driven beans, like entity and session beans, are enterprise beans. But message beans don't have component interfaces such as home, remote, or local interfaces. All they have is a bean class and an XML deployment descriptor. Since they aren't accessible using the Java RMI API and they respond only to asynchronous messages, they don't require component interfaces. The messages processed by a message bean can come from any messaging client, such as MQSeries or SonicMQ client. Message-driven beans are stateless; this means they cannot hold any conversational state. In this sense they are similar to stateless session beans. Since the container could instantiate multiple instances of the message bean to process messages, they cannot hold any conversational state. Having laid the groundwork, we're now ready to discuss what's involved in developing message-driven beans.
MDB Semantics
public interface MessageDrivenBean extends javax.ejb.EnterpriseBean { public void ejbRemove() throws EJBException; public void setMessageDrivenContext( MessageDrivenContext ctx)throws EJBException; } The life cycle of a message bean is simple. The container creates an instance of a message bean in three steps. First, the container checks the message bean pool for an available instance. If it cannot find one, it creates a new instance of the message bean and adds it to the pool. Second, the container calls the setMessageDrivenContext method to pass the context object to the instance. Third, the container calls the instance's ejbCreate method. The setMessageDrivenContext method is called by the bean's container to provide the bean with access to information about the container in which it executes. In the ejbCreate method you would code logic to initialize it. Since message beans are stateless and the ejbCreate method doesn't have any arguments, in most cases you wouldn't have any code in this method. The ejbRemove method is invoked when a message-driven bean is removed from the pool. You would code logic to clean up any resources you have acquired in ejbCreate. Here's what the javax.jms.MessageListener interface looks like:
public interface MessageListener { public void onMessage(Message message) } By implementing the MessageListener interface, the bean class is telling the container that it is a message-driven bean. Whenever a message arrives in the destination object associated with the bean, the container invokes the bean instance's onMessage method by passing the message as an argument. The onMessage method contains the business logic that handles the processing of the message.
A Simple Example
Message Bean Class
MDBs have deployment descriptors like entity and session beans. The code snippet below shows only the portion of the deployment descriptor relevant to our simple message-driven bean.
For each message-driven bean you need to have a <message-driven> entry. The <message-driven> entry is declared in the <enterprise-beans> element alongside other entity and session beans. Just like session beans, it defines an <ejb-name>, <ejb-class> and <transaction-type>. It doesn't have any component interfaces. In addition, it also needs to define the <message-driven-destination> entry. The <message-driven-destination> element identifies whether the MDB is listening to a queue or topic. In our example, it is listening to a queue. You notice that nowhere in the deployment descriptor do we specify the name of the actual queue. It is specified when the MDB is deployed in WebSphere.
The JMS Client
In short, the bean client:
Deploying the Message Bean
If you can't wait for the next installment of this series, download the already packaged enterprise application and client JAR and follow the included instructions on how to install and execute the message-driven bean. For location of the download refer to the Resources section.
Summary
Resources
WEBSPHERE LATEST STORIES . . .
SUBSCRIBE TO THE WORLD'S MOST POWERFUL NEWSLETTERS SUBSCRIBE TO OUR RSS FEEDS & GET YOUR SYS-CON NEWS LIVE!
|
SYS-CON FEATURED WHITEPAPERS MOST READ THIS WEEK BREAKING WEBSPHERE NEWS
|
|||||||||||||||||||||||||||||||||||