Implementing the Enterprise Bean Class

Table 14.1 shows a summary of message-driven bean methods.

Table 14.1. Summary of Message-Driven Bean Methods
MethodPurposeWhat You Need to Do
setMessageDrivenContext (MessageDrivenContext)The EJB container calls this method to associate the bean instance with its context maintained by the container.You must store the reference to the message-driven context in an instance variable if you need to access the run-time context, such as accessing or changing the transaction context.
ejbCreate()The EJB container calls this method so that you can initialize your bean instance.You initialize the bean here. You also allocate any resources that are to be held for the instance's lifetime. Each message-driven bean must have only one ejbCreate() method with no arguments.
onMessage(Message)This method is called by the EJB container when a message has arrived for the bean to service.You must code the business logic that handles the processing of the message.
ejbRemove()The EJB container calls this method before it ends the life cycle of the instance.You must release any resources that the instance is holding.

Listing 14.1 shows the OrderVerifierMDB bean class. The OrderVerifierMDB message-driven bean implements the javax.ejb.MessageDrivenBean and javax.jms.MessageListener interfaces. It implements the methods setMessageDrivenContext(), ejbRemove(), and onMessage(). In the onMessage() method, the bean retrieves the order ID from the text message. It uses the helper method verifyOrder() to process the order. The verifyOrder() method locates the order by using its primary key from the database, and sets the order's status to Verified.

Listing 14.1. The Full Text of day14/OrderVerifierMDB.java
package day14;

import javax.jms.*;
import javax.ejb.*;
import javax.naming.*;
import java.rmi.*;
import javax.rmi.*;

import day11.OrderHome;
import day11.Order;

public class OrderVerifierMDB implements
MessageDrivenBean, MessageListener {
   private MessageDrivenContext ctx;
   public void setMessageDrivenContext(MessageDrivenContext ctx) {
      print("setMessageDrivenContext called.
");
      this.ctx = ctx;
   }
   public void ejbCreate() {
      print("ejbCreate() called.
");
   }
   public void ejbRemove() {
      print("ejbRemove() called.
");
   }
   public void onMessage(Message msg) {
      print("OrderVerifierMDB: onMessage() called.
");
      if(!(msg instanceof TextMessage)){
         throw new EJBException
            ("OrderVerifierMDB handles only Text Messages.");
      }
      try {
         TextMessage tm = (TextMessage) msg;
         String orderId = tm.getText();
         verifyOrder(orderId);
      } catch(Exception ex) {
         ex.printStackTrace();
         throw new EJBException(ex);
      }
   }
   private void verifyOrder(String orderId) throws FinderException,
      NamingException, RemoteException {
      print("Verifying order Id:" + orderId);
      Context context = new InitialContext();
      OrderHome orderHome = (OrderHome) PortableRemoteObject.narrow
         (context.lookup("day11/Order"),OrderHome.class);
      Order order = orderHome.findByPrimaryKey(orderId);

      /* Now we can verify things such as the class capacity,
         students billing information etc.,
         Here we merely set the order status to "verified"
      */
      print("Changing order status to Verified");
      order.setStatus("Verified");
   }
   void print(String s) {
      System.out.println(s);
   }
}

Note that the bean throws EJBException on encountering any error.

Note

Message-driven bean methods, unlike the methods of session and entity beans, do not throw application exceptions and can't throw exceptions to the client. The onMessage() method should not throw application exceptions or the java.rmi.RemoteException.

Message-driven beans can throw only system exceptions from their methods. In such situations, the EJB container performs the following: log the exception; on error, roll back the current transaction, if any; and discard the instance.

However EJB 2.1 removed the restriction that the methods of a message listener interface must not throw application exceptions.


Note

In EJB 2.1, a JMS message-driven bean implements the javax.jms.MessageListener interface and a JAXM message-driven bean implements the javax.xml.messaging.OnewayListener or javax.xml. messaging.ReqRespListener interface. Also, the message-driven bean can implement more than one type of message listener interface.

In addition, in EJB 2.1, a message-driven bean can implement the javax.ejb.TimedObject interface for time-based event notifications. The EJB container invokes the bean instance's ejbTimeout() method when the timer for the bean has expired.


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

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