Deploying message-driven beans to connect to the embedded messaging server
Use the message-driven beans (MDB) to connect to the embedded messaging server.
About this task
- When both MDB and the messaging engine are on the same Liberty server
- When MDB is connected to a remote messaging engine that is running on a different Liberty server. The following diagram depicts the
two configuration scenarios for MDB. The first scenario is when both messaging engine and MDB are on
the same Liberty server. The second scenario
is when MDB is connecting to a messaging engine that is on a different server.

Procedure
-
Configure the MDB feature.
Enable MDB support by configuring the
jmsMdb-3.1andwasJmsClient-2.0features in the server.xml file. If you want to perform a JNDI lookup, then you must also add thejndi-1.0feature along with the other two features.<featureManager> <feature>jmsMdb-3.1</feature> <feature>wasJmsClient-2.0</feature> <feature>jndi-1.0</feature> </featureManager>Configuring the
wasJmsClient-2.0feature enables you to define the required JMS resources and enables MDB to interact with the messaging engine.Important: ThewasJmsClient-2.0feature supports the features of both JMS 1.1 and JMS 2.0 specifications. However, you can choose to use thewasJmsClient-1.1feature if you want to use only the features that are compliant with JMS 1.1 specification. -
Configure a JCA activation specification that uses the JMS resource adapter so that the MDB
acts as a listener on a specific JMS destination.
Choose one of the following three options to configure the MDB to interact with the messaging engine:
- Define activation specification properties in the server.xml file.
- Define resource information in the EJB binding file.
- Define resource information in the
applicationelement in the server.xml file.
-
Option 1: Define activation specification properties in the server.xml file.
You can define the property in the server.xml file so that the MDB can use the property to listen on a specific JMS destination.<jmsActivationSpec id="JMSSample/JMSSampleMDB"> <properties.wasJms destinationRef="jndi/MDBQ" /> </jmsActivationSpec> <jmsQueue id="jndi/MDBQ" jndiName="jndi/MDBQ"> <properties.wasJms queueName="Q1"/> </jmsQueue>The
destinationRefattribute refers to the ID of thejmsQueueelement. If no ID is defined for thejmsQueueelement, then thedestinationRefattribute must point to thejndiNamevalue of thejmsQueueelement.Note: The ID value must be in theapplication name/module name/bean nameformat.- application name is the name of the application that is deployed (for example, JMSSample). The application name applies only if the bean is packaged within an EAR file. The application defaults to the base name of the EAR file with no file name extension unless specified by the application.xml deployment descriptor.
- module name is the name of the module in which the bean is packaged. In a
stand-alone ejb-jar file or WAR file, the
module-namevalue defaults to the base name of the module with any file name extension removed. In an EAR file, themodule-namevalue defaults to the path name of the module with any file name extension removed, but with any directory names included. You can override the defaultmodule-namevalue by using themodule-nameelement of ejb-jar.xml (for ejb-jar files) or web.xml (for WAR files). - bean name is the
ejb-nameof the enterprise bean. For enterprise beans defined through annotation, the bean name defaults to the unqualified name of the session bean class, unless specified in the contents of the name() attribute of theMessageDrivenannotation. For enterprise beans defined through ejb-jar.xml, it is specified in theejb-namedeployment descriptor element.
-
Option 2: Define resource information in the ibm-ejb-jar-bnd.xml EJB binding file.
You can also use this binding file to define the resource information that is required for the MDB to connect to the messaging engine. When you are using the EJB binding file, the activation specification property ID need not necessarily be in the
application name/module name/bean nameformat.Define the activation specification properties in the server.xml file.<jmsActivationSpec id="PriceChangeAS"> <properties.wasJms destinationRef="jms/TriggerQ" /> </jmsActivationSpec> <jmsQueue id="jms/TriggerQ" jndiName="jms/TriggerQ"> <properties.wasJms queueName="Q1"/> </jmsQueue>Add the following MDB-binding information in the ibm-ejb-jar-bnd.xml file.<ejb-jar-bnd xmlns="http://websphere.ibm.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://websphere.ibm.com/xml/ns/javaee http://websphere.ibm.com/xml/ns/javaee/ibm-ejb-jar-bnd_1_1.xsd" (http://websphere.ibm.com/xml/ns/javaee/ibm-ejb-jar-bnd_1_1.xsd%27) version="1.1"> <message-driven name="PriceChangeMDBBean"> <jca-adapter activation-spec-binding-name="PriceChangeAS" destination-binding-name="jms/TriggerQ" /> </message-driven> </ejb-jar-bnd>Note: When you use the EJB binding file, the activation-spec-binding-name attribute in the ibm-ejb-jar-bnd.xml file must point to the activation specification property ID value that is specified in the server.xml file. -
Option 3: Define resource information in the
applicationelement in the server.xml file.You can define the server.xml configuration information in the same form as the information that is specified in the ibm-ejb-jar-bnd.xml file, as shown in the following example:
<application location="PriceChangeApp.ear"> <ejb-jar-bnd moduleName="PriceChangeEJB"> <message-driven name="PriceChangeMDBBean"> <jca-adapter activation-spec-binding-name="PriceChangeAS" destination-binding-name="jms/TriggerQ" /> </message-driven> </ejb-jar-bnd> </application>Note: Specify the moduleName attribute for theejb-jar-bndelement. The value is the name of the JAR file that contains the MDB without the .jar extension. For example, if the MDB is contained in PriceChangeEJB.jar, the moduleName attribute isPriceChangeEJB.
- Optional: Provide activation configuration properties to the
ActivationSpecinstance by specifying annotations on the MDB.Liberty supports defining annotations for MDBs that can be used with the activation specification property that is defined in the server.xml file. To use the annotation, first define the activation specification property as described in the previous step. For each of the MDB, you can define the annotations, as shown in the following example:
@MessageDriven( name = "JMSSampleMDB", activationConfig = { @ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue"), @ActivationConfigProperty(propertyName = "userName", propertyValue = "user1"), @ActivationConfigProperty(propertyName = "password", propertyValue = "user1pwd"), @ActivationConfigProperty(propertyName = "destination", propertyValue = "jndi_INPUT_Q") } ) public class JMSSampleMDB implements MessageListener{ @TransactionAttribute(value = TransactionAttributeType.REQUIRED) public void onMessage(Message message) { } }