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) {} } } } }