What Are Message-Driven Beans?

Message-Driven Beans (MDBs) are generally constructed to be message consumers, although they can, like any other EJB, also be used to create and send messages. An MDB lives entirely within the container; it has no security context of its own. When the bean is deployed, it is associated with a particular queue or topic, and is invoked by the container when a message arrives for that queue or topic.

The following are the features of a Message-Driven bean:

  • It is anonymous; that is, it has no client visibility. No state is maintained for the client.

  • All instances of a particular Message-Driven bean are equivalent.

  • The container can pool instances.

  • It does not have a local or remote interface.

  • It is invoked asynchronously by the container.

  • The bean lives entirely within a container; the container manages its lifecycle and environment.

These features are discussed in more detail next.

The Message Producer's View

To the client producing JMS messages, the Message-Driven bean is just an anonymous message consumer. The client need not be aware that the consumer is an MDB. The client simply sends its messages to a destination, either a queue or a topic, and the bean handles the message when it arrives. Therefore, the coding of message producers in an application using MDBs is exactly the same as any JMS application; that is, the message must conform to the JMS specification and the destination must be a Java Naming and Directory Interface (JNDI) registered name. Apart from this, the message does not have to correspond to any particular format.

It is not necessary for the client to be a Java client application or an EJB to take advantage of Message-Driven beans; it can be a Java ServerPages (JSP) component or a non-J2EE application.

Similarities and Differences with Other EJBs

In some respects, an MDB is similar to a stateless Session bean. It is a complete EJB that can encapsulate business logic. An advantage is that the container is responsible for providing functionality for security, concurrency, transactions, and so forth. Like a Session or Entity bean, an MDB has a bean class and XML deployment descriptor.

The main difference from the other EJBs is that Message-Driven Beans cannot be called directly by the client. For this reason, they do not have Home, Remote, or Local interfaces, which makes them less prone to misuse by the client.

Unlike Entity and Session beans, MDBs do not have a passive state. Therefore, they do not implement the ejbActivate() and ejbPassivate() methods.

NOTE

Although a Message-Driven bean is considered to be a stateless object, from the client's view, it can and should retain state in its instance variables. Examples of this are an open database connection and the Home, Local, and Remote interfaces to other EJBs.


..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.147.71.6