![]() |
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 ONE Application Server version 7.This sample application document contains the following sections:
Overview
Compiling and Assembling the Sample Application
Deploying the Sample Application
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 3The 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
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:
Compile and Assemble.
Configure resources.
- 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.
Deploy the application.
- When you have re-created the sample application from scratch, proceed to Configuring Resources
Clean the web application project area.
- After configuring the resources, proceed to Deploying the Sample Application.
Undeploy sample application and resources.
- Execute the asant clean command to remove the sample application's assemble and build directories.
- 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 describes how to manually register the application in a Sun ONE 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 ONE 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 .
Go to the src directory of the sample:
Execute the asant deploy command.
- <install_dir>/samples/ejb/mdb/simple/src
Go to the root of the sample directory. For example:
The deployment process involves the following operations:
Execute the asadmin command to deploy the application to the local application server instance:
- <install_dir>/samples/ejb/mdb/simple/
- asadmin deploy -u <user-name> -w <password> -H <hostname> -p <admin server port> --instance <instance name> mdb-simple.ear
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.
Open the pre-existing sample .ear file.
Start the admin server if it is not already started.
- UNIX
Open the .ear file.
- Windows
Navigate to the browser based admin server page [http://<server-host>:<admin-port>/index.html].
Enter your username and password to access the admin server.
Click App Server Instances>server1>Applications>Enterprise Apps.
In the right hand pane, click Deploy to deploy an application.
Choose the file (from disk) to be deployed (mdb-simple.ear).
Enter mdb-simple.
- You are prompted to supply the application name.
- 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 ONE 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:
Execute the asadmin command to look at applications deployed with a server instance. You see mdb-simple as one of the listed applications.
Or navigate the Admin tool GUI to App Server Instances>server1>Applications>Enterprise Apps.
- asadmin list-components -u <username> -w <password> -H <host> -p <admin-port><server instance name>.
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/simple/src
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/
2. In the left pane, click on App Server Instance
3. Click on Server1
4. Click on JMS
5. Click on Connection Factory Resource. Click New in the right pane.
6. Add the following:JNDI-Name: jms/MyQcf7. Click on Destination Resources. Click New in the right pane.
Type: javax.jms.QueueConnectionFactory
Enabled: true
8. Add the following:JNDI-Name: jms/MyQueue9. Add a new property for this destination resource, with name-value pair as imqDestinationName=MyQueue
Type: javax.jms.Queue
Enabled: true
Configuring resource through CLI admin
You can as well configure these resources using CLI admin, as follows:for connection factory:asadmin create-jms-resource jms/MyQcf --resourcetype javax.jms.QueueConnectionFactory -u amin-user -w admin-password -p admin-port -H host-name --instance server1
for destination(queue):
asadmin create-jms-resource jms/MyQueue --resourcetype javax.jms.Queue -u amin-user -w admin-password -p admin-port -H host-name --instance server1 --property imqDestinationName=MyQueue
Running the Sample Application
Running the sample is a two step process.
Go to the directory where the sample has been deployed.
Run appclient using the following instructions.
- > 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 3The 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
After javadocs are generated, you can access them at
- asant javadocs
- or
- asant all
<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