1 package com.ubermq.jms.client.test; 2 3 import javax.jms.*; 4 import com.ubermq.jms.client.UnicastConnectionFactory; 5 6 10 public class DurableTest 11 { 12 private static final org.apache.log4j.Logger log = org.apache.log4j.Logger.getLogger(DurableTest.class); 13 14 17 public static void main(String [] args) 18 { 19 String function = args[1]; 20 int nmsg = Integer.valueOf(args[2]).intValue(); 21 String sz = args[3]; 22 String name = args[4]; 23 24 try 25 { 26 if (function.equalsIgnoreCase("pub")) 27 { 28 TopicConnectionFactory tcf = new UnicastConnectionFactory(args[0]); 29 TopicConnection tc = tcf.createTopicConnection(); 30 TopicSession ts = tc.createTopicSession(true, TopicSession.AUTO_ACKNOWLEDGE); 31 TopicPublisher tp = ts.createPublisher(ts.createTopic(sz)); 32 33 for (int i = 0; i < nmsg; i++) 34 { 35 Message m = ts.createMessage(); 36 m.setIntProperty("num", i); 37 tp.publish(m); 38 39 System.out.println("sent " + i); 40 } 41 42 ts.close(); 43 tc.stop(); 44 tc.close(); 45 46 } else if (function.equalsIgnoreCase("sub-forever")) { 47 TopicConnectionFactory tcf = new UnicastConnectionFactory(args[0]); 48 TopicConnection tc = tcf.createTopicConnection(); 49 TopicSession ts = tc.createTopicSession(true, TopicSession.CLIENT_ACKNOWLEDGE); 50 TopicSubscriber sub = ts.createDurableSubscriber(ts.createTopic(sz), name); 51 52 while(true) 53 { 54 Message m = sub.receive(); 55 m.acknowledge(); 56 System.out.print("."); 57 } 58 } 59 else if (function.equalsIgnoreCase("sub")) { 60 61 TopicConnectionFactory tcf = new UnicastConnectionFactory(args[0]); 62 TopicConnection tc = tcf.createTopicConnection(); 63 TopicSession ts = tc.createTopicSession(true, TopicSession.CLIENT_ACKNOWLEDGE); 64 TopicSubscriber sub = ts.createDurableSubscriber(ts.createTopic(sz), name); 65 66 for (int i = 0; i < nmsg; i++) 67 { 68 Message m = sub.receive(); 69 System.out.println(m.getIntProperty("num")); 70 m.acknowledge(); 71 } 72 73 ts.close(); 74 tc.close(); 75 } else if (function.equalsIgnoreCase("delete")) { 76 TopicConnectionFactory tcf = new UnicastConnectionFactory(args[0]); 77 TopicConnection tc = tcf.createTopicConnection(); 78 TopicSession ts = tc.createTopicSession(true, TopicSession.AUTO_ACKNOWLEDGE); 79 ts.unsubscribe(name); 80 ts.close(); 81 tc.close(); 82 } 83 } 84 catch (Exception e) {log.error("", e);;} 85 86 } 87 } 88 | Popular Tags |