Publish and Subscribe messages using JMS Topic in JBoss Server

Publish and Subscribe using JMS Topic

This tips gives overview on how to write Java Messaging Service(JMS) code for creating Topic in the Tomcat server. This is very basic example and only show how to get started instead of looking into the advanced concepts in JMS technology.

also read:

In JMS, publish/subscribe messaging uses a JMS-managed object called a Topic to manage message flow from publishers to subscribers. JMS publishers are called message producers, and JMS subscribers are called message consumers. A message producer acquires a reference to a JMS Topic on a server, and sends messages to that Topic. When a message arrives, the JMS provider is responsible for notifying all message consumers subscribed to that Topic. The JMS provider (optionally) receives acknowledgment of the message receipt each time it sends the message.

This example is used JBoss server for the testing of application. Before run this example please start your JBoss server and put the following jar files in the classpath. JAR files are specific to the JBoss server MQ operations. It may not work with other vendor provided application servers.

The example program just simply add a message into the JMS Topic and subscribe to the topic.

JAR files

  • jms.jar
  • jnp-client.jar
  • jboss-common.jar
  • concurrent.jar
  • jbossmq-client.jar


The following code needs to be added in the JBoss configuration xml file for creating the Topic while server
is started.

 <mbean code=""
    <depends optional-attribute-name="DestinationManager"></depends>
    <depends optional-attribute-name="SecurityManager"></depends>
    <attribute name="SecurityConf">
        <role name="guest" read="true" write="true"/>
        <role name="publisher" read="true" write="true" create="false"/>
        <role name="durpublisher" read="true" write="true" create="true"/>



import java.util.Properties;

import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;
import javax.jms.Topic;
import javax.jms.TopicConnection;
import javax.jms.TopicConnectionFactory;
import javax.jms.TopicPublisher;
import javax.jms.TopicSession;
import javax.jms.TopicSubscriber;
import javax.naming.Context;
import javax.naming.InitialContext;

public class JMSExample implements MessageListener {

    public static void main(String argc[]) {
        new JMSExample().testMessage();

    public void testMessage() {
        TopicConnection conn = null;
        TopicSession session = null;
        Topic topic = null;
        try {
            Properties props = new Properties();
            props.setProperty("java.naming.provider.url", "localhost");
            Context context = new InitialContext(props);
            TopicConnectionFactory tcf = (TopicConnectionFactory) context.lookup("ConnectionFactory");
            conn = tcf.createTopicConnection();
            topic = (Topic) context.lookup("topic/testTopic");
            session = conn.createTopicSession(false,
            TopicPublisher send = session.createPublisher(topic);
            TextMessage tm = session.createTextMessage("JavaBeat Test Message");
            TopicSubscriber topicSubscriber = session.createSubscriber(
            while (true) {
                Message message = topicSubscriber.receive(1);
                if (message != null) {
                    if (message instanceof TextMessage) {
                        TextMessage textMessage = (TextMessage) message;
                    } else {

        } catch (Exception e) {
            System.out.println(e.toString() + " Does the queue exist?");

    public void onMessage(Message message) {
        try {
            TextMessage textMessage = (TextMessage) message;
            String text = textMessage.getText();
        } catch (Exception jmse) {

Leave a Reply

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

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=""> <s> <strike> <strong>