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