Java Reference
In-Depth Information
The Message-Driven Bean Class
The code for the
SimpleMessageBean
class illustrates the requirements of a message-
driven bean class:
• It must be annotated with the
@MessageDriven
annotation if it does not use a
deployment descriptor.
• The class must be defined as
public
.
• The class cannot be defined as
abstract
or
final
.
• It must contain a public constructor with no arguments.
• It must not define the
finalize
method.
It is recommended, but not required, that a message-driven bean class implement the mes-
sage listener interface for the message type it supports. A bean that supports the JMS API
implements the
javax.jms.MessageListener
interface.
Unlike session beans and entities, message-driven beans do not have the remote or local
interfaces that define client access. Client components do not locate message-driven beans
and invoke methods on them. Although message-driven beans do not have business meth-
ods, they may contain helper methods that are invoked internally by the
onMessage
method.
For the GlassFish Server, the
@MessageDriven
annotation typically contains a
mappedName
element that specifies the JNDI name of the destination from which the
bean will consume messages. For complex message-driven beans, there can also be an
activationconfig
element containing
@ActivationConfigProperty
annota-
tions used by the bean.
A message-driven bean can also inject a
MessageDrivenContext
resource. Com-
monly you use this resource to call the
setRollbackOnly
method to handle excep-
tions for a bean that uses container-managed transactions.
Therefore, the first few lines of the
SimpleMessageBean
class look like this:
@MessageDriven(mappedName="jms/Queue", activationConfig = {
@ActivationConfigProperty(propertyName = "acknowledgeMode",
propertyValue = "Auto-acknow-
ledge"),
@ActivationConfigProperty(propertyName = "destinationType",
propertyValue = "javax.jms.Queue")
})
public class SimpleMessageBean implements MessageListener {