KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > ubermq > jms > client > test > ReconnectTest


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 JavaDoc;
14 import javax.jms.TopicConnection JavaDoc;
15 import javax.jms.TopicSession JavaDoc;
16
17 /**
18  * A JUnit test case that exercises reconnection. This requires interactive
19  * help to start and kill an external server.
20  */

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 JavaDoc sz) {
31         super(sz);
32     }
33
34     public void xtestReconnect()
35         throws Exception JavaDoc
36     {
37         // ok connect, and set up heartbeating and reconnect
38
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         // create pub'er
46
TopicSession JavaDoc ts2 = c2.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
47         Topic theTopic = ts2.createTopic("theTopic");
48         TopicPublisher tp = ts2.createPublisher(theTopic);
49
50         // send some messages
51
TopicSession JavaDoc 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         // bring down the server.
57
System.out.println("Please stop the server!");
58         Thread.sleep(RECONNECT_SLEEP);
59
60         // now we should fail to deliver
61
// and the publish should be fail-fast.
62
try {
63             // should be reconnected... try same operation again
64
RegressionTestCase.sendExactly(ts2, tp, theTopic, 10);
65             Assert.assertTrue(false);
66         } catch(JMSException jmse) {
67             // yes that's right
68
Assert.assertTrue(true);
69         }
70
71         // ok now restart the server
72
System.out.println("Please start the server!");
73         Thread.sleep(RECONNECT_SLEEP);
74         c.reconnect();
75         c2.reconnect();
76
77         // should be reconnected... try same operation again
78
// now we should get back the messages that we originally couldn't
79
// send, and we should NOT get the messages that we tried to send, above.
80
RegressionTestCase.sendExactly(ts2, tp, theTopic, 10);
81         RegressionTestCase.receiveExactly(tsub, 10);
82
83         // now let's try automatic reconnection
84
c.startHeartbeat(200);
85         c2.startHeartbeat(200);
86
87         // ok bring it down again...
88
System.out.println("Please cycle the server!");
89         Thread.sleep(3 * RECONNECT_SLEEP);
90
91         // now we should be able to send as usual
92
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 JavaDoc
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         // ok, two conn factories
105
TopicConnectionFactory f = new UnicastConnectionFactory("localhost:3999,localhost:4000");
106
107         // connect.
108
TopicConnection JavaDoc c = f.createTopicConnection();
109         TopicSession JavaDoc s = c.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
110         TopicPublisher p = s.createPublisher(s.createTopic("A"));
111
112         // stats for reconnect events.
113
final SynchronizedInt nClosed = new SynchronizedInt(0),
114             nReconnect = new SynchronizedInt(0),
115             nIo = new SynchronizedInt(0);
116
117         // reconnect logic.
118
((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         // ok now fail 3999.
137
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         // our reconnect logic should have recognized the failure, and reconnected
144
// to the 4000 instance, after retrying the 3999 several times.
145
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         // subscribe on 4000
153
TopicConnection JavaDoc tc4000 = new UnicastConnectionFactory("localhost", 4000).createTopicConnection();
154         TopicSession JavaDoc 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         // test the msg infrastructure, now on 4000.
166
RegressionTestCase.sendExactly(s, p, s.createTopic("A"), 15);
167         RegressionTestCase.receiveOrdered(tsub4000, 15);
168
169         // now fail 4000
170
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         // check that we got the events, and that we are back.
177
// we get one closed from the tc4000, and one IO and recon from the original c.
178
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         // ok our connection should be back to 3999 now.
186
TopicConnection JavaDoc tc3999 = f.createTopicConnection();
187         TopicSession JavaDoc ts3999 = tc3999.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
188         TopicSubscriber tsub3999 = ts3999.createSubscriber(ts3999.createTopic("A"));
189         tc3999.start();
190
191         // test the msg infrastructure, now on 3999.
192
RegressionTestCase.sendExactly(s, p, s.createTopic("A"), 15);
193         RegressionTestCase.receiveOrdered(tsub3999, 15);
194
195         // close out 3999
196
tsub3999.close();
197         ts3999.close();
198         tc3999.close();
199
200         // close
201
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