Configuring a JMS Queue example in JBoss 5

Note: This tutorial has been written for JBoss AS 5.
To learn how to configure a JMS Queue / Topic in WildFly or JBoss EAP 7 we recommend checking this tutorial: JBoss JMS configuration

The following article shows how to create a simple JMS Queue Producer and Consumer. This example uses JBoss Initial Context to run, however it can be easily adapted to any JMS Provider.  

JMS Queue MBean

At first you need to deploy a JMS Queue to JBoss. Create a file ending with -service.xml in the deploy folder of JBoss.

You can also include the JMS destinations into the default file destinations-service.xml.

Here is an example of JMS Queue definition whose name is “queueA”:

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

Example Code to use this Queue:

package com.sample;

import java.util.Properties;
import java.util.Scanner;

import javax.jms.*;

import javax.naming.Context;

public class QueueExample implements MessageListener  
{

    public void example() throws Exception
    {
        String destinationName = "queue/queueA";

        Context ic = null;
        ConnectionFactory cf = null;
        Connection connection =  null;

        try
        {         
            ic = getInitialContext();

            cf = (ConnectionFactory)ic.lookup("/ConnectionFactory");
            Queue queue = (Queue)ic.lookup(destinationName);

            connection = cf.createConnection();
            Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
            MessageProducer publisher = session.createProducer(queue);
            MessageConsumer subscriber = session.createConsumer(queue);

            subscriber.setMessageListener(this);
            connection.start();

            TextMessage message = session.createTextMessage("Hello!");
            publisher.send(message);

            Scanner keyIn = new Scanner(System.in); 

            System.out.print("JMS Server listening. Type a Key + CR to exit\n");
            keyIn.next();

        }
        finally
        {         
            if(ic != null)
            {
                try
                {
                    ic.close();
                }
                catch(Exception e)
                {
                    throw e;
                }
            }

            // ALWAYS close your connection in a finally block to avoid leaks.
            // Closing connection also takes care of closing its related objects e.g. sessions.
            closeConnection(connection);
        }
    }
    public synchronized void onMessage(Message message)
    {
        TextMessage text = (TextMessage)message;
        String strMessage = null;
        try {
            strMessage = text.getText();
        } catch (JMSException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        System.out.println("Message received: "+strMessage);
    }

    private void closeConnection(Connection con)
    {      
        try
        {
            if (con != null)
            {
                con.close();
            }         
        }
        catch(JMSException jmse)
        {
            System.out.println("Could not close connection " + con +" exception was " + jmse);
        }
    }

    protected boolean isQueueExample()
    {
        return true;
    }

    public static void main(String[] args) throws Exception
    {
        new QueueExample().example();
    }
    public static Context getInitialContext( )
    throws javax.naming.NamingException {

        Properties p = new Properties( );
        p.put(Context.INITIAL_CONTEXT_FACTORY,
                "org.jnp.interfaces.NamingContextFactory");
        p.put(Context.URL_PKG_PREFIXES,
        " org.jboss.naming:org.jnp.interfaces");
        p.put(Context.PROVIDER_URL, "jnp://localhost:1099");
        return new javax.naming.InitialContext(p);
    }  
}

How to configure a Clustered JMS Queue in JBoss AS 5

To enable Clustered JMS destinations, set the clustered attribute to “true” in your Queue/Topic definition. Here’s a Queue example: 
 <mbean code="org.jboss.jms.server.destination.QueueService"
      name="jboss.messaging.destination:service=Queue,name=SampleQueue"
      xmbean-dd="xmdesc/Queue-xmbean.xml">
      <depends optional-attribute-name="ServerPeer">jboss.messaging:service=ServerPeer</depends>
      <depends>jboss.messaging:service=PostOffice</depends>
      <attribute name="Clustered">true</attribute>

   </mbean>

Additionally, you need to use a ClusteredConnectionFactory instead of the default ConnectionFactory. The ClusteredConnectionFactory is bound into the following JNDI contexts:

  • /ClusteredConnectionFactory
  • /ClusteredXAConnectionFactory
  • java:/ClusteredConnectionFactory
  • java:/ClusteredXAConnectionFactory

If you want to learn how to cluster JMS with WildFly check: JMS Clustering in WildFly and JBoss EAP