1 24 25 package org.objectweb.jtests.jms.framework; 26 27 import org.objectweb.jtests.jms.admin.*; 28 29 import junit.framework.*; 30 import javax.naming.*; 31 import javax.jms.*; 32 33 46 public class PubSubTestCase extends JMSTestCase { 47 48 private Admin admin; 49 private InitialContext ctx; 50 private static final String TCF_NAME = "testTCF"; 51 private static final String TOPIC_NAME = "testTopic"; 52 53 56 protected Topic publisherTopic; 57 58 61 protected TopicPublisher publisher; 62 63 66 protected TopicConnectionFactory publisherTCF; 67 68 71 protected TopicConnection publisherConnection; 72 73 76 protected TopicSession publisherSession; 77 78 81 protected Topic subscriberTopic; 82 83 86 protected TopicSubscriber subscriber; 87 88 91 protected TopicConnectionFactory subscriberTCF; 92 93 96 protected TopicConnection subscriberConnection; 97 98 101 protected TopicSession subscriberSession; 102 103 104 109 protected void setUp() { 110 try { 111 admin = AdminFactory.getAdmin(); 114 admin.createTopicConnectionFactory(TCF_NAME); 116 admin.createTopic(TOPIC_NAME); 117 118 ctx = admin.createInitialContext(); 120 121 publisherTCF = (TopicConnectionFactory)ctx.lookup(TCF_NAME); 122 publisherTopic = (Topic)ctx.lookup(TOPIC_NAME); 123 publisherConnection = publisherTCF.createTopicConnection(); 124 if (publisherConnection.getClientID() == null) { 125 publisherConnection.setClientID("publisherConnection"); 126 } 127 publisherSession = publisherConnection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE); 128 publisher = publisherSession.createPublisher(publisherTopic); 129 130 subscriberTCF = (TopicConnectionFactory)ctx.lookup(TCF_NAME); 131 subscriberTopic = (Topic)ctx.lookup(TOPIC_NAME); 132 subscriberConnection = subscriberTCF.createTopicConnection(); 133 if (subscriberConnection.getClientID() == null) { 134 subscriberConnection.setClientID("subscriberConnection"); 135 } 136 subscriberSession = subscriberConnection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE); 137 subscriber = subscriberSession.createSubscriber(subscriberTopic); 138 139 publisherConnection.start(); 140 subscriberConnection.start(); 141 } catch (Exception e) { 143 e.printStackTrace(); 145 } 146 } 147 148 151 protected void tearDown() { 152 try { 153 publisherConnection.close(); 154 subscriberConnection.close(); 155 156 admin.deleteTopicConnectionFactory(TCF_NAME); 157 admin.deleteTopic(TOPIC_NAME); 158 159 ctx.close(); 160 } catch (Exception e) { 161 e.printStackTrace(); 163 } finally { 164 publisherTopic = null; 165 publisher = null; 166 publisherTCF = null; 167 publisherSession = null; 168 publisherConnection = null; 169 170 subscriberTopic = null; 171 subscriber = null; 172 subscriberTCF = null; 173 subscriberSession = null; 174 subscriberConnection = null; 175 } 176 } 177 178 public PubSubTestCase (String name) { 179 super(name); 180 } 181 } 182 | Popular Tags |