• Menu
  • Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

JavaBeat

Java Tutorial Blog

  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)
  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)

Publish and Subscribe messages using JMS Topic in JBoss Server

July 10, 2008 //  by Krishna Srinivasan//  Leave a Comment

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:

  • Java EE Tutorials
  • Java EE 7 Tutorials
  • Java EE Interview Questions

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
  • JMSExample.java

jbossmq-destinations-service.xml

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

 <mbean code="org.jboss.mq.server.jmx.Topic"
     name="jboss.mq.destination:service=Topic,name=testTopic">
    <depends optional-attribute-name="DestinationManager">jboss.mq:service=DestinationManager</depends>
    <depends optional-attribute-name="SecurityManager">jboss.mq:service=SecurityManager</depends>
    <attribute name="SecurityConf">
      <security>
        <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"/>
      </security>
    </attribute>
  </mbean> 

jbossmq-destinations-service.xml

package javabeat.net.jms;

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.factory.initial",
                    "org.jnp.interfaces.NamingContextFactory");
            props.setProperty("java.naming.factory.url.pkgs",
                    "org.jboss.naming");
            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,
                    TopicSession.AUTO_ACKNOWLEDGE);
            conn.start();
            TopicPublisher send = session.createPublisher(topic);
            TextMessage tm = session.createTextMessage("JavaBeat Test Message");
            send.publish(tm);
            send.close();
            TopicSubscriber topicSubscriber = session.createSubscriber(
                    topic);
            topicSubscriber.setMessageListener(this);
            while (true) {
                Message message = topicSubscriber.receive(1);
                if (message != null) {
                    if (message instanceof TextMessage) {
                        TextMessage textMessage = (TextMessage) message;
                        System.out.println(message);
                        System.out.println(
                                textMessage.getText());
                    } else {
                        break;
                    }
                }
            }

        } 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();
            System.out.println(text);
        } catch (Exception jmse) {
            jmse.printStackTrace();
        }
    }
}

Category: Java EETag: JBoss, JMS

About Krishna Srinivasan

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.

Previous Post: « How to use h:selectOneRadio inside h:dataTable in JSF?
Next Post: Lists and Maps as Managed Beans in JSF »

Reader Interactions

Leave a Reply Cancel reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

Follow Us

  • Facebook
  • Pinterest

FEATURED TUTORIALS

New Features in Spring Boot 1.4

Difference Between @RequestParam and @PathVariable in Spring MVC

What is new in Java 6.0 Collections API?

The Java 6.0 Compiler API

Introductiion to Jakarta Struts

What’s new in Struts 2.0? – Struts 2.0 Framework

JavaBeat

Copyright © by JavaBeat · All rights reserved
Privacy Policy | Contact