Previous Contents Index Next |
iPlanet Application Server Developer's Guide |
Chapter 7 Using Message Driven Beans
This chapter describes a Message Driven Bean and its properties. This chapter also provides additional guidelines for creating message- driven beans and deploying them to iPlanet Application Server.This chapter contains the following sections:
Components of Message Driven Beans
Message Driven Bean Guidelines
Accessing iPlanet Application Server Functionality
Creating the Deployment Descriptors by Hand
Note The message-driven bean functionality in iPlanet Application Server is meant for developer use only. It has neither been tested or certified for production environments.
Introducing Message Driven Beans
iPlanet Application Server uses the iPlanet Message Queue for Java, 2.0 SP1 as the messaging middleware application that implements the JMS specifications. You must install iMQ for Java, 2.0 SP1 before you can use message-driven beans on iPlanet Application Server.iMQ for Java, 2.0 SP1 is bundled on the iPlanet Application Server installation CD. If you don't have the installation CD, you can download a free, developer edition from http://www.iplanet.com/products/iplanet_message_queue/home_message_queue.html.
How a Message Driven Bean is Accessed
A message-driven bean interfaces with the Java Message Service (JMS). In other words, a Message Driven Bean is a JMS Listener. JMS middleware and the message-driven bean's container jointly control the delivery of messages to JMS MessageListener objects.
The following steps describe the actions that follow a client request:
When the application server boots up, it will load up all deployed message-driven beans and start up the message listeners.
The following figure shows the request flow path for a deployed bean:
A client, such as a browser, or a servlet, or a stand-alone application sends a message to the JMS destination.
- The application server registers it's destination specific representative with JMS, using iPlanet Application Server's ServerSessionPool.
The JMS calls the application server's callback for the specified destination to serve the request.
The JMS Session's MessageListener will be the container for an instance of a Message Driven Bean.
iPlanet Application Server takes care of the supported services, such as transactions, security, etc., and delegates the servicing of the request to the bean's business method.
- When a message arrives, the container calls the message-driven bean's onMessage method to process the message. The onMessage method normally casts the message to one of the five JMS message types and handles it in accordance with the application's business logic. The onMessage method may call helper methods, or it may invoke a session or entity bean to process the information in the message or to store it in a database.
- A message may be delivered to a message-driven bean within a transaction context, so that all operations within the onMessage method are part of a single transaction. If message processing is rolled back, the message will be resent.
The JMS removes the message from the queue and sends an acknowledgement back to the client if there was a reply-to property in the message.
In case of failure, the JMS tries to send the message again.
Figure 7-1   
Request flow path
The ConnectionConsumer and Session are part of the JMS Provider. The ServerSessionPool, ServerSession and the Message Listener are part of iPlanet Application Server.
Components of Message Driven Beans
When creating a message-driven beans, you must provide the following class files:
Enterprise bean class definition.
Enterprise bean metadata (Deployment Descriptors (DDs) and other configuration information).
Creating the Class Definition
For a message-driven bean, the bean class must be defined as public and cannot be abstract. The bean class must implement the javax.ejb.MessageDrivenBean interface. For example:import javax.jms.*;
import javax.ejb.*;
public class MySessionBean implements MessageDrivenBean, MessageListener {
// message-driven bean implementation. These methods must always included.
public void ejbRemove() throws RemoteException{
}
public void setMessageDrivenContext(MessageDrivenContext ctx) throws RemoteException {
}
// other code omitted here....
}The message-driven bean must also implement one or more ejbCreate(...) methods. There must be one method for each way a client invokes the bean. For example:
Each ejbCreate (...) method must be declared as public, return void, and be named ejbCreate. The throws clause may include java.rmi.RemoteException or java.ejb.CreateException
Message Driven Bean Guidelines
Before deciding which parts of an application you can represent as message-driven beans, you should know a few more aspects about message-driven beans. A couple of these are related to the EJB specification for message-driven beans, and a couple are specific to iPlanet Application Server and its support for message-driven beans.
Accessing iPlanet Application Server Functionality
This section contains the following topics:You can develop message-driven beans that adhere strictly to the EJB specification, you can develop message-driven beans that take advantage of both the specification and additional, value-added iPlanet Application Server features, or you can develop message-driven beans that adhere to the specification in non-iPlanet Application Server environments, but that take advantage of iPlanet Application Server features if they are available. Make the choice that is best for your intended deployment scenario.
The iPlanet Application Server offers several features through the iPlanet Application Server container, and the iPlanet Application Server APIs enable applications to take programmatic advantage of specific iPlanet Application Server environment features. Embed API calls in message-driven beans if you plan on using those beans only in an iPlanet Application Server environment.
For example, you can trigger a named application event from an EJB using the IAppEventMgr interface by using the following steps and example:
First obtain a com.kivasoft.IContext instance by casting javax.ejb.SessionContext or javax.ejb.EntityContext to IServerContext.
Next, use the GetAppEventMgr() method in the GXContext class to create an IAppEventMgr object.
Finally, trigger the application event with triggerEvent().
javax.ejb.SessionContext m_ctx;
....
com.netscape.server.IServerContext sc;
sc = (com.netscape.server.IServerContext) m_ctx;
com.kivasoft.IContext kivaContext = sc.getContext();
IAppEventMgr mgr = com.kivasoft.dlm.GXContext.GetAppEventMgr(ic);
mgr.triggerEvent("eventName");
Managing Transactions
Many message-driven beans interact with databases. You control bean transactions by using settings in the bean's property file. This permits specifying transaction attributes at bean deployment time. By having a bean handle transaction management there is no need to explicitly start, rollback, or commit transactions in the bean's database access methods.By moving transaction management to the container level, you gain the ability to place all the bean's activitieseven those not directly tied to the database accessunder the same transaction control as your database calls. This guarantees that all application parts controlled by a message-driven bean run as part of the same transaction, and either everything the bean undertakes is committed, or it is rolled back in a failure case. In effect, a container managed transactional state permits synchronizing the application without programming any synchronization routines.
Committing a Transaction
When a commit occurs, it signals the container that the message-driven bean has completed its useful work and tells the container to synchronize its state with the underlying datasource. The container permits the transaction to complete and then frees the bean.Note that transactions from the container are implicitly committed. Also, any participant can rollback a transaction. For details about transactions, see Chapter 8 "Handling Transactions with EJBs."
Accessing Databases
Many message-driven beans access and update data. Because message-driven beans are transient, be careful about how accesses occur. In general, use the JDBC API to make calls, and always use the transaction and security management methods described in Chapter 8 " Handling Transactions with EJBs" to manage the transaction isolation level and transaction requirements at the bean level.For details about database access, see Chapter 9 "Using JDBC for Database Access."
Using the Deployment Tool
A simpler way to create the standard ejb-jar deployment descriptors for a message-driven bean is by using the iPlanet Application Server Deployment Tool.Deploying a message-driven bean is similar to deploying other applications using the Deploy Tool.
Start by either opening an existing EJB Module or creating a new one. Once the EJB's class files have been added to the EJB Module, you can right-click on the bean to edit its descriptor, as shown in the following figure:
Figure 7-2   
Selecting a message-driven bean in Deployment Tool
You have to provide the following information in the Deploy Tool's deployment descriptor dialog box:
J2EE Specific Deployment Descriptor Fields
Bean Name
Bean Type (Message Driven Bean)
Transaction Management Type ( Container managed or Bean Managed )
Message Driven Bean Specific Parameters
Destination Type (Queue or Topic)
Durable Name (for Topic subscription only -- optional)
Topic Subscription Durable (optional)
Acknowledgement Mode ( Auto-acknowledge, or Duplicates-Auto-Acknowledge)
Security Identity Type (Only Run-as-specified-user allowed)
Transaction Manager Type (Local or Global -- if not selected, the Transaction Manager Type for this module will be used)
Creating the Deployment Descriptors by Hand
Sample Deployment Descriptor File
<ias-mdbs><!-- This is an extract from the EJB 2.0 DTD based deployment descriptor --<&/tt>
<ejb-class>mycompany.mypackage.MyMDB1</ejb-class>
<transaction-type>Container</transaction-type>
<jms-destination-type>javax.jms.Topic</jms-destination-type>
<!-- This contains all the ias specific deployment information --<&/tt>
<max-pool-size>100</max-pool-size>
<min-pool-size>10</min-pool-size>
For more information, refer to the XML DTD (IASEjb_jar_1_1.dtd) for an EJB JAR file in the iASInstallDir/ias/dtd.
Previous Contents Index Next
Copyright © 2002 Sun Microsystems, Inc. All rights reserved.
Last Updated March 06, 2002