KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > jbossmq > stress > MultipleDurableSubscribers


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.test.jbossmq.stress;
23
24 import javax.jms.JMSException JavaDoc;
25
26 import junit.framework.Assert;
27
28 import org.jboss.test.jbossmq.MQBase;
29
30 /**
31  * According to JMS spec, 6.11.1 to have several durable subscriptions under
32  * one client id. We test for this here.
33  *
34  * @author <a HREF="mailto:pra@tim.se">Peter Antman</a>
35  * @version $Revision: 37406 $
36  */

37
38 public class MultipleDurableSubscribers extends MQBase
39    {
40    class PiggyBackWorker extends TopicWorker
41       {
42       public PiggyBackWorker(TopicWorker worker)
43       {
44          super();
45          connection = worker.connection;
46          destination = worker.destination;
47          session = worker.session;
48       }
49
50       public void connect()
51       {
52          log.debug("In null connect");
53          // Does nothing
54
}
55
56       public void subscribe() throws JMSException JavaDoc
57       {
58          super.subscribe();
59          log.debug("Message consumer set up " + consumer);
60       }
61    }
62
63    public MultipleDurableSubscribers(String JavaDoc name)
64    {
65       super(name);
66    }
67    
68    // This is build the same way as Durable test, to make it possible to take
69
// server down in between.
70
/**
71     * Test setting up a durable subscription. Disconnect after half
72     * the messages have been sent. Connect later to see if they are still there.
73     * This test is done it two parts to be able to take down the server in
74     * between
75     */

76    public void runDurableSubscriberPartOne() throws Exception JavaDoc
77    {
78       try
79       {
80          // Clean testarea up
81
drainTopic();
82
83          int ic = getIterationCount();
84       
85          // Set up a durable subscriber
86
IntRangeMessageFilter f1 = new IntRangeMessageFilter(javax.jms.Message JavaDoc.class,
87             "DURABLE_NR",
88             0,
89             ic / 2);
90
91          TopicWorker sub1 = new TopicWorker(SUBSCRIBER,
92             TRANS_NONE,
93             f1);
94          sub1.setDurable("john", "needle", "sub1");
95          Thread JavaDoc t1 = new Thread JavaDoc(sub1);
96          t1.start();
97
98          log.debug("Sub1 set up");
99          sleep(5000L);
100          TopicWorker sub2 = new PiggyBackWorker(sub1);
101          sub2.setSubscriberAttrs(SUBSCRIBER,
102             TRANS_NONE,
103             f1);
104          sub2.setDurable("john", "needle", "sub2");
105          Thread JavaDoc t2 = new Thread JavaDoc(sub2);
106          t2.start();
107          log.debug("Sub2 setup");
108
109          // Publish
110
IntRangeMessageCreator c1 = new IntRangeMessageCreator("DURABLE_NR",
111             0);
112          TopicWorker pub1 = new TopicWorker(PUBLISHER,
113             TRANS_NONE,
114             c1,
115             ic / 2);
116          pub1.connect();
117          pub1.publish();
118
119          Assert.assertEquals("Publisher did not publish correct number of messages " + pub1.getMessageHandled(),
120             ic / 2,
121             pub1.getMessageHandled());
122       
123          // let sub1 have some time to handle the messages.
124
log.debug("Sleeping for " + ((ic * 10) / 60000) + " minutes");
125          sleep(ic * 10);
126
127
128          Assert.assertEquals("Subscriber1 did not get correct number of messages " + sub1.getMessageHandled(),
129             ic / 2,
130             sub1.getMessageHandled());
131          Assert.assertEquals("Subscriber2 did not get correct number of messages " + sub1.getMessageHandled(),
132             ic / 2,
133             sub2.getMessageHandled());
134       
135          // Take down subs
136
sub1.close();
137          t1.interrupt();
138          sub2.close();
139          t2.interrupt();
140       
141          //Publish some more
142
pub1.publish(ic / 2);
143          Assert.assertEquals("Publisher did not publish correct number of messages " + pub1.getMessageHandled(), ic,
144             pub1.getMessageHandled());
145
146          pub1.close();
147       }
148       catch (Throwable JavaDoc t)
149       {
150          log.error("Error in test: " + t, t);
151          throw new Exception JavaDoc(t.getMessage());
152       }
153    }
154
155    /**
156     * Part two of durable subscriber test, part one should be run before
157     * this is run.
158     */

159    public void runDurableSubscriberPartTwo() throws Exception JavaDoc
160    {
161       try
162       {
163          int ic = getIterationCount();
164          // Set up a durable subscriber
165
IntRangeMessageFilter f1 = new IntRangeMessageFilter(javax.jms.Message JavaDoc.class,
166             "DURABLE_NR",
167             0,
168             ic / 2);
169
170          TopicWorker sub1 = new TopicWorker(SUBSCRIBER,
171             TRANS_NONE,
172             f1);
173          sub1.setDurable("john", "needle", "sub1");
174
175          // Start up subscription again
176
Thread JavaDoc t1 = new Thread JavaDoc(sub1);
177          t1.start();
178          sleep(5000L);
179          TopicWorker sub2 = new PiggyBackWorker(sub1);
180          sub2.setSubscriberAttrs(SUBSCRIBER,
181             TRANS_NONE,
182             f1);
183          sub2.setDurable("john", "needle", "sub2");
184          Thread JavaDoc t2 = new Thread JavaDoc(sub2);
185          t2.start();
186
187          log.debug("Sleeping for " + ((ic * 10) / 60000) + " minutes");
188          sleep(ic * 10);
189          Assert.assertEquals("Subscriber did not get correct number of messages " + sub1.getMessageHandled(), ic / 2,
190             sub1.getMessageHandled());
191          Assert.assertEquals("Subscriber did not get correct number of messages " + sub1.getMessageHandled(), ic / 2,
192             sub2.getMessageHandled());
193
194          //OK, take everything down
195
sub1.unsubscribe();
196          sub2.unsubscribe();
197          sub1.close();
198          t1.interrupt();
199          sub2.close();
200          t2.interrupt();
201       }
202       catch (Throwable JavaDoc t)
203       {
204          log.error("Error in test: " + t, t);
205          throw new Exception JavaDoc(t.getMessage());
206       }
207    }
208
209    public void testDurableSubscriber() throws Exception JavaDoc
210    {
211       runDurableSubscriberPartOne();
212       runDurableSubscriberPartTwo();
213    }
214
215    public static void main(String JavaDoc[] args)
216    {
217
218    }
219
220 } // MultipleDurableSubscribers
221
Popular Tags