Looking Under the Hood of an MDB

Figure 14.1 shows the interactions between the client, the JMS service, the EJB container, and the message-driven bean.

Figure 14.1. Under the hood of a message-driven bean.


The following steps describe the sequence of interactions in detail:

1.
At startup, the JMS service registers all JMS Destinations (Topic or Queue) with the Java Naming and Directory Interface (JNDI) service. The EJB container also registers the message-driven beans as JMS consumers with the JMS service.

2.
The EJB container decides to instantiate a message-driven bean based on the caching policy. For example, the EJB container instantiates the OrderVerifierMDB bean using the Class.newInstance("OrderVerifierMDB.class") and then calls the setMessageDrivenContext() and ejbCreate() methods on the instance. The bean instance is now ready to accept and process a message. The instance enters the pool of method-ready instances.

3.
The client looks up the JMS Destination associated with a message-driven bean by using JNDI. For example, the OrderVerifierTopic Topic associated with the OrderVerifierMDB bean can be located using the following code segment:

Context initialContext = new InitialContext();
Topic topic = (javax.jms.Topic) initialContext.lookup
      ("jms/OrderVerifierTopic");

4.
The client sends a message to the JMS Destination. For example, the client sends a message to the Topic as follows:

TextMessage tm = session.createTextMessage();
tm.setText("1234");
publisher.publish(tm);

When a client sends a message to a Destination for which a message-driven bean is the consumer, the EJB container selects one of its method-ready instances and invokes the onMessage() method on the bean instance passing the client's message.

5.
The EJB container decides to terminate the session bean instance by calling the ejbRemove() method of the bean instance. This happens when the container needs to reduce the number of instances in the method-ready pool.

Note

According to EJB 2.0, message-driven beans support only JMS services, and process standard JMS messages such as TextMessage and ByteMessage. With the advent of Web Services, EJB 2.1 (which was a work in progress at the time this book was written) extends the use of message-driven beans to support Java API for XML Messaging. This will allow message-driven beans to process messages, which conform to Simple Object Access Protocol (SOAP) 1.1. Message-driven beans will also support one-way messages and the blocking of request-response messages.


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

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