Publish and Subscribe messages using JMS Topic in JBoss Server

July 10, 2008

JBoss, JMS

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.

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.

1
2
3
4
5
6
7
8
9
10
11
12
 <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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
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();
        }
    }
}
email

Comments

comments

  • http://khpartlastic.com/topusers.php kplastic

    Generally I don’t learn post on blogs, but I wish to say that this write-up very compelled me to take a look at and do it! Your writing taste has been amazed me. Thank you, very great post.

  • suthukrish

    Thanks you for the comments!!!