Deploying EJB3 Message Driven Beans in JBoss

Back in the ‘old days’ if you wanted to configure a message driven bean you’d need to write reams of accompanying XML configuration. Thankfully, EJB3 came along, and with it the wonderful world of annotations. Now all you need to do is annotate your implementation of the MessageListener interface, pass it over to JBoss and have the application server take care of things for you.

Let’s take a look at an example:

package com.lansdowntech.examples.mdb;
 
import javax.ejb.ActivationConfigProperty;
import javax.ejb.MessageDriven;
 
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.ObjectMessage;
 
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
 
@MessageDriven(name = "ExampleMDB", activationConfig = {
        @ActivationConfigProperty(
                propertyName = "destinationType",
                propertyValue = "javax.jms.Queue"),
        @ActivationConfigProperty(
                propertyName = "destination",
                propertyValue = "queue/ExampleQueue")
})
public class ExampleMDB implements MessageListener
{
    private static final Log log = LogFactory.getLog(ExampleMDB.class);
 
    public void onMessage (Message msg)
    {
        try
        {
            log.debug("Processing message from ExampleQueue");
            ObjectMessage oMsg = (ObjectMessage) msg;
 
            /** Your business logic goes here... **/
        }
        catch (Exception e)
        {
            log.error("Unable to process message from ExampleQueue", e);
        }
    }
}

In JBoss 4 that’s all you need do; the app server will automatically handle everything for you, even creating the queue dynamically if it doesn’t already exist. Be warned though, that JBoss 5 isn’t quite so generous and you’ll need to add some additional config to get things working. There’s two options here – either you can declare the queue yourself, or you can explicitly instruct JBoss to do it for you.

Let’s take a look at both of those options.

Declare the queue yourself
This is probably the most obvious solution, simply add an appropriate declaration of the JMS queue yourself. As with most things, this can be achieved with an appropriate XML config file located within the JBoss deploy directory.

Here’s an example:

<server>
    <mbean
        code="org.jboss.jms.server.destination.QueueService"
        name="jboss.messaging.destination:service=Queue,name=ExampleQueue"
        xmbean-dd="xmdesc/Queue-xmbean.xml">
        <depends optional-attribute-name="ServerPeer">
            jboss.messaging:service=ServerPeer
        </depends>
        <depends>
            jboss.messaging:service=PostOffice
        </depends>
    </mbean>
</server>

Add deployment descriptors to auto create the queue
If you want JBoss 5 to continue to create the queue for you, on deployment of your MDB, you’ll need to make an edit to jboss.xml within your JAR. If you’re not already using one, jboss.xml is an optional configuration file which you should add to the META-INF folder of your application. The key thing you need to add is to set create-destination to true. The advantage to this approach is that you can maintain all the config within your application; ready to deploy on an “out of the box” JBoss installation. The main downside is that this starts to introduce JBoss-specific functionality to your application – something you may be trying to avoid.

Here’s an example:

<?xml version="1.0" encoding="UTF-8"?>
<jboss xmlns="http://www.jboss.com/xml/ns/javaee"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.jboss.com/xml/ns/javaee
                           http://www.jboss.org/j2ee/schema/jboss_5_0.xsd"
       version="3.0">
    <enterprise-beans>
        <message-driven>
            <ejb-name>ExampleMDB</ejb-name>
            <destination-jndi-name>queue/ExampleQueue</destination-jndi-name>
            <create-destination>true</create-destination>
        </message-driven>
    </enterprise-beans>
</jboss>

Leave a Reply

Your email address will not be published. Required fields are marked *


4 + eight =

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>