Java .

Java JMS With A Topic

A Publisher / Subscriber JMS Topic

The Publisher - QuidditchTopicPublisher.java

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

public class QuidditchTopicPublisher {

public static void main(String[] args) { String quidditchScores[] = {"Hufflepuff 80, Slytherin 300" , "Gryffindor 320, Ravenclaw 155"};

TopicConnectionFactory topicConnectionFactory = null; Topic quidditchTopic = null;

try { Context jndiContext = new InitialContext(); topicConnectionFactory = (TopicConnectionFactory) jndiContext.lookup("TopicConnectionFactory"); quidditchTopic = (Topic) jndiContext.lookup("QuidditchTopic"); } catch (NamingException nameEx) { System.out.println("Naming Exception: " + nameEx.toString());}

TopicConnection topicConnection = null;

try { topicConnection = topicConnectionFactory.createTopicConnection(); TopicSession topicSession = topicConnection.createTopicSession( false,Session.AUTO_ACKNOWLEDGE); TopicPublisher topicPublisher = topicSession.createPublisher(quidditchTopic); TextMessage textMessage = topicSession.createTextMessage(); for (int msgCount = 0; msgCount < quidditchScores.length; msgCount++) { textMessage.setText(quidditchScores[msgCount]); topicPublisher.publish(textMessage); System.out.println(" publishing line " + msgCount + " : " + quidditchScores[msgCount]); }

textMessage.setText("end of message"); topicPublisher.publish(textMessage); System.out.println(" publishing last line " + " : " + textMessage.getText());

topicConnection.close(); System.out.println( " quidditch publisher closed"); } catch (javax.jms.JMSException jmsEx) { System.out.println("JMS Exception: " + jmsEx.toString()); } finally { if (topicConnection != null) { try { topicConnection.close(); } catch (javax.jms.JMSException jmse) {} } } } }
To download source right-click here and "Save As...".

The Subscriber - WeasleyQuidditchTopicSubscriber.java

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

public class WeasleyQuidditchTopicSubscriber implements MessageListener {

TopicConnection topicConnection = null; boolean subscriptionOn; public static void main(String[] args) { WeasleyQuidditchTopicSubscriber wqts = new WeasleyQuidditchTopicSubscriber(); try { while (wqts.subscriptionOn) {Thread.sleep(500);} //onMessage() waits for message here } catch (java.lang.InterruptedException intExc) {} try { wqts.topicConnection.close(); } catch (javax.jms.JMSException jmsEx) { System.out.println("JMS Exception: " + jmsEx.toString()); } System.out.println(" Weasley quidditch subscriber closed"); } public WeasleyQuidditchTopicSubscriber() { TopicConnectionFactory topicConnectionFactory = null; Topic quidditchTopic = null; this.subscriptionOn = true; try { Context jndiContext = new InitialContext(); topicConnectionFactory = (TopicConnectionFactory) jndiContext.lookup("TopicConnectionFactory"); quidditchTopic = (Topic) jndiContext.lookup("QuidditchTopic"); } catch (NamingException nameEx) { System.out.println("Naming Exception: " + nameEx.toString()); }

try { topicConnection = topicConnectionFactory.createTopicConnection(); TopicSession topicSession = topicConnection.createTopicSession( false, Session.AUTO_ACKNOWLEDGE); TopicSubscriber topicSubscriber = topicSession.createSubscriber(quidditchTopic); topicSubscriber.setMessageListener(this); //will use the onMessage() method below topicConnection.start(); } catch (javax.jms.JMSException jmsEx) { System.out.println("JMS Exception: " + jmsEx.toString()); } }

public void onMessage(Message message) { try { TextMessage textMessage = (TextMessage)message; System.out.println(" receiving line " + " : " + textMessage.getText()); if (textMessage.getText().equals("end of message")) { this.subscriptionOn = false; } } catch (javax.jms.JMSException jmsEx) { System.out.println("JMS Exception in onMessage: " + jmsEx.toString()); } } }
To download source right-click here and "Save As...".

Session 1, Create the QuidditchTopic topic

                          
j2eeadmin 
  -addJmsDestination QuidditchTopic topic                                

Session 2, Run WeasleyQuidditchTopicSubscriber

                          
java 
  -Djms.properties=%J2EE_HOME%\config\jms_client.properties
  WeasleyQuidditchTopicSubscriber
Java(TM) Message Service 1.0.2 Reference Implementation (build b14)

Session 3, Run QuidditchTopicPublisher

                          
java 
  -Djms.properties=%J2EE_HOME%\config\jms_client.properties 
  QuidditchTopicPublisher
Java(TM) Message Service 1.0.2 Reference Implementation (build b14)
 publishing line 0 : Hufflepuff 80, Slytherin 300
 publishing line 1 : Gryffindor 320, Ravenclaw 155
 publishing last line  : end of message
  quidditch publisher closed

... and back in Session 2:

                          
 receiving line  : Hufflepuff 80, Slytherin 300
 receiving line  : Gryffindor 320, Ravenclaw 155
 receiving line  : end of message
  Weasley quidditch subscriber closed

References


online
Java Message Service Tutorial from Sun
Books
Enterprise Java Beans by Richard Monson-Haefel
Mastering Enterprise JavaBeans by Ed Roman
Harry Potter by J.K.Rowling
Comments
Sign in to be the first to comment on Java JMS With A Topic.