| 1 package com.ubermq.jms.client.test; 2 3 import EDU.oswego.cs.dl.util.concurrent.*; 4 import com.ubermq.jms.client.*; 5 6 import com.ubermq.jms.client.unicast.*; 7 import com.ubermq.kernel.event.*; 8 import com.ubermq.kernel.overflow.*; 9 import java.io.*; 10 import javax.jms.*; 11 import junit.framework.*; 12 13 import javax.jms.Session ; 14 import javax.jms.TopicConnection ; 15 import javax.jms.TopicSession ; 16 17 21 public class ReconnectTest 22 extends TestCase 23 { 24 private static final long RECONNECT_SLEEP = 10000; 25 26 public static TestSuite suite() { 27 return new TestSuite(ReconnectTest.class); 28 } 29 30 public ReconnectTest(String sz) { 31 super(sz); 32 } 33 34 public void xtestReconnect() 35 throws Exception  36 { 37 TopicConnectionFactory f = new UnicastConnectionFactory("localhost"); 39 UnicastConnection c = (UnicastConnection)f.createTopicConnection(); 40 c.start(); 41 42 UnicastConnection c2 = (UnicastConnection)f.createTopicConnection(); 43 c2.start(); 44 45 TopicSession ts2 = c2.createTopicSession(false, Session.AUTO_ACKNOWLEDGE); 47 Topic theTopic = ts2.createTopic("theTopic"); 48 TopicPublisher tp = ts2.createPublisher(theTopic); 49 50 TopicSession ts = c.createTopicSession(false, Session.AUTO_ACKNOWLEDGE); 52 TopicSubscriber tsub = ts.createSubscriber(theTopic); 53 RegressionTestCase.sendExactly(ts2, tp, theTopic, 10); 54 RegressionTestCase.receiveExactly(tsub, 10); 55 56 System.out.println("Please stop the server!"); 58 Thread.sleep(RECONNECT_SLEEP); 59 60 try { 63 RegressionTestCase.sendExactly(ts2, tp, theTopic, 10); 65 Assert.assertTrue(false); 66 } catch(JMSException jmse) { 67 Assert.assertTrue(true); 69 } 70 71 System.out.println("Please start the server!"); 73 Thread.sleep(RECONNECT_SLEEP); 74 c.reconnect(); 75 c2.reconnect(); 76 77 RegressionTestCase.sendExactly(ts2, tp, theTopic, 10); 81 RegressionTestCase.receiveExactly(tsub, 10); 82 83 c.startHeartbeat(200); 85 c2.startHeartbeat(200); 86 87 System.out.println("Please cycle the server!"); 89 Thread.sleep(3 * RECONNECT_SLEEP); 90 91 RegressionTestCase.sendExactly(ts2, tp, theTopic, 10); 93 System.out.println("just published."); 94 RegressionTestCase.receiveExactly(tsub, 10); 95 } 96 97 public void testMultipleReconnect() 98 throws Exception  99 { 100 System.out.println("Multiple Reconnect Test"); 101 System.out.println("======================="); 102 System.out.println("Please ensure there are two servers, one running at 3999 and one at 4000."); 103 104 TopicConnectionFactory f = new UnicastConnectionFactory("localhost:3999,localhost:4000"); 106 107 TopicConnection c = f.createTopicConnection(); 109 TopicSession s = c.createTopicSession(false, Session.AUTO_ACKNOWLEDGE); 110 TopicPublisher p = s.createPublisher(s.createTopic("A")); 111 112 final SynchronizedInt nClosed = new SynchronizedInt(0), 114 nReconnect = new SynchronizedInt(0), 115 nIo = new SynchronizedInt(0); 116 117 ((UnicastConnection)c).addEventListener(new ConnectionEventListener() { 119 public void connectionEvent(ConnectionEvent e) 120 { 121 switch(e.getEventCode()) { 122 case ConnectionEvent.CONNECTION_CLOSED: 123 nClosed.increment(); 124 break; 125 case ConnectionEvent.CONNECTION_RECONNECTED: 126 nReconnect.increment(); 127 break; 128 case ConnectionEvent.CONNECTION_IO_EXCEPTION: 129 nIo.increment(); 130 break; 131 } 132 } 133 }); 134 ((UnicastConnection)c).startHeartbeat(500); 135 136 Assert.assertEquals(0, nClosed.get()); 138 Assert.assertEquals(0, nReconnect.get()); 139 Assert.assertEquals(0, nIo.get()); 140 System.out.println("Please bring down the 3999 server."); 141 Thread.sleep(RECONNECT_SLEEP); 142 143 Assert.assertEquals(0, nClosed.get()); 146 Assert.assertEquals(1, nIo.get()); 147 Assert.assertEquals(1, nReconnect.get()); 148 nClosed.set(0); 149 nIo.set(0); 150 nReconnect.set(0); 151 152 TopicConnection tc4000 = new UnicastConnectionFactory("localhost", 4000).createTopicConnection(); 154 TopicSession ts4000 = tc4000.createTopicSession(false, Session.AUTO_ACKNOWLEDGE); 155 TopicSubscriber tsub4000 = ts4000.createSubscriber(ts4000.createTopic("A")); 156 tc4000.start(); 157 ((UnicastConnection)tc4000).addEventListener(new ConnectionEventListener() { 158 public void connectionEvent(ConnectionEvent e) 159 { 160 if (e.getEventCode() == ConnectionEvent.CONNECTION_CLOSED) 161 nClosed.increment(); 162 } 163 }); 164 165 RegressionTestCase.sendExactly(s, p, s.createTopic("A"), 15); 167 RegressionTestCase.receiveOrdered(tsub4000, 15); 168 169 tsub4000.close(); 171 ts4000.close(); 172 tc4000.close(); 173 System.out.println("Please bring up the 3999 server, and kill the 4000 server."); 174 Thread.sleep(3 * RECONNECT_SLEEP); 175 176 Assert.assertEquals(1, nClosed.get()); 179 Assert.assertEquals(1, nIo.get()); 180 Assert.assertEquals(1, nReconnect.get()); 181 nClosed.set(0); 182 nIo.set(0); 183 nReconnect.set(0); 184 185 TopicConnection tc3999 = f.createTopicConnection(); 187 TopicSession ts3999 = tc3999.createTopicSession(false, Session.AUTO_ACKNOWLEDGE); 188 TopicSubscriber tsub3999 = ts3999.createSubscriber(ts3999.createTopic("A")); 189 tc3999.start(); 190 191 RegressionTestCase.sendExactly(s, p, s.createTopic("A"), 15); 193 RegressionTestCase.receiveOrdered(tsub3999, 15); 194 195 tsub3999.close(); 197 ts3999.close(); 198 tc3999.close(); 199 200 p.close(); 202 s.close(); 203 c.close(); 204 Assert.assertEquals(1, nClosed.get()); 205 Assert.assertEquals(0, nIo.get()); 206 Assert.assertEquals(0, nReconnect.get()); 207 } 208 } 209 | Popular Tags |