How to use a Resource Adapter in your MDB?

This article is a walk though the configuration of a Resource Adapter in WildFly application server. At first, we will learn how to configure it in the resource-adapter subsystem and how to use the adapter in your MDB..

You can configure a Resource Adapters through the resource-adapters subsystem. Behind the hoods, the IronJacamar project provides support for the Java Connector Architecture.

jboss configure resource adapter

Configuring the Resource Adapter involves the following steps:

  1. At first, you need to deploy the Resource Adapter (or as an alternative, install is as a module)
  2. Then, you need to add the Resource Adapter Configuration
  3. Finally, you can annotate your Message Driver Beans to use the Resource Adapter
  4. Optionally, you can also configure the Resource Adapter as the Default one for your MDBs.

Let’s see all steps in greater detail.

Installing the Resource Adapter

The simplest way to install the Resource Adapter consists in copying it under the deployments folder of the application server. Example:

cp wmq.jmsra.rar $JBOSS_HOME/standalone/deployments

Configuring the Resource Adapter

The next step will be adding the RA to the configuration. A resource adapter definition can provide support for XA Transactions. If you want to use XA Transaction after adding it you need to specify the Security Domain for it:

/subsystem=resource-adapters/resource-adapter=wmq.jmsra.rar:add(archive=wmq.jmsra.rar, transaction-support=XATransaction)

/subsystem=resource-adapters/resource-adapter=test/connection-definitions=test:write-attribute(name=recovery-security-domain,value=security-domain)

On the other hand, if you don’t need support for XA Transactions you can simply add it as follows:

/subsystem=resource-adapters/resource-adapter=wmq.jmsra.rar:add(archive=wmq.jmsra.rar, transaction-support=NoTransaction)

Finally, include the connection-definitions and config-properties of the Resource Adapter. Example:

<subsystem xmlns="urn:jboss:domain:resource-adapters:5.0">
    <resource-adapters>
        <resource-adapter id="wmq.jmsra.rar">
            <archive>
                wmq.jmsra.rar
            </archive>
            <transaction-support>NoTransaction</transaction-support>
            <connection-definitions>
                <connection-definition class-name="com.ibm.mq.connector.outbound.ManagedConnectionFactoryImpl"
                                       jndi-name="java:jboss/jms/ivt/IVTCF" enabled="true" use-java-context="true"
                                       pool-name="IVTCF">
                    <config-property name="channel">SYSTEM.DEF.SVRCONN
                    </config-property>
                    <config-property
                                     name="hostName">localhost
                    </config-property>
                    <config-property name="transportType">
                        CLIENT
                    </config-property>
                    <config-property name="queueManager">
                        QM1
                    </config-property>
                    <config-property name="port">
                        1414
                    </config-property>
                </connection-definition>
                <connection-definition class-name="com.ibm.mq.connector.outbound.ManagedConnectionFactoryImpl"
                                       jndi-name="java:jboss/jms/ivt/JMS2CF" enabled="true" use-java-context="true"
                                       pool-name="JMS2CF">
                    <config-property name="channel">
                        SYSTEM.DEF.SVRCONN
                    </config-property>
                    <config-property name="hostName">
                        localhost
                    </config-property>
                    <config-property name="transportType">
                        CLIENT
                    </config-property>
                    <config-property name="queueManager">
                        QM1
                    </config-property>
                    <config-property name="port">
                        1414
                    </config-property>
                </connection-definition>
            </connection-definitions>
        <admin-objects>
            <admin-object class-name="com.ibm.mq.connector.outbound.MQQueueProxy"
                          jndi-name="java:jboss/jms/ivt/IVTQueue" pool-name="IVTQueue">
                <config-property name="baseQueueName">
                    EXAMPLE.QUEUE
                </config-property>
                </admin-object>
            </admin-objects>
        </resource-adapter>
    </resource-adapters>
</subsystem>

Configure MDBs to use the Resource Adapter

Next, you can refer to the Resource Adapter id in your Message Driven Beans:

@MessageDriven(name="IbmMqMdb", activationConfig = {
        @ActivationConfigProperty(propertyName = "connectionFactoryLookup", propertyValue = "java:jboss/jms/ivt/IVTCF"),
        @ActivationConfigProperty(propertyName = "destinationLookup", propertyValue = "java:jboss/jms/ivt/IVTQueue"),
        @ActivationConfigProperty(propertyName = "destinationType",propertyValue = "javax.jms.Queue")
})
@ResourceAdapter(value = "wmq.jmsra.rar")
public class IBMMDB implements MessageListener {
    @Override
    public void onMessage(Message message) {
       ...
    }
}

Please notice that the above @ResourceAdapter annotation requires a JBoss specific dependency to be used:

<dependency>
         <groupId>org.jboss.ejb3</groupId>
         <artifactId>jboss-ejb3-ext-api</artifactId>
         <scope>provided</scope>
</dependency>

Decoupling the Resource Adapter from the MDB

If you want to avoid tight coupling between the RAR file and the MDB, you can specify the Resource Adapter name at deployment descriptor level (jboss-ejb3.xml). Example:

<jboss xmlns="http://www.jboss.com/xml/ns/javaee" 
    xmlns:jee="http://java.sun.com/xml/ns/javaee"
    xmlns:mdb="urn:resource-adapter-binding"
    xmlns:security="urn:security">

    <jee:assembly-descriptor>
        <mdb:resource-adapter-binding>
            <jee:ejb-name>SOCKET_MDB</jee:ejb-name>
            <mdb:resource-adapter-name>wmq.jmsra.rar</mdb:resource-adapter-name>
        </mdb:resource-adapter-binding>
    </jee:assembly-descriptor>
</jboss>

With the above jboss-ejb3.xml configuration in place, you can refer to the generic “SOCKET_MDB” in your @MessageDriven annotation:

@MessageDriven(name = "SOCKET_MDB", activationConfig = {
        @ActivationConfigProperty(propertyName = "destination", propertyValue = "queue.demoQueue"),
        @ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue"),
        @ActivationConfigProperty(propertyName = "acknowledgeMode", propertyValue = "Auto-acknowledge")
},mappedName = "java:/jms/demoQueue")

public class HelloWorldQueueMDB implements MessageListener {

   
}

Configuring the Resource Adapter as the default one

By default, the application server uses the ArtemisMQ Resource adapter as the default one for your MDB Pool:

<mdb>
    <resource-adapter-ref resource-adapter-name="${ejb.resource-adapter-name:activemq-ra.rar}"/>
    <bean-instance-pool-ref pool-name="mdb-strict-max-pool"/>
</mdb>

To use a different one for your EJB Container, simply change the resource-adapter-name property accordingly. For example:

<mdb>
    <resource-adapter-ref resource-adapter-name="${ejb.resource-adapter-name:wmq.jmsra.rar}"/>
    <bean-instance-pool-ref pool-name="mdb-strict-max-pool"/>
</mdb>
Found the article helpful? if so please follow us on Socials