Advertise with us

Our eBooks

JBoss HornetQ simple tutorial

Deploy a simple MDB 

Here is an example of MDB from the example suite which demonstrate sending a message and the MDB consuming only the message that matches the message selector.
package org.hornetq.javaee.example.server;

import org.jboss.ejb3.annotation.ResourceAdapter;

import javax.ejb.ActivationConfigProperty;
import javax.ejb.MessageDriven;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;
import javax.ejb.TransactionManagement;
import javax.ejb.TransactionManagementType;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;

 * @author Andy Taylor
@MessageDriven(name = "MDBMessageSelectorExample",
               activationConfig =
                        @ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue"),
                        @ActivationConfigProperty(propertyName = "destination", propertyValue = "queue/testQueue"),
                        @ActivationConfigProperty(propertyName = "messageSelector", propertyValue = "color = 'RED'")
@TransactionManagement(value= TransactionManagementType.CONTAINER)
@TransactionAttribute(value= TransactionAttributeType.REQUIRED)
public class MDBMessageSelectorExample implements MessageListener
   public void onMessage(Message message)
         //Step 11. We know the client is sending a text message so we cast
         TextMessage textMessage = (TextMessage)message;

         //Step 12. get the text from the message.
         String text = textMessage.getText();

         //Step 13. We check we received the right color of message
         String color = textMessage.getStringProperty("color");

         System.out.println("message " + text + " received color=" + color);

      catch (Exception e)
Ok. In order to connect to the MDB, you have to provide the following exports in your messaging configuration:
    . . . . .
    <connection-factory name="RemoteConnectionFactory">
            <connector-ref connector-name="netty"/>
            <entry name="RemoteConnectionFactory"/>
            <entry name="java:jboss/exported/jms/RemoteConnectionFactory"/>


    <jms-queue name="testQueue">
        <entry name="queue/test"/>
        <entry name="java:jboss/exported/jms/queues/testQueue"/>
And finally here's a generic test client which can be used to send messages to the "testQueue":
package org.hornetq.javaee.example;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.MessageProducer;
import javax.jms.Queue;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.naming.Context;
import javax.naming.InitialContext;
import java.util.Properties;

 * @author Andy Taylor
public class MDBMessageSelectorClientExample
   public static void main(String[] args) throws Exception
      Connection connection = null;
      InitialContext initialContext = null;
         //Step 1. Create an initial context to perform the JNDI lookup.
         final Properties env = new Properties();

         env.put(Context.INITIAL_CONTEXT_FACTORY, "org.jboss.naming.remote.client.InitialContextFactory");

         env.put(Context.PROVIDER_URL, "remote://localhost:4447");

         env.put(Context.SECURITY_PRINCIPAL, "guest");

         env.put(Context.SECURITY_CREDENTIALS, "password");

         initialContext = new InitialContext(env);

         //Step 2. Perfom a lookup on the queue
         Queue queue = (Queue) initialContext.lookup("jms/queues/testQueue");

         //Step 3. Perform a lookup on the Connection Factory
         ConnectionFactory cf = (ConnectionFactory) initialContext.lookup("jms/RemoteConnectionFactory");

         //Step 4.Create a JMS Connection
         connection = cf.createConnection("guest", "password");

         //Step 5. Create a JMS Session
         Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);

         //Step 6. Create a JMS Message Producer
         MessageProducer producer = session.createProducer(queue);

         //Step 7. Create a Text Message and set the color property to blue
         TextMessage blueMessage = session.createTextMessage("This is a text message");

         blueMessage.setStringProperty("color", "BLUE");

         System.out.println("Sent message: " + blueMessage.getText() + " color=BLUE");

         //Step 8. Send the Message

         //Step 9. create another message and set the color property to red
         TextMessage redMessage = session.createTextMessage("This is a text message");

         redMessage.setStringProperty("color", "RED");

         System.out.println("Sent message: " + redMessage.getText() + " color=RED");

         //Step 10. Send the Message
          //Step 10,11 and 12 in MDBMessageSelectorExample
         //Step 13. Be sure to close our JMS resources!
         if (initialContext != null)
         if(connection != null)
Your example is completed.In order to build the example, the following dependencies need to be set in your Maven project:


© 2021 All Rights Reserved.

Please publish modules in offcanvas position.