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.  

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

Case 1: JBoss Messaging
If you are running JBoss 5, which ships with JBoss messaging you can use the following descriptor:

<mbean code="org.jboss.jms.server.destination.QueueService"
  <depends optional-attribute-name="ServerPeer">jboss.messaging:service=ServerPeer</depends>

Case 2: JBoss MQ
If you using JBoss 4 MQ provider, the configuration file is the following:

  <mbean code=""
    <depends optional-attribute-name="DestinationManager"></depends>

Example Code:

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;

            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);


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

            Scanner keyIn = new Scanner(; 

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

            if(ic != null)
                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.
    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
        System.out.println("Message received: "+strMessage);

    private void closeConnection(Connection con)
            if (con != null)
        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( );
        " org.jboss.naming:org.jnp.interfaces");
        p.put(Context.PROVIDER_URL, "jnp://localhost:1099");
        return new javax.naming.InitialContext(p);


Related articles available on

How to configure a Queue in JBoss ?

This article has been moved here: JBoss JMS configuration

How to create a Queue with Jmx Console ?

  Bring up the JMX Console in your browser and look for the sect

JBoss JMS Topic example

The following article shows how to create a simple JMS Topic Publ

JBoss HornetQ simple tutorial

HornetQ is an open source project to build a multi-protocol, embe

How do I configure a Queue/Topic to work in a cluster?

  JBoss AS 5 Just set the Clustered attribute to "true" in your

JBoss JMS configuration

The Java Messaging Service (JMS) has been implemented by means of