1 28 29 32 package newsamplemdb; 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 static TopicConnectionFactory tcf = null; 45 static TopicPublisher tp = null; 46 static Topic topic = null; 47 static String topicName = "mdbTopic"; 49 static String conFactName = "JTCF"; 51 52 public static void main(String[] arg) { 53 try { 55 ictx = new InitialContext(); 56 tcf = (TopicConnectionFactory) ictx.lookup(conFactName ); 58 System.out.println("JMS client: tcf = " + tcf); 59 topic = (Topic) ictx.lookup(topicName); 61 System.out.println("JMS client: topic = " + topic); 62 } catch (NamingException e) { 63 e.printStackTrace(); 64 System.exit(2); 65 } 66 67 TopicConnection tc = null; 68 TopicSession session = null; 69 try { 70 tc = tcf.createTopicConnection(); 71 System.out.println("JMS client: tc = " + tc.toString()); 72 session = tc.createTopicSession(false, Session.AUTO_ACKNOWLEDGE); 73 tp = session.createPublisher(topic); 74 }catch (Exception e) { 75 e.printStackTrace(); 76 System.exit(2); 77 } 78 79 int nbmess = 10; 81 try { 82 TextMessage message; 83 for (int i=0;i<nbmess;i++){ 84 message = session.createTextMessage(); 85 message.setText("Message"+i); 86 tp.publish(message); 87 88 } 89 session.close(); 90 94 } catch (Exception e) { 95 e.printStackTrace(); 96 System.exit(2); 97 } 98 System.out.println("MDBsample is Ok"); 99 } 100 } 101 | Popular Tags |