Skip to content
Pere Villega
Go back

Message-Driven Beans

This page contains a brief summary of EJB 3.0 Message-Driven Beans (MDB). It only gives basic information, for more details about life cycle and other functionalities check the EJB 3.0 reference or some book like EJB3 in Action.

Message-Driven Beans are an abstraction over Java Messaging Service (JMS). They are used to send and receive messages in your application without having to know all the details about JMS. That said a good understanding of JMS is important for the correct use of MDB. The advantages of MDB over standard JMS coding are the services provided by the container (like pooling) and the reduction on code size due to automation of tasks.

By message (in the context of a JEE application) we mean an asynchronous and loosely coupled communication between system components. Using Message-Oriented Middleware (MOM) messages sent are stored until a consumer is available to read them. We consider two types of communication:

MDB are POJOs like other EJB and require a public constructor without arguments. You can’t throw any RuntimeException or RemoteException as that would cause the MDB to be terminated. They also have the following requirements:

Example of MDB listening to a Queue (point-to-point communication) named SampleServerQueue:


@MessageDriven(mappedName = "SampleServerQueue", activationConfig = {
  @ActivationConfigProperty(propertyName = "acknowledgeMode",
     propertyValue = "Auto-acknowledge"),
  @ActivationConfigProperty(propertyName = "destinationType",
     propertyValue = "javax.jms.Queue")
})
public class MDBTestBean implements MessageListener {

  //create the references to a destination Queue
  //where we will send messages using DI
  @Resource(name = "jms/ShippingErrorQueue")
  private Destination errorQueue;

  @Resource(name = "jms/QueueConnectionFactory")
  private ConnectionFactory connectionFactory;

  private Connection jmsConnection;

  public MDBTestBean() { }

  //message consumer, reads a message from the queue
  public void onMessage(Message message) {
    // code
  }

  //send a message
  public void sendMessage() {

    try {
      Session session = jmsConnection.
        createSession(true, Session.AUTO_ACKNOWLEDGE);
      MessageProducer producer = session.createProducer(errorQueue);
      ObjectMessage message = session.createObjectMessage();

      //add info to message
      message.setObject("Message");

      producer.send(message);
      session.close();

    } catch (JMSException ex) {
      Logger.getLogger(MDBTestBean.class.getName()).
        log(Level.SEVERE, null, ex);
    }

  }


  @PostConstruct
  private void onConstruct() {

    try {
      jmsConnection = connectionFactory.createConnection();
    } catch (JMSException ex) {
      Logger.getLogger(MDBTestBean.class.getName()).
        log(Level.SEVERE, null, ex);
    }

  }


  @PreDestroy
  private void onDestroy() {

    try {
      jmsConnection.close();
    } catch (JMSException ex) {
      Logger.getLogger(MDBTestBean.class.getName()).
        log(Level.SEVERE, null, ex);
    }

  }

}

Share this post on:

Previous Post
Java Persistence API
Next Post
Ubuntu Server Maintenance