1 24 package classic; 25 26 import javax.jms.*; 27 import javax.naming.*; 28 29 32 public class Publisher 33 { 34 static Context ictx = null; 35 36 public static void main(String [] args) throws Exception 37 { 38 System.out.println(); 39 System.out.println("Publishes messages..."); 40 41 ictx = new InitialContext(); 42 43 Topic topic = (Topic) ictx.lookup("topic"); 44 TopicConnectionFactory tcf = (TopicConnectionFactory) ictx.lookup("tcf"); 45 46 ictx.close(); 47 48 TopicConnection tc = tcf.createTopicConnection(); 49 TopicSession ts = tc.createTopicSession(true, 0); 50 TopicPublisher tpub = ts.createPublisher(topic); 51 TextMessage msg = ts.createTextMessage(); 52 53 int i; 54 for (i = 0; i < 10; i++) { 55 msg.setText("Test number " + i); 56 tpub.publish(msg); 57 } 58 59 ts.commit(); 60 61 System.out.println(i + " messages published."); 62 63 tc.close(); 64 } 65 } 66 | Popular Tags |