online
Java Message Service Tutorial from SunBooks
Enterprise Java Beans by Richard Monson-HaefelMastering Enterprise JavaBeans by Ed Roman
Harry Potter by J.K.Rowling
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) {} } } } }
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()); } } }
j2eeadmin -addJmsDestination QuidditchTopic topic
java -Djms.properties=%J2EE_HOME%\config\jms_client.properties WeasleyQuidditchTopicSubscriber Java(TM) Message Service 1.0.2 Reference Implementation (build b14)
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
receiving line : Hufflepuff 80, Slytherin 300 receiving line : Gryffindor 320, Ravenclaw 155 receiving line : end of message Weasley quidditch subscriber closed
| Comments |
| Sign In |
| to add the first comment for Java JMS With A Topic. |