7 Using EJB 3.0 Compliant MDBs

The following topics provide information on how to program and implement EJB 3.0 compliant MDBs:

Implementing EJB 3.0 Compliant MDBs

To implement EJB 3.0 compliant MDBs, follow the steps described in "Overview of the EJB 3.0 Development Process" in Programming WebLogic Enterprise JavaBeans, Version 3.0 for Oracle WebLogic Server.

Programming EJB 3.0 Compliant MDBs

To program EJB 3.0 compliant MDBs, follow the steps described in "Programming the Bean File: Typical Steps" in Programming WebLogic Enterprise JavaBeans, Version 3.0 for Oracle WebLogic Server.

You must use the @javax.ejb.MessageDriven annotation to declare the EJB type as Message-driven. You can specify the following optional attributes:

  • messageListenerInterface—Specifies the message listener interface, if you haven't explicitly implemented it or if the bean implements additional interfaces.


    The bean class must implement, directly or indirectly, the message listener interface required by the messaging type that it supports or the methods of the message listener interface. In the case of JMS, this is the javax.jms.MessageListener interface.
  • activationConfig—Specifies an array of activation configuration name-value pairs that configure the bean in its operational environment. See activationConfig Properties.

For detailed information on developing MDBs to support the messaging modes as described in MDBs and Messaging Models, see Programming and Configuring MDBs: Details.

Example MDB Using Annotations

This example demonstrates EJB 3.0 annotations for an MDB that references resources that are not injected. The references are resolved at runtime when the MDB is invoked instead of when the MDB instances are instantiated.

Example 7-1 Non-Injected Resources MDB Example

package test;
import javax.annotation.Resources;
import javax.annotation.Resource;
import javax.naming.*;
import javax.ejb.*;
import javax.jms.*;
import weblogic.javaee.MessageDestinationConfiguration;
import weblogic.javaee.TransactionTimeoutSeconds;
  name = "MyMDB",
  mappedName = "JNDINameOfMDBSourceDest"
// optionally specify a connection factory
// there's no need to specify a connection factory if the source
// destination is a WebLogic JMS destination
  connectionFactoryJNDIName = "JNDINameOfMDBSourceCF"  
// optionally set a tx timeout, the default timeout is typically 30 seconds
@TransactionTimeoutSeconds(value = 60) 
// resources that are not injected
@Resources ({
public class MyMDB implements MessageListener {
  // inject a reference to the MDB context
  private MessageDrivenContext mdctx;  
  // cache targetCF and targetDest for re-use (performance) 
  private ConnectionFactory targetCF;
  private Destination targetDest;
  public void onMessage(Message message) {
    Connection jmsConnection = null;
    try {
      System.out.println("My MDB got message: " + message);
      if (targetCF == null) 
        targetCF = (javax.jms.ConnectionFactory)mdctx.lookup("targetCFRef");
      if (targetDest == null)
        targetDest = (javax.jms.Destination)mdctx.lookup("targetDestRef");
      jmsConnection = targetCF.createConnection();
      Session s = jmsConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
      MessageProducer mp = s.createProducer(null);
      if (message.getJMSReplyTo() != null) 
        mp.send(message.getJMSReplyTo(), s.createTextMessage("My Reply"));
        mp.send(targetDest, message);
    } catch (JMSException e) {
      throw new EJBException(e); 
    } finally {
      // Return JMS resources to the resource reference pool for later re-use.
      // Closing a connection automatically also closes its sessions, etc.
      try { if (jmsConnection != null) jmsConnection.close(); }
      catch (JMSException ignored) {};

activationConfig Properties

activationConfig properties are name-value pairs that are passed to the MDB container when an MDB is deployed. The properties can be declared in either an ejb-jar.xml deployment descriptor or using the @ActivationConfigProperty annotation on the MDB bean class. See "javax.ejb.ActivationConfigProperty" in Programming WebLogic Enterprise JavaBeans, Version 3.0 for Oracle WebLogic Server.

Example 7-2 Example @ActivationConfigProperty Code

. . .
            name="connectionFactoryJndiName", value="JNDINameOfMDBSourceCF"
            name="initialContextFactory", value="weblogic.jndi.WLInitialContextFactory"
. . .


If activationConfig properties names conflict with with existing descriptors, use the descriptor's priority order to resolve conflicts. The a descriptors priority order (from highest to lowest) is: weblogic-ejb-jar.xml, WebLogic Server 10.0 annotations, ejb-jar.xml, and finally activationConfig properties. For example, if the same descriptor exists in the weblogic-ejb-jar.xml and ejb-jar.xml, weblogic-ejb-jar.xml has the higher priority order and overrides the ejb-jar.xml value.

Table 10-4 summarizes activationConfig name-value pairs supported by WebLogic Server.