|
MDB Features
The Message driven bean provides all the
features that EJB clients are good for. The EJB container takes the
responsibility of concurrency, transactions, security and other services so
that the client can focus solely on the business logic. A few of the features
are discussed below.
- The EJB Advantage
By implementing the MessageDrivenBean
interface the MDB allows the container to take control over the bean and how it
is handled. This results in more robust and reliable message consumers compared
to traditional message clients.
- The flexibility of JMS
The MDB
implements the javax.jms.MessageListener interface to leverage on the
advantages of JMS. By implementing JMS separately MDB's provide room for
providing implementation of other types of messaging systems. Currently only
JMS is supported. Support for other Message types like JAXM message-driven
beans is also possible.
- Timing out MDB's
In addition to the above
interfaces MDB's can implement the javax.ejb.TimedObject interface to add time
based event notifications. This registers the MDB with the EJB Timer service.
When the Timer expires the container calls the ejbTimeout method
- The MessageDrivenContext interface
When the container calls the ejbCreate
method of the MDB to create a bean and puts it into the container it also
creates an instance of the MessageDrivenContext. The MessageDrivenContext
provides methods for transaction management, Timer service, security services
etc. In bean managed transaction based MDB the MessageDrivenContext can be used
to set transaction demarcations. The getUserTransaction method returns
a javax.transaction.UserTransaction
object which can be used to find the status of a transaction or set rollback on
a transaction.
- Concurrency
Typical Message consumers are
single threaded unless they are specifically written to handle multiple
requests. MDB's are by default multithreaded and can handle concurrent
requests. The container creates multiple instances of message beans in the pool
and allows MDB's to concurrently serve requests. However the container does not
guarantee the order in which the request would be served since MDb's are
asynchronous clients. It would be the responsibility of the calling program to
handle the order.
- Transaction management
Like Entity beans, Message driven
beans allow either the bean to manage its own transaction or the container
takes care of transactions. The type of transaction needs to be specified in
the transaction-type attribute of the deployment descriptor. If a bean prefers
to deal with transactions by itself then it implements the javax.transaction.UserTransaction
interface to demarcate transactions.
- Configuration properties
MDB's provide the flexibility to
describe the configuration of the Message driven bean in its deployment
environment. This is helpful for deployers to deploy the bean. The information
could be message acknowledgement modes, message selectors, destinations etc.
- Message Acknowledgement
Message acknowledgement is
automatically handled by the container. If the Message driven bean uses
container managed transaction demarcation then the message acknowledgement is
done automatically as a part of the transaction commit. If bean managed
transaction demarcation is used then the receipt is acknowledged by the
container. However the Bean provider can specify the type of acknowledgement in
the deployment descriptor. The activation-config-property-value attribute is
used to specify the acknowledge mode. The options are JMS AUTO_ACKNOWLEDGE or
DUPS_OK_ACKNOWLEDGE. The JMS AUTO_ACKNOWLEDGE is the default and is assumed if
the entry is not specified.
- Message Selectors
With Message driven beans the bean
provider can restrict the types of messages received by the bean. The bean
provider can specify the message selector to determine the messages that the
MDB can receive.
- Destinations
A Message driven bean always has
an associated destination and it is determined only at deployment time when the
bean is being deployed. However the bean provider provides information on
whether the message needs to be associated with a Queue or Topic in the
deployment descriptor file. The deployer has to associate the actual bean with
the destination.
- Exceptions
Message driven beans are not
allowed to throw runtime exceptions. A RuntimeException thrown from any method
of the message driven bean class results in the bean being thrown into the
'does not exist state'. But as far as the client is concerned it does not know
the message consumer and hence does not stop sending messages even if a
exception occurs. However the container takes care of it and redirects
subsequent requests for the bean to a different instance.
Deployment Descriptor
In line with the EJB specifications for
enterprise beans, Message driven beans have to register themselves in the XML
deployment descriptor. A typical descriptor is shown below.
<enterprise-beans>
session beans or entity beans ...
..
<message-driven>
<ejb-name>message Bean Name</ejb-name>
<ejb-class>com.name.bean.SampleMessageBean</ejb-class>
<transaction-type>Container</transaction-type>
<message-driven-destination>
<destination-type>javax.jms.Queue</destination-type>
</message-driven-destination>
<security-identity>
<run-as><role-name>everyone</role-name></run-as>
</security-identity>
</message-driven>
</enterprise-beans>
The deployment descriptor defines all the
enterprise beans that are defined in a specified ejb-jar file. Message driven
beans are defined in the 'message-driven' tags. This includes the Message
Driven bean name, class. The 'transaction-type' could be container or
bean-managed. The 'message-driven-destination' is either a queue or a Topic,
depending on what the bean is supposed to do. The 'security-identity' defines
the security roles and permissions for the beans.
Conclusion
Sun has tried its best at standardizing
the messaging API and also provide support for messaging in the EJB2.0
specification. However these are just specifications and are left to the
vendors for implementation. Just as technologies, like JSP, servlets, and
EJBs, Message driven beans will take some time to standardize themselves
in the application server market.
New on the Java Boutique:
New Review:
Time Management Made Easy with the Quartz Enterprise Job Scheduler
Why not just use the Java timer API? This open source scheduling
API boasts simplicity, ease-of-integration, a well-rounded feature
set, and it's free!
New Applet:
Reverse Complement
Reverse Complement is a simple applet that converts DNA or RNA
sequences into three useful formats.
Elsewhere on internet.com:
WebDeveloper Java
Lots of Java information on webdeveloper.com
WDVL Java
Thorough Java resource at the Web Developer's Virtual Library.
ScriptSearch Java
Hundreds of free Java code files to download.
jGuru: Your View of the Java Universe
Customizable portal with online training, FAQs, regular news updates, and tutorials.
|