Skip Headers

Oracle9iAS Containers for J2EE Enterprise JavaBeans Developer's Guide and Reference
Release 2 (9.0.2)

Part Number A95881-01
Go To Documentation Library
Go To Product List
Solution Area
Go To Table Of Contents
Go To Index

Go to previous page Go to next page

Message-Driven Beans

A Message-Driven Bean (MDB) is a JMS message listener that can reliably consume messages from a queue or a durable subscription. The advantage of using an MDB instead of a JMS message listener is because you can use the asynchronous nature of a JMS listener with the advantages that the EJB container does the following for you:

An MDB is an easy method for creating a JMS message listener.

This chapter discusses the tasks involved in creating an MDB in OC4J and demonstrates simple MDB development with a basic configuration and deployment. Download the MessageLogger MDB example (messagelogger.ear) from the OC4J sample code page on the OTN site.

Creating Message Driven Beans

To create an MDB, you perform the following steps:

  1. Implement the bean. This includes the following:

    1. The bean class must implement the javax.ejb.MessageDrivenBean and javax.jms.MessageListener interfaces, which includes the following:

      • the onMessage method in the MessageListener interface

      • the setMessageDrivenContext method in the MessageDrivenBean interface

    2. Container callback methods that normally match methods in the EJB home interface. A remote and home interface are not implemented with an MDB. However, some of the callback methods required for these interfaces are implemented in the bean implementation. These include the following:

      • an ejbCreate method

      • an ejbRemove method

  2. Create the MDB deployment descriptors.

  3. Configure the JMS Destination type (queue or topic) in the OC4J JMS XML file--jms.xml.

  4. Map the JMS Destination type to the MDB in the OC4J-specific deployment descriptor--orion-ejb-jar.xml.

  5. If you involve a database in your MDB application, configure the DataSource that represents your database in data-sources.xml.

  6. Create an EJB JAR file containing the bean and the deployment descriptor. Once created, configure the application.xml file, create an EAR file, and install the EJB in OC4J.

The following sections demonstrates a simple MDB.

Bean Class Implementation

Most MDBs receive messages from a queue or a topic, then invoke an entity bean to process the request contained within the message.

The following example is a MessageLogger MDB prints the message it receives, and invokes an entity bean, LogMessage, to process the message.

As an MDB, it is responsible for the following:

Configuring XML Files

You need to create both of the MDB deployment descriptors and the JMS Destination configuration.

MDB Deployment Descriptor

Within the EJB deployment descriptor, define the MDB name, class, JNDI reference, and JMS Destination type (queue or topic) in the <message-driven> element. If a queue is specified, you must also define whether it is durable or not.

The following is the deployment descriptor for the entire EJB application. It includes the deployment information for both the MessageLogger MDB and the LogMessage entity bean. The MessageLogger MDB invokes the LogMessage entity bean.

The MessageLogger MDB is defined in the <message-driven> element, as follows:


<?xml version="1.0"?>
<!DOCTYPE ejb-jar PUBLIC "-//Sun Microsystems, Inc.//DTD Enterprise JavaBeans 
1.1//EN" "">


OC4J-Specific Deployment Descriptor

Once you have configured the MDB and the JMS Destination type, you must inform the container which JMS Destination to associate with the MDB. You could have several topics and queues defined in your jms.xml file. For information on defining these JMS Destination types in jms.xml, see the JMS chapter in Oracle9iAS Containers for J2EE Services Guide.

In order to identify the Destination that is to be associated with the MDB, you map the Destination location and connection factory to the MDB through the <message-driven-deployment> element in the orion-ejb-jar.xml file.

The following is the orion-ejb-jar.xml deployment descriptor for the MessageLogger example. It maps a JMS Topic to the MessageLogger MDB, providing the following:

Once all of these are specified in the <message-driven-deployment> element, the container knows how to map the MDB to the correct JMS Destination.

    <ejb-ref-mapping name="logMessages" />

JMS XML Configuration

Configure the topic or queue that the client sends all messages to that are destined for the MDB. The name, location, and connection factory for either Destination type must be specified.

The following JMS configuration specifies a topic (theTopic) that is used by the MessageLogger example:

<?xml version="1.0"?>
<!DOCTYPE jms-server PUBLIC "Orion JMS server" "

<jms-server port="9127">
        <topic name="jms/theTopic" location="jms/theTopic">
                <description>Employee topic</description>

        <topic-connection-factory location="jms/theTopicConnectionFactory" 
username="admin" password="welcome" /> <!-- path to the log-file where JMS-events/errors are stored --> <log> <file path="../log/jms.log" /> </log> </jms-server>

DataSource XML Configuration

If you use a database in your application, you should configure the DataSource for that database. Full details for DataSource configuration is provided in the Oracle9iAS Containers for J2EE Services Guide.

The database that the MessageLogger example uses is configured in the data-sources.xml file, as follows:


Deploying the Entity Bean

Archive your EJB into a JAR file. You deploy the MDB the same way as the session bean, which is detailed in "Prepare the EJB Application for Assembly" and "Deploy the Enterprise Application to OC4J".

Client Accessing MDB

The client sends a message to the MDB through a JMS Destination. The MDB is associated with the JMS Destination by the container. The following is a JNDI lookup of the JMS Destination for the MessageLogger MDB.

Context ic - new InitialContext();
Queue msgQueue = (javax.jms.Queue) ic.lookup("java:comp/env/jms/msgQueue");

For example, the following JSP sends a message to the MessageLogger MDB Topic:

<%@ page import="javax.jms.*, javax.naming.*, java.util.*" %>
   TopicConnectionFactory connectionFactory = 
       (TopicConnectionFactory)new InitialContext().lookup
   TopicConnection connection = connectionFactory.createTopicConnection();
   TopicSession topicSession = 
        connection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
   Topic topic = 
        (Topic)new InitialContext().lookup("java:comp/env/jms/theTopic");
   TopicPublisher publisher = topicSession.createPublisher(topic);
   Message message = topicSession.createMessage();
   message.setLongProperty("time", System.currentTimeMillis());
   message.setStringProperty("subject", request.getParameter("subject"));
   message.setStringProperty("message", request.getParameter("message"));
Message sent!

Go to previous page Go to next page
Copyright © 2002 Oracle Corporation.

All Rights Reserved.
Go To Documentation Library
Go To Product List
Solution Area
Go To Table Of Contents
Go To Index