Before looking at Message Driven Beans (MDBs) a brief overview of the Java Messaging Service (JMS) API is in order. JMS is a way for applications to send and receive messages.
There are three distinct components in JMS:
- A destination - is where messages are sent.
- A publisher - sends messages to a destination.
- A subscriber - tells a destination that it is interested in receiving messages that are sent to the destination. In other words a subscriber recieves messages that are sent to a destination.
For more details on the JMS API see the JCP Specification at the following URL:
MDBs are different from other Enterprise JavaBeans because clients/users of MDBs, typically do not interact directly with MDBs. Instead they send messages to a JMS Destination and, if the MDB is a registered listener of this destination, the MDB recieves the message and acts accordingly.
This article is organized in the following sections:
- MDB implementation analysis
- Sample application
- The JBoss environment
- The Geronimo environment
- Step-by-step migration
MDB implementation analysis
EJB implementations may vary from one vendor to another. The purpose of this section is to provide an EJB specific feature-to-feature comparison between JBoss v4 and Apache Geronimo so you can clearly identify the differences and plan accordingly before migration.
Given that MDBs are dependent on parts of the Java Messaging Service API (JMS), there will be an overlap of some JMS features when comparing the platform specific MDB features.
JBoss AS 4.0 comes with its own EJB Container implementation.
Geronimo uses OpenEJB as its EJB Container.
JBoss AS 4.0 is packaged with JBoss MQ.
Geronimo uses ActiveMQ as its JMS implementation.
The MDB Sample Application creates an Entity Bean from the information stored in the messages that it recieves. There is also a sample "client" that sends messages to a destination. This sample application implements javax.ejb.MessageDrivenBean and javax.jms.MessageListener. All of the work is done in the onMessage method, which is called by the container when a message is received.
The following figure illustrates the application flow:
The user accesses the command line client and is prompted for the appropriate information. The information is then packaged into a JMS message and sent to a JMS destination. The Message Driven Bean then recieves messages that are sent to the JMS Destination that it is configured to listen to and creates an entity bean based on the information from the message. The entity bean then adds it's information to the data souce.
The MDB application consists of the following packages:
- Contains the main class that is called from the console.
- Pprompts for user input to populate the message.
- Implements javax.ejb.MessageDrivenBean and javax.jms.MessageListener
- Has empty implementations of the methods of javax.ejb.MessageDrivenBean
- All the work is done in this bean's onMessage.
- onMessage is called by the container when a message arrives at the destination.
The MDB sample application also provides a Web Application client messaging-ejb.war. This application client allows you to create (create.jsp) and list (list.jsp) customers.
The tools used for developing and building all the applications are:
Eclipse with JBoss IDE
The Eclipse with JBoss IDE plug-ins was used for development of Java-source code of the sample applications. Eclipse is a very powerful and popular open source development tool. Eclipse can be downloaded from the following URL:
Maven is a software project management and comprehension tool. Based on the concept of a project object model (POM). Maven can manage a project's build, reporting and documentation from a central piece of information.
For this migration example Maven 1.0.2 was used. Maven can be downloaded from the followinf URL:
The sample database for the MDB sample application has only one table. This is an in-memory table. The MEMORY storage engine creates tables with contents that are stored in just in memory. These were formerly known as HEAP tables.
The following table describes the fields of the CUSTOMER table.
The JBoss environment
This section shows you how and where the sample JBoss reference environment was installed so you can map this scenario to your own implementation. Note that for this migration example JBoss v4.0.2 was used.
Detailed instructions for installing, configuring, and managing JBoss are provided in the product documentation. Check the product Web site for the most updated documents.
The following list highlights the general tasks you will need to complete to install and configure the initial environment as the starting point for deploying the sample application.
- Download and install JBoss v4 as explained in the product documentation guides. From now on the installation directory will be referred as <jboss_home>
- Create a copy of the default JBoss v4 application server. Copy recursively <jboss_home>\server\default to <jboss_home>\server\<your_server_name>
- Start the new server by running the
run.sh -c <your_server_name>command from the <jboss_home>\bin directory.
- Once the server is started, you can verify that it is running by opening a Web browser and pointing it to this URL: http://localhost:8080. You should see the JBoss Welcome window and be able to access the JBoss console.
- Once the application server is up and running, the next step is to install and configure all the remaining prerequisite software required by the sample application. This step is described in the following section.
Install and configure prerequisite software
In order to build and run the Loan BMP application included in this article, you need to install and configure the build tool and the database that is used by the application.
Modify database settings
This application is using the HSQL database that comes as part of the JBoss bundle. You need to modify the script for creating the database. Edit the localDB.script file located in the following directory:
Add at the top of the localDB.script file the content of the following example in order to create the sample HSQL database.
CREATE MEMORY TABLE CUSTOMER(ID INTEGER NOT NULL PRIMARY KEY,NAME VARCHAR(45),BIRTHDATE DATE,SSS_NO VARCHAR(25),ADDRESS VARCHAR(60),ANNUAL_SALARY DOUBLE,LOAN_AMOUNT DOUBLE)
As mentioned before, Apache Maven is used to build the binaries for the Loan BMP application. If you do not have Maven installed this is a good time for doing it.
Apache Maven can be downloaded from the following URL:
Build the sample application
In order to build the MDB application a Maven script has been provided. Download the MDB sample application from the following link:
After extracting the zip file, a mdb directory will be created. From now on, this directory will be referred as <mdb_home>. In that directory open the project.properties file. Edit the maven.jboss.home property to match your environment.
From a command prompt or shell go to the <bmp_home> directory and run the following command:
This will build the jar file and put it in the <bmp_home>/target folder. The jar created by the Maven build contains a JBoss specific deployment descriptor, the jboss.xml file in located the META-INF directory of the JAR is shown in the following example:
The resource-ref element is used to map the resource referred to by the name jdbc/ibm-demo in the ejb-jar.xml file to the resource with the JNDI name java:/DefaultDS and also the CustomerHomeRemote name in ejb-jar.xml to the JNDI name CustomerHomeRemote (The Customer EJB).
Deploy the sample application
To deploy the Loan BMP application in JBoss, copy the messaging-ejb-SNAPSHOT.jar and messaging-ejb.war files from the <mdb_home>/target directory to the following directory:
If JBoss is already started, it will automatically deploy and start the applications; otherwise, the applications will be deployed and started at the next startup.
Test the sample application
To test this sample application you can use the command line client or a Web Application client also provided with the sample application download.
Test the application with the command line client
To test the sample client application type the following command from the <mdb_home>/target/classes directory:
java -cp ".;<jboss_home>\client\jbossall-client.jar" com.ibm.demo.mdb.client.MessageSenderJBoss
The user will then be prompted for information to populate the message as in the following example:
Enter the appropriate information the press enter. When you have filled up all of the fields the message will be sent.
Look in the JBoss console for the INFO: SUCCESS!!! message, this is the indication that the message was sent successfully.
Test the application with the Web Application client
To test the Web application client open a Web browser and access the following URL:
If you tested via the command line before you should be able to see the entries you just entered.
From this Web Application client you can also enter new customers. Click on Add Customer to call the create.jsp, the following page will prompt you to enter the new customer info.
Enter the new customer information then click Create. This will take you back to the first page and show you the updated list of customers.
The Geronimo environment
Download and install Geronimo from the following URL:
The release notes available there provide clear instructions on system requirements and how to install and start Geronimo. Throughout the rest of this article we will refer to the Geronimo installation directory as <geronimo_home>.
For this scenario the MDB application will use directly the SystemDatabase from Geronimo. In this case there is no need to set up a new connector for the SystemDatabase since it is already configured as the DefaultDatasource.
Start the Geronimo server
Ensure that Geronimo is up and running. If the server has not been started yet, do so by typing the following command:
Once the server is started you should see a screen similar as the one illustrated in the following example:
Configure database via Geronimo Console
Access the Geronimo Console by pointing your Web browser to the following URL:
Enter system as the username and manager as the password, click Login.
Once logged in, on the bottom left corner from the left navigation panel click on DB Manager. In the text area labeled SQL Command/s enter the following SQL statement and click Run SQL; this will create the table used by the Entity Bean.
CREATE TABLE CUSTOMER(ID INTEGER NOT NULL PRIMARY KEY,NAME VARCHAR(45),BIRTHDATE DATE,SSS_NO VARCHAR(25),ADDRESS VARCHAR(60),ANNUAL_SALARY DOUBLE,LOAN_AMOUNT DOUBLE)
In this migration example application, the same jar file you deployed in JBoss can also be deployed in Geronimo. This is because the build step packaged both the JBoss jboss.xml and Geronimo openejb-jar.xml specific deployment plans in the jar file.
You can see both of these files in the META-INF folder of the jar file or in the <mdb_home>/dd/META-INF directory. The openejb-jar.xml should look like the following example:
As shown in the example, the parent for this configuration is the org/apache/geronimo/SystemJMS connector. Then follows the definition of the EJBs.
The message-driver element defines the message driven bean. The child element resource-adapter/target-name specifies the Gbean name of the resource adapter that will be used to connect to JMS. The activation-config element and its children define the Queue that the MDB will listen to. Here it is used the SendReceiveQueue that is deployed in Geronimo by default. The ejb-ref element and its children define the EJBs that the MDB will access.
The entity element defines an entity bean. The resource ref element defines the datasource that this EJB will be using.
The Web Application client can also be direclty deployed in Geronimo. This is because the build step packages both the JBoss jboss-web.xml and Geronimo geronimo-web.xml specific deployment plans in the war file. You can see both of these files in the <mdb_home>\src\webapp\WEB-INF directory.
The geronimo-web.xml deployment plan should look like the following example.
Deploy the migrated sample application
Ensure that Geronimo server is up and running. From a command line change directory to <geronimo_home>/bin and type the following command:
java -jar deployer.jar --user system --password manager deploy <mdb_home>/target/messaging-ejb-SNAPSHOT.jar
To deploy the sample Web Application client type the following command:
java -jar deployer.jar --user system --password manager deploy <mdb_home>/target/messaging-ejb.war
Once the application is deployed, you can test it by running the application client from a command line or using the sample Web Application client just like you tested the application in JBoss.
Test the application from the command line client
From a command line change directory to the <mdb_home>/target/classes directory and type the following command:
java -cp ".;<geronimo_home>/repository/activemq/jars/activemq-core-3.2-M1.jar;<geronimo_home>/repository/geronimo-spec/jars/geronimo-spec-j2ee-1.4-rc4.jar;<geronimo_home>/lib/commons-logging-1.0.4.jar;<geronimo_home>/repository/concurrent/jars/concurrent-1.3.4.jar" com.ibm.demo.mdb.client.MessageSenderAMQ
You will then be prompted for information to populate the message as in the following example. Enter the appropriate information the press Enter. When you have filled up all of the fields the message will be sent.
To verify that the data has been added to the database, from the DB Manager page on the Geronimo Consloe click on the Application link under View Tables section for the SystemDatabase . On the following page, click on View Contents for the Customer table. You should see something like the following figure.
Additionally, in the command line window/shell where you started Geronimo you should be able to see the confirmation that the message was sent.
Test the application with the Web Application client
Open a Web browser and poit it to the following URL:
Repeat the same steps you did when testing on JBoss.
The sample application used for this migration exercise, while simple, provides you with detailed instructions on how to migrate an application that uses MDB from JBoss V4.0.2 to Apache Geronimo. The modifications to make this happen were restricted to the xml configuration files with no java code changes necessary.