![Red Hat Application Server Manual Download Page 127](http://html.mh-extra.com/html/red-hat/application-server/application-server_manual_1427406127.webp)
Chapter 9.
Developing Message-Driven Beans
The EJB 2.1 specification defines a new kind of EJB component for receiving asynchronous messages.
This implements some type of “asynchronous EJB component method invocation” mechanism. The
Message-Driven Bean (also referred to as MDB in the following) is an Enterprise JavaBean, not an
Entity Bean or a Session Bean, that plays the role of a JMS MessageListener.
The EJB 2.1 specification contains detailed information about MDB. The Java Message Service Spec-
ification 1.1 contains detailed information about JMS. This chapter focuses on the use of Message-
Driven Beans within the JOnAS server.
9.1. Description of a Message-Driven Bean
A Message-Driven Bean is an EJB component that can be considered as a JMS
MessageListener, that is, processing JMS messages asynchronously; it implements the
onMessage(javax.jms.Message)
method, defined in the
javax.jms.MessageListener
interface. It is associated with a JMS destination, that is, a Queue for “point-to-point” messaging or a
Topic for “publish/subscribe.” The
onMessage
method is activated on receipt of messages sent by a
client application to the corresponding JMS destination. It is possible to associate a JMS message
selector to filter the messages that the Message-Driven Bean should receive.
JMS messages do not carry any context, thus the
onMessage
method will execute without pre-existing
transactional context. However, a new transaction can be initiated at this moment (refer to Section
9.5
Transactional Aspects
for more details). The
onMessage
method can call other methods on the
MDB itself or on other beans, and can involve other resources by accessing databases or by sending
messages. Such resources are accessed the same way as for other beans (entity or session), that is,
through resource references declared in the deployment descriptor.
The JOnAS container maintains a pool of MDB instances, allowing large volumes of messages to be
processed concurrently. An MDB is similar in some ways to a stateless Session Bean: its instances are
relatively short-lived, it retains no state for a specific client, and several instances may be running at
the same time.
9.2. Developing a Message-Driven Bean
The
MDB
class
must
implement
the
javax.jms.MessageListener
and
the
javax.ejb.MessageDrivenBean
interfaces. In addition to the
onMessage
method, the following
must be implemented:
•
A public constructor with no argument.
•
public void ejbCreate()
: with no arguments, called at the bean-instantiation time. It may
be used to allocate some resources, such as connection factories, for example if the bean sends
messages, or datasources or if the bean accesses databases.
•
public void ejbRemove()
: usually used to free the resources allocated in the ejbCreate
method.
•
public void setMessageDrivenContext(MessageDrivenContext mdc)
: called by the
container after the instance creation, with no transaction context. The JOnAS container provides
the bean with a container context that can be used for transaction management, for example, for
calling
setRollbackOnly(), getRollbackOnly(), getUserTransaction()
.
The following is an example of an MDB class:
Summary of Contents for Application Server
Page 1: ...Red Hat Application Server JOnAS User Guide ...
Page 8: ......
Page 22: ...14 Chapter 1 Java Open Application Server JOnAS a J2EE Platform ...
Page 58: ...50 Chapter 3 JOnAS Configuration ...
Page 66: ...58 Chapter 5 JOnAS Class Loader Hierarchy ...
Page 78: ...70 Chapter 6 JOnAS Command Reference ...
Page 80: ......
Page 86: ...78 Chapter 7 Developing Session Beans ...
Page 136: ...128 Chapter 9 Developing Message Driven Beans ...
Page 142: ...134 Chapter 10 Defining the Deployment Descriptor ...
Page 148: ...140 Chapter 11 Transactional Behavior of EJB Applications ...
Page 158: ...150 Chapter 14 EJB Packaging ...
Page 162: ...154 Chapter 15 Application Deployment and Installation Guide ...
Page 164: ......
Page 176: ...168 Chapter 18 WAR Packaging ...
Page 178: ......
Page 184: ...176 Chapter 20 Defining the Client Deployment Descriptor ...
Page 186: ...178 Chapter 21 Client Packaging ...
Page 188: ......
Page 192: ...184 Chapter 23 EAR Packaging ...
Page 194: ......
Page 200: ...192 Chapter 24 JOnAS Services ...
Page 204: ...196 Chapter 25 JOnAS and the Connector Architecture ...
Page 222: ...214 Chapter 27 Ant EJB Tasks Using EJB JAR ...
Page 234: ...226 Chapter 29 Web Services with JOnAS ...
Page 236: ......
Page 260: ...252 Chapter 34 How to use Axis in JOnAS ...
Page 270: ...262 Chapter 36 Web Service Interoperability between JOnAS and BEA WebLogic ...
Page 296: ......