Message Driven Bean (MDB) Simple Sample Application

  Samples Index




Message Driven Bean (MDB) Simple Sample Application


This document describes how to utilize the Message Driven Bean (MDB) simple sample application in conjunction with the Application Server.

This sample application document contains the following sections:



Overview

The sample demonstrate the use of a simple yet powerful message driven bean. A message-driven bean is an enterprise bean that allows J2EE applications to process messages asynchronously. It acts as a JMS message listener, which is similar to an event listener except that it receives messages instead of events. The messages may be sent by any J2EE component--an application client, another enterprise bean, or a Web component--or by a JMS application or system that does not use J2EE technology.

This is how it looks like when you try to run the client using appclient.

> appclient -client mdb-simpleClient.jar -name SimpleMessageClient -textauth -xml <install_dir>/domains/<domain_name>/config/sun-acc.xml
...
Sending message: This is message 1
Sending message: This is message 2
Sending message: This is message 3

The message bean receives the messages and are printed in the log file (server.log) as follows:
MESSAGE BEAN: Message received: This is message 1
MESSAGE BEAN: Message received: This is message 2
MESSAGE BEAN: Message received: This is message 3

where <install_dir> is the location of where you installed your application server, and <domain_name> is the name of the domain in which this application client is running.

For example, to run client in "samples" domain, enter this command:

  appclient -client mdb-simpleClient.jar
 -name SimpleMessageClient -textauth -xml <install_dir>/domains/samples/config/sun-acc.xml   
    

Refer here for running the sample. To understand sample application source code better, you can generate javadocs using asant javadocs and access them at <install_root>/samples/ejb/mdb/apps/simple/javadocs/index.html.



Compiling and Assembling the Sample Application



This Command Line Based Compilation and Assembly procedure describes how to assemble and compile the sample using a Command Line Interface (CLI).


Command Line Based Compilation and Assembly


To rebuild the entire application from scratch, do the following:


  1. Compile and Assemble.

    Execute the asant command under <install_dir>/samples/ejb/mdb/apps/simple/

    [please add <install_dir>/bin to your path]

    The default target core is executed to rebuild the .ear file.


  2. Configure resources.

    When you have re-created the sample application from scratch, proceed to    Configuring Resources


  3. Deploy the application.

    After configuring the resources, proceed to Deploying the Sample Application.


  4. Clean the web application project area.

    Execute the asant clean command to remove the sample application's assemble and build directories.


  5. Undeploy sample application and resources.

    Execute the asant undeploy-jms-resource command to undeploy resources and asant undeploy command to undeploy the sample application.



Deploying the Sample Application

Select one of the following approaches to deploy the application:


Command Line Based Deployment

Since a complete .ear file is supplied, the fastest means of setting up the application is to use the command line utilities as described in this section.

If you want to deploy through a GUI tool, follow the instructions for GUI Based Deployment.

The pre-built mdb-simple.ear file is an Enterprise Archive file that contains the Java Archive (JAR) files of the application.

Since this application consists of EJB and client, it has two JARs - one for each. Individual JAR files have the XML deployment descriptor files, application class files, and other content as required by the application.

Deploy using either the asant script or the asadmin command .


Using the asant Script


  1. Go to the simple directory of the sample:

    <install_dir>/samples/ejb/mdb/apps/simple


  2. Execute the asant deploy command.


Using the asadmin Command


  1. Go to the root of the sample directory. For example:

    <install_dir>/samples/ejb/mdb/apps/simple/


  2. Execute the asadmin command to deploy the application to the local application server instance:

    asadmin deploy --user <username> --password <password> --host <host> --port <admin-port>   mdb-simple.ear

The deployment process involves the following operations:


GUI Based Deployment

Since a pre-built Enterprise Archive (.ear) file for the sample application is included with the application server, you can use the Admin GUI to quickly read in the .ear file and deploy it to the application server.

  1. Start the admin server if it is not already started. Here are the commands for each platform:
    1. Unix - Execute the <install_dir>/bin/asadmin start-domain domain1 command.
    2. Windows - Click "Start->Programs->Sun Microsystems->J2EE 1.4 SDK->Start Default Server"  command.
  2. Open the .ear file.
    1. Start Admin GUI, by entering URL  http://localhost:<admin.port>/asadmin   in browser. See replacing "localhost" and <admin.port> note here to determine correct values to use for your environment
    2. You are prompted to supply the user name and password of the application server. Enter correct values for your installed application server for "User Name:"  and "Password:" input fields, then click on the "Login" button.
    3. In the left-hand frame, click Applications-->Enterprise Applications  icon in the tree.
    4. In the right-hand frame, click on the "Deploy..."  button.
    5. Choose the file to be deployed by clicking on the "Browse..."  button, and navigate to select component to deploy (mdb-simple.ear).
    6. In the browse dialog, click on the "OK"  button.
    7. In the right-hand frame, click on the "Next"  button.
    8. In the right-hand frame, click on the "OK"  button. (Optionally fill in remaining values for application to be deployed)
    9. In the left hand pane, you will see the newly deployed module's icon in the tree. In the right hand pane you will also see the component listed as deployed.

Verifying Deployment

As an optional step, you can use the Application Server Administration Tool to verify that the application has been registered.

If you do not want to verify that the application has been registered, proceed directly to Running the Sample Application.


To verify the registration of the application:


  1. Execute the asadmin command to look at applications deployed with a server instance. You see mdb-simple as one of the listed applications.

    asadmin list-components --user <username> --password <password


  2. Or navigate the Admin tool GUI to Applications>Enterprise Applications.


  3. Click Enterprise Apps on the left frame menu to display a list of applications deployed on the server.



Configuring Resources

There are several ways to configure resources for this sample.
 

Configuring resources through asant

 build.xml file comes with a predefined set of ant targets for configuring  resources.

Execute  asant   deploy-jms-resource  under  <install_dir>/samples/ejb/mdb/apps/simple

 You can logon to http://<hostname>:<admin-port>/ to see, if the JMS resources are configured or not.
 

Configuring resource through admin GUI

Follow these steps to manually configure resources through admin.
1. Logon to http://<hostname>:<admin-port>/asadmin
2. In the left pane, click on Java Message Service
3. Click on Connection Factories. Click New in the right pane.
4. Add the following:
JNDI-Name: jms/MyMDBQcf
Type: javax.jms.QueueConnectionFactory
Resource: Select Enabled checkbox
5. Click on Destination Resources. Click New in the right pane.
6. Add the following:
JNDI-Name: jms/MyQueue
Type: javax.jms.Queue
Resource: Select Enabled checkbox
7. Add a new property for this destination resource, with name-value pair as imqDestinationName=MyQueue
 

Configuring resource through CLI admin

You can as well configure these resources using CLI admin, as follows:
for connection factory:

asadmin create-jms-resource  --restype javax.jms.QueueConnectionFactory --user <username> --password <password>  jms/MyMDBQcf

for destination(queue):

asadmin create-jms-resource  --restype javax.jms.Queue --user <amin-user> --password <admin-password>  --property imqDestinationName=MyQueue  jms/MyQueue


Running the Sample Application

Running the sample is a two step process.


  1. Go to the directory where the sample has been deployed.


  2. Run appclient using the following instructions.

    > cd <install_dir>/domains/<domain_name>/applications/j2ee-apps/mdb-simple

    > appclient -client mdb-simpleClient.jar -name SimpleMessageClient -textauth -xml <install_dir>/domains/<domain_name>/config/sun-acc.xml

    where <install_dir> is the location of where you installed your application server, and <domain_name> is the name of the domain in which this application client is running.

    For example, to run client in "samples" domain, enter this command:

      appclient -client mdb-simpleClient.jar
     -name SimpleMessageClient -textauth -xml <install_dir>/domains/samples/config/sun-acc.xml   
        

    You see the following output:

    ...


    Sending message: This is message 1
    Sending message: This is message 2
    Sending message: This is message 3

    The message bean receives the messages and are printed in the log file (<install_dir>/domains/<domain_name>/logs/server.log) as follows:
    MESSAGE BEAN: Message received: This is message 1
    MESSAGE BEAN: Message received: This is message 2
    MESSAGE BEAN: Message received: This is message 3





Generating Javadocs

To understand sample application source code better, you can refer to the related javadocs. To generate javadocs, run the command

asant javadocs

or

asant all

After javadocs are generated, you can access them at

<install_dir>/samples/ejb/mdb/apps/simple/javadocs/index.html



Troubleshooting



Although the war, ear, context-root and servlet alias names in this sample are all "mdb-simple", it is not necessary that it be so. The application will work as expected if the names of each of these attributes is unique. Rebuild the app after changing the context-root (in application.xml), the servlet alias (in web.xml) and the war and ear names, redeploy and test the sample to confirm this.

If you encounter problems when deploying the application, make sure you configured resources before deploying the application.

If you encounter problems when running the application, review the log files at <install-dir>/domains/<domain_name>/logs/server.log to see what went wrong.



Copyright © 2004 Sun Microsystems, Inc. All rights reserved.

Last Updated March 6, 2004