JBoss JMS Topic example

The following article shows how to create a simple JMS Topic using WildFly and older versions of JBoss application server.

Creating a JMS Topic with WildFly

If you want to create a new Topic, the command to be executed from the CLI is the following one:

[standalone@localhost:9990 /] jms-topic add --topic-address=jms.topic.DemoTopic --entries=java:/jms/topic/demoTopic

The following topic will be added in your XML configuration:

<jms-topic name="jms.topic.DemoTopic" entries="java:/jms/topic/demoTopic"/>

On the other hand, if you are using Domain mode, you should specify the profile as argument:

[domain@localhost:9990 /] jms-topic add --profile=full-ha --topic-address=jms.topic.DemoTopic --entries=java:/jms/topic/demoTopic

To see an example application which uses JMS to send messages, check this tutorial: JBoss JMS configuration

Creating a Deployable Topic

JMS destinations can be also be created on the fly by dropping a *-jms.xml file in the deployments folder of your standalone server or packaging it along with your application. Here’s an example of a JMS Queue and a JMS Topic:

<messaging-deployment xmlns="urn:jboss:messaging-activemq-deployment:1.0">
            <jms-queue name="ExampleQueue">
                <entry name="java:/jms/queue/ExampleQueue"/>
            <jms-topic name="ExampleTopic">
                <entry name="java:/jms/topic/ExampleTopic"/>

Creating a JMS Topic with JBoss 5

At first you need to deploy a JMS Topic 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.TopicService"
   <depends optional-attribute-name="ServerPeer">jboss.messaging:service=ServerPeer</depends>

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

<mbean code="org.jboss.mq.server.jmx.Topic"
    <depends optional-attribute-name="DestinationManager">jboss.mq:service=DestinationManager</depends>

Example Code:

package com.sample;

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

import javax.jms.*;
import javax.naming.Context;

public class TopicExample implements MessageListener  
   public void example() throws Exception
      String destinationName = "topic/topicA";
      Context ic = null;
      ConnectionFactory cf = null;
      Connection connection =  null;

         ic = getInitialContext();
         cf = (ConnectionFactory)ic.lookup("/ConnectionFactory");
         Topic topic = (Topic)ic.lookup(destinationName);
         connection = cf.createConnection();
         Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
         MessageProducer publisher = session.createProducer(topic);
         MessageConsumer subscriber = session.createConsumer(topic);
         TextMessage message = session.createTextMessage("Hello!");
          Scanner keyIn = new Scanner(System.in); 
          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 TopicExample().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);