How to use a Resource Adapter in your MDB?

In JBoss AS 7 you can bind your MDBs to a specific Resource Adapter in three ways. Here's how to do it!

1. At deployment descriptor level


<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>activemq-rar-5.7.0.rar</mdb:resource-adapter-name>
        </mdb:resource-adapter-binding>
    </jee:assembly-descriptor>
</jboss>

This is a sample jboss-ejb3.xml file which can be deployed along with your EJB (or WEB) Project:

2. Annotate it in your MDB

Another option is by using the @ResourceAdapter annotation within your MDB:


@MessageDriven(
activationConfig = { 
@ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue"),
@ActivationConfigProperty(propertyName="destination", propertyValue="java:jboss/activemq/queue/LocalRPCRequest")
}, 
name = "ActiveMQMDB")
@ResourceAdapter(value="activemq-rar-5.7.0.rar")
public class ActiveMQMDB implements MessageListener {

    public ActiveMQMDB() {}

    @Override
    public void onMessage(Message message) {
    try {    
    if(message instanceof TextMessage) {
    System.out.println("Got Message "+((TextMessage)message).getText());
}
        }
        catch (JMSException e) {
        e.printStackTrace();
        }
    }
}

3. Configure it at MDB Pool level

If you want to configure the Resource adapter for all the MDBs in your pool, then you can just configure it in the EJB3 subsystem:


<subsystem xmlns="urn:jboss:domain:ejb3:1.4">

. . . . .

    <mdb>
        <resource-adapter-ref resource-adapter-name="activemq-rar-5.7.0.rar"/>
        <bean-instance-pool-ref pool-name="mdb-strict-max-pool"/>
    </mdb>

</subsystem>

Remember that you need to declare your resource adapter in a file name ironjacamar.xml which can be deployed alogn with your .rar file, or configure the resource adapter into the resource-adapters subsystem:


<subsystem xmlns="urn:jboss:domain:resource-adapters:1.0">
    <resource-adapters>
        <resource-adapter>
            <archive>
                activemq-rar-5.7.0.rar
            </archive>
        </resource-adapter>
    </resource-adapters>
</subsystem>

Do you need some complete tutorials for configuring an external JMS provider on JBoss AS 7? Have a look here!

Follow us on Twitter