Home > Articles > Programming > Windows Programming

  • Print
  • + Share This
This chapter is from the book

Message-driven Bean

One of main improvements in EJB 2.0 is the introduction of the Message-driven Bean (MDB). MDB improves the programming of the server side although it does not affect the client side at all.

Let's look at OrderManagementListener again. Did you notice that queueReceiver.receive() was called within a while(true) block? This indicates that messages are processed sequentially. Of course, you can extend the program for concurrent processing. If you provide an MDB class, the EJB container dispatches messages to a MDB instance, so as to process messages concurrently.

Listing 3 is OrderManagementMDB. The functionality is the same as in OrderManagementListener, but there is no code to set up a queue connection and session. In other words, basic operations for getting a message from the queue are performed by an EJB platform; accordingly, MDB just receives the dispatched messages.

Listing 3: OrderManagementMDB Class

public class OrderManagementMDB implements MessageDrivenBean {

  public void onMessage(Message inMessage) {
    ObjectMessage msg = null;
    try {
      if (inMessage instanceof TextMessage) {
        msg = (ObjectMessage)inMessage;
        OrderData order = (OrderData)msg.getObject();
        // Invoke order management system
      } else {
      }
    } catch (Exception e) {}
  }
  
}

Although OrderManagementMDB defines four methods, only onMessage() is shown here. In contrast to OrderManagementListener, a procedure for receiving messages from the queue is not necessary. So, this class can focus on extracting the OrderData object and invoking the order management system.

MDB is convenient; however, we need an EJB container for performing it. On the other hand, the JMS client can be a standalone Java program like OrderManagementListener. In some cases, you may use JMS directly (you may not have a proper EJB container in your platform).

  • + Share This
  • 🔖 Save To Your Account