Java Reference
In-Depth Information
Like a table in a relational database, an entity bean may be related to other entity beans. For example,
in a college enrollment application, StudentEJB and CourseEJB are related because students enroll
in classes. With container-managed persistence, the EJB container takes care of the relationships. For
this reason, relationships in entity beans with container-managed persistence are often referred to as
container-managed relationships (CMRs).
Cross-
Reference
You learn more on CMP entity beans in Chapter 22 .
In addition to session beans and entity beans, the EJB 2.0 introduced a third type of EJB: message
driven bean, as discussed next.
Message -Driven Beans
A message-driven bean is a new type of EJB. It acts as a listener for the Java Message Service (JMS)
API and processes messages asynchronously. That means the client does not need to wait the
complete of the tasks it delegated to the message driven bean. Instead, it can continue on other tasks
as soon as it has dropped the message to the JMS. The Message-driven beans were introduced as
recently as late 2001 in EJB Specification 2.0 to fill up the gap in interactions between the J2EE
platform and the Java Message Service (JMS). The messages may be sent by any J2EE component
(such as an application client, another enterprise bean, or a Web component) or by a JMS application or
system that does not use J2EE technology at all.
A message-driven bean is similar to an event listener, except that it receives messages instead of
events. The calling client does not need to wait for the completion of the services it requests. As soon
as the message is dropped to the JMS message queue or the topic, the calling client moves on to other
tasks. Message-driven beans currently process only JMS messages, but in the future they may be used
to process other kinds of messages as well.
A visible difference between message-driven beans and session or entity beans is that clients do not
access message-driven beans through interfaces. In fact, clients do not directly access message-driven
beans at all. A message-driven bean can only be accessed by an EJB container once a JMS message
is received. As a consequence, message-driven beans have no home or remote interfaces. Only the
implementation class needs to be developed. As you can see from the example in Listing 20-8 , there is
actually only one specific method, onMessage , that the bean developer needs to code.
Listing 20-8: MessageEchoEJB source code
package java_database.MessageEchoMDEJB;
import javax.ejb.*;
import javax.jms.*;
/**
* This message driven bean echos the message text it received on the
standard output.
* It can be extended to implement any business rules upon receiving the
message.
* @author: Andrew Yang
* @version: 1.0
*/
public class MessageEchoBean implements MessageDrivenBean, MessageListener {
private MessageDrivenContext context;
Search WWH ::




Custom Search