1 28 29 package samplemdb; 32 33 import javax.naming.Context; 34 import javax.naming.InitialContext; 35 import javax.naming.*; 36 import javax.jms.*; 37 38 41 public class MdbClient { 42 43 static Context ictx = null; 44 45 static TopicConnectionFactory tcf = null; 46 47 static TopicPublisher tp = null; 48 49 static Topic topic = null; 50 51 static String topicName = "mdbTopic"; 53 54 static String conFactName = "JTCF"; 56 57 public static void main(String[] arg) { 58 try { 60 ictx = new InitialContext(); 61 tcf = (TopicConnectionFactory) ictx.lookup(conFactName); 63 System.out.println("JMS client: tcf = " + tcf.toString()); 64 topic = (Topic) ictx.lookup(topicName); 66 } catch (NamingException e) { 67 e.printStackTrace(); 68 System.exit(2); 69 } 70 71 TopicConnection tc = null; 72 TopicSession session = null; 73 try { 74 tc = tcf.createTopicConnection(); 75 System.out.println("JMS client: tc = " + tc.toString()); 76 session = tc.createTopicSession(false, Session.AUTO_ACKNOWLEDGE); 77 tp = session.createPublisher(topic); 78 } catch (Exception e) { 79 e.printStackTrace(); 80 System.exit(2); 81 } 82 83 int nbmess = 10; 85 try { 86 TextMessage message; 87 for (int i = 0; i < nbmess; i++) { 88 message = session.createTextMessage(); 89 message.setText("Message" + i); 90 tp.publish(message); 91 92 } 93 session.close(); 94 98 } catch (Exception e) { 99 e.printStackTrace(); 100 System.exit(2); 101 } 102 System.out.println("MDBsample is Ok"); 103 } 104 } | Popular Tags |