Developing message-driven beans

You can develop a bean implementation class for a message-driven bean as introduced by the Enterprise JavaBeans specification. A message-driven bean (MDB) is a message consumer that implements business logic and runs on the server.

Before you begin

Determine the messaging model you want for your application regarding use of topics, queues, producers and consumers, publish or subscribe, and so on. You can refer to the message-driven bean component contract that is described in the Enterprise JavaBeans™ specification.

About this task

A message-driven bean (MDB) is a consumer of messages from a Java™ Message Service (JMS) provider. An MDB is invoked on arrival of a message at the destination or endpoint that the MDB services. MDB instances are anonymous, and therefore, all instances are equivalent when not actively servicing a client message. The container controls the life cycle of bean instances, which hold no state that is visible to a client.

The following example is a basic message-driven bean:

@MessageDriven(activationConfig={
                @ActivationConfigProperty(propertyName="destination",     propertyValue="myDestination"),
                @ActivationConfigProperty(propertyName="destinationType", propertyValue="javax.jms.Queue")
})
public class MsgBean implements javax.jms.MessageListener {

  public void onMessage(javax.jms.Message msg) {

      String receivedMsg = ((TextMessage) msg).getText();
      System.out.println("Received message: " + receivedMsg);

   }

}
As with other enterprise bean types, you can also declare metadata for message-driven beans in the deployment descriptor rather than using annotations, for example:
<?xml version="1.0" encoding="UTF-8"?>

<ejb-jar id="EJBJar_1060639024453" version="3.0"
      xmlns="http://java.sun.com/xml/ns/javaee"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/ejb-jar_3_0.xsd"
      metadata-complete="false">
  <enterprise-beans>

    <message-driven>

      <ejb-name>MsgBean</ejb-name>
      <ejb-class>com.acme.ejb.MsgBean</ejb-class>
      <activation-config>
         <activation-config-property>
            <activation-config-property-name>destination</activation-config-property-name>
            <activation-config-property-value>myDestination</activation-config-property-value>
         </activation-config-property>
         <activation-config-property>
           <activation-config-property-name>destinationType</activation-config-property-name>
           <activation-config-property-value>javax.jms.Queue</activation-config-property-value>
        </activation-config-property>
      </activation-config>

    </message-driven>

  </enterprise-beans>
</ejb-jar>
Note: In WebSphere® Application Server version 9, the destinationLookup property can also be used instead of the destination activation configuration property. Both the activation configuration properties serve the same purpose of setting the destination JNDI name for MDBs. However, when both the activation properties are defined in the configuration, the destinationLookup property takes precedence over the destination property.

Procedure

Results

You developed a simple message-driven bean, along with some deployment and packaging options.

What to do next

Read related information about designing an enterprise application that uses message-driven beans.

指出主題類型的圖示 作業主題



時間戳記圖示 前次更新: July 9, 2016 11:15
http://www14.software.ibm.com/webapp/wsbroker/redirect?version=cord&product=was-nd-mp&topic=tejb_mdb
檔名:tejb_mdb.html