Sun Java System logo     
Message Driven Bean (MDB) Simple Sample Application



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 Sun Java (tm) System Application Server 7.

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
...
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

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/simple/javadocs/index.html.



Compiling and Assembling the Sample Application



Before compiling and Assembling the application, refer to Installing MQ Series to install the product.
  • Edit <install_root>/samples/ejb/mdb/simple/src/sun-application-client.xml and remove guest from userid and password.
  • 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/simple/src/

    [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 to configure resources with MQ series

  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 the sample application.

    Execute the asant undeploy command to undeploy sample application. Make sure you delete the resources through GUI admin before undeploying the application.



Deploying the Sample Application

Select one of the following approaches to deploy the application:

  • Command Line Based Deployment describes how to manually register the application in a Sun Java (tm) System Application Server using a Command Line Interface (CLI). This is the fastest means of deploying the application.

  • GUI Based Deployment describes how to use the Sun Java (tm) System Deployment Tool to import and deploy the sample 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 src directory of the sample. For example:

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

  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/simple/

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

    asadmin deploy -u <user-name> -w <password> -H <hostname> -p <admin server port> --instance <instance name> mdb-simple.ear

The deployment process involves the following operations:

  • It authenticates against the local application server's administrative server.

  • The .ear file is transferred to the administrative server.

  • The administrative server begins the registration process:

    • Parses the .ear file.

    • Explodes the bits into the repository.

    • Registers the J2EE application mdb-simple by updating the server.xml file.

      If you want to verify the registration of the application, proceed to the Verifying Registration section.

      Otherwise, you are ready to exercise the application by Running the Sample Application.


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 Tool to quickly read in the .ear file and deploy it to the application server.

  1. Open the pre-existing sample .ear file.

  2. Start the admin server if it is not already started.

    UNIX

    1. Go to <install_dir>/domains/domain1/admin-server/.

    2. Execute the startserv command.

    Windows

    1. Go to <install_dir>\domains\domain1\admin-server\

    2. Run startserv.

  3. Open the .ear file.

    1. Navigate to the browser based admin server page [http://<server-host>:<admin-port>/index.html].

    2. Enter your username and password to access the admin server.

    3. Click App Server Instances>server1>Applications>Enterprise Apps.

    4. In the right hand pane, click Deploy to deploy an application.

    5. Choose the file (from disk) to be deployed (mdb-simple.ear).

    6. Click OK.

      You are prompted to supply the application name.

    7. Enter mdb-simple.

    8. Click OK.

      If the deployment goes through properly, you see the original pane with the mdb-simple application as deployed.


Verifying Registration

As an optional step, you can use the Sun Java (tm) System 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 -u <username> -w <password> -H <host> -p <admin-port><server instance name>.

  2. Or navigate the Admin tool GUI to App Server Instances>server1>Applications>Enterprise Apps.

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



Running the Sample Application

Running the sample is a three step process.

  1.   Modify classpath and LD_LIBRARY_PATH in  appclient  to include  MQ Series jar files.

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

  3. Run appclient using the following instructions.

    >cd <install_dir>/bin
    > change  JVM_CLASSPATH  in appclient   to include

       /opt/mqm/java/lib/com.ibm.mq.jar
       /opt/mqm/java/lib/com.ibm.mqjms.jar
       /opt/mqm/java/lib
    > change LD_LIBRARY_PATH to include /opt/mqm/java/lib

    > cd < install_dir>/domains/domain1/server1/applications/j2ee-apps/mdb-simple_1

    > appclient -client mdb-simpleClient.jar -name SimpleMessageClient -textauth

    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 (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/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/domain1/<server-instance>/logs/server.log to see what went wrong.



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

Last Updated August 6, 2002