You can connect to WebSphere® MQ by using the message-driven beans (MDB).
Enable MDB support in the Liberty profile by configuring the jmsMdb-3.1 and wmqJmsClient-1.1 features in the server.xml file. If you want to perform a JNDI lookup, then you must also add the jndi-1.0 feature along with the other two features.
<featureManager>
<feature>jmsMdb-3.1</feature>
<feature>wmqJmsClient-1.1</feature>
<feature>jndi-1.0</feature>
</featureManager>
Configuring the wmqJmsClient-1.1 feature enables the users to define the required JMS resources and enables MDB to interact with the messaging engine.
<variable name="wmqJmsClient.rar.location" value="/path/to/wmq/rar/wmq.jmsra.rar"/>
where
the value attribute specifies the absolute path to
the WebSphere MQ Resource
Adapter file, wmq.jmsra.rar.For details of the supported versions, and how to obtain the wmq.jmsra.rar file and install it, refer to the WebSphere MQ technote 1633761, Obtaining the WebSphere MQ Resource Adapter for the WebSphere Application Server Liberty Profile.
<jmsActivationSpec id="JMSSample/JMSSampleMDB">
<properties.wmqJms destinationRef="jndi/MDBQ" transportType="CLIENT" queueManager="myQM" hostName="myHost" port="1414"/>
</jmsActivationSpec>
<jmsQueue id=”jndi/MDBQ” jndiName="jndi/MDBQ">
<properties.wmqJms baseQueueName="MYQ" baseQueueManagerName="myQM"/>
</jmsQueue>
The <destinationRef> refers to the ID of <jmsQueue>. If an ID in <jmsQueue> is not mentioned, then <destinationRef> must point to the <jndiName> of the <jmsQueue>.
Liberty profile supports defining annotations for MDBs that can be used with the activation specification property defined in the server.xml file. In order to use the annotation, first define the activation specification property as mentioned in the previous step. And for each of the MDB, user 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) {
}
}
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 format of application name/module name/bean name as mentioned in step 1.
<jmsActivationSpec id="PriceChangeAS">
<properties.wmqJms destinationRef="jms/TriggerQ" transportType="CLIENT" queueManager="myQM" hostName="myHost" port="1414"/>
</jmsActivationSpec>
<jmsQueue id=”jms/TriggerQ” jndiName="jms/TriggerQ">
<properties.wmqJms baseQueueName="Q1"/>
</jmsQueue>
<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>