KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > jbossmq > perf > SendReplyPerfStressTestCase


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.perf;
23 import java.util.ArrayList JavaDoc;
24 import java.util.Iterator JavaDoc;
25 import javax.jms.Message JavaDoc;
26 import javax.jms.MessageListener JavaDoc;
27 import javax.jms.QueueConnection JavaDoc;
28 import javax.jms.QueueConnectionFactory JavaDoc;
29 import javax.jms.QueueReceiver JavaDoc;
30 import javax.jms.QueueSender JavaDoc;
31 import javax.jms.QueueSession JavaDoc;
32 import javax.jms.Session JavaDoc;
33 import javax.jms.TemporaryQueue JavaDoc;
34 import javax.jms.Topic JavaDoc;
35 import javax.jms.TopicConnection JavaDoc;
36 import javax.jms.TopicConnectionFactory JavaDoc;
37 import javax.jms.TopicPublisher JavaDoc;
38 import javax.jms.TopicSession JavaDoc;
39 import javax.jms.TopicSubscriber JavaDoc;
40 import javax.jms.Queue JavaDoc;
41 import javax.naming.Context JavaDoc;
42
43 import org.jboss.test.JBossTestCase;
44
45 /**
46  * JBossMQPerfStressTestCase.java Some simple tests of JBossMQ
47  *
48  * @author
49  * @version
50  */

51 public class SendReplyPerfStressTestCase extends JBossTestCase
52 {
53    // Provider specific
54
static String JavaDoc TOPIC_FACTORY = "ConnectionFactory";
55    static String JavaDoc QUEUE_FACTORY = "ConnectionFactory";
56
57    static String JavaDoc TEST_QUEUE = "queue/testQueue";
58    static String JavaDoc TEST_TOPIC = "topic/testTopic";
59
60    static byte[] PERFORMANCE_TEST_DATA_PAYLOAD = new byte[10];
61
62    //JMSProviderAdapter providerAdapter;
63
static Context JavaDoc context;
64    static QueueConnection JavaDoc queueConnection;
65    static TopicConnection JavaDoc topicConnection;
66
67    public SendReplyPerfStressTestCase(String JavaDoc name) throws Exception JavaDoc
68    {
69       super(name);
70    }
71
72    /**
73     * The main entry-point for the JBossMQPerfStressTestCase class
74     *
75     * @param args The command line arguments
76     */

77    public static void main(String JavaDoc[] args)
78    {
79
80       String JavaDoc newArgs[] = {"org.jboss.test.jbossmq.perf.SendReplyPerfStressTestCase"};
81       junit.swingui.TestRunner.main(newArgs);
82    }
83
84    public static class State
85    {
86       public int expected;
87       public int finished = 0;
88       public ArrayList JavaDoc errors = new ArrayList JavaDoc();
89       public State(int expected)
90       {
91          this.expected = expected;
92       }
93       public synchronized void addError(Throwable JavaDoc t)
94       {
95          errors.add(t);
96       }
97       public synchronized void finished()
98       {
99          ++finished;
100          if (finished == expected)
101             notifyAll();
102       }
103       public synchronized void waitForFinish() throws Exception JavaDoc
104       {
105          if (finished == expected)
106             return;
107          wait();
108       }
109    }
110
111    public static class MessageQueueSender
112       implements Runnable JavaDoc
113    {
114       State state;
115       public MessageQueueSender(State state)
116       {
117          this.state = state;
118       }
119
120       public void run()
121       {
122          try
123          {
124             Queue JavaDoc queue = (Queue JavaDoc)context.lookup(TEST_QUEUE);
125             QueueSession JavaDoc session = queueConnection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
126             TemporaryQueue JavaDoc temp = session.createTemporaryQueue();
127             Message JavaDoc message = session.createTextMessage();
128             message.setJMSReplyTo(temp);
129
130             QueueSender JavaDoc sender = session.createSender(queue);
131             sender.send(message);
132
133             QueueReceiver JavaDoc receiver = session.createReceiver(temp);
134             receiver.receive();
135             receiver.close();
136             temp.delete();
137             
138             session.close();
139          }
140          catch (Throwable JavaDoc t)
141          {
142             state.addError(t);
143          }
144          finally
145          {
146             state.finished();
147          }
148       }
149    }
150
151    public static class MessageTopicSender
152       implements Runnable JavaDoc
153    {
154       State state;
155       public MessageTopicSender(State state)
156       {
157          this.state = state;
158       }
159
160       public void run()
161       {
162          try
163          {
164             Topic JavaDoc topic = (Topic JavaDoc)context.lookup(TEST_TOPIC);
165             TopicSession JavaDoc session = topicConnection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
166             Message JavaDoc message = session.createTextMessage();
167
168             QueueSession JavaDoc qsession = queueConnection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
169             TemporaryQueue JavaDoc temp = qsession.createTemporaryQueue();
170             message.setJMSReplyTo(temp);
171
172             TopicPublisher JavaDoc publisher = session.createPublisher(topic);
173             publisher.publish(message);
174
175             QueueReceiver JavaDoc receiver = qsession.createReceiver(temp);
176             receiver.receive();
177             receiver.close();
178             
179             session.close();
180          }
181          catch (Throwable JavaDoc t)
182          {
183             state.addError(t);
184          }
185          finally
186          {
187             state.finished();
188          }
189       }
190    }
191
192    public static class MessageReplier
193       implements MessageListener JavaDoc
194    {
195       State state;
196       public MessageReplier(State state)
197       {
198          this.state = state;
199       }
200       public void onMessage(Message JavaDoc message)
201       {
202          try
203          {
204             QueueSession JavaDoc session = queueConnection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
205             Queue JavaDoc replyQueue = session.createQueue(((Queue JavaDoc)message.getJMSReplyTo()).getQueueName());
206             QueueSender JavaDoc sender = session.createSender(replyQueue);
207             sender.send(message);
208             sender.close();
209             session.close();
210          }
211          catch (Throwable JavaDoc t)
212          {
213             state.addError(t);
214          }
215       }
216    }
217
218    public void testSendReplyQueue() throws Exception JavaDoc
219    {
220       drainQueue();
221
222       // Set up the workers
223
State state = new State(getThreadCount());
224       MessageReplier replier = new MessageReplier(state);
225       Thread JavaDoc[] threads = new Thread JavaDoc[getThreadCount()];
226       for (int i = 0; i < threads.length; ++i)
227           threads[i] = new Thread JavaDoc(new MessageQueueSender(state));
228
229       // Register the message listener
230
Queue JavaDoc queue = (Queue JavaDoc)context.lookup(TEST_QUEUE);
231       QueueSession JavaDoc session = queueConnection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
232       QueueReceiver JavaDoc receiver = session.createReceiver(queue);
233       receiver.setMessageListener(replier);
234       queueConnection.start();
235
236       // Start the senders
237
for (int i = 0; i < threads.length; ++i)
238           threads[i].start();
239
240       // Wait for it to finish
241
state.waitForFinish();
242
243       // Report the result
244
for (Iterator JavaDoc i = state.errors.iterator(); i.hasNext();)
245          getLog().error("Error", (Throwable JavaDoc) i.next());
246       if (state.errors.size() > 0)
247          throw new RuntimeException JavaDoc("Test failed with " + state.errors.size() + " errors");
248    }
249
250    public void testSendReplyTopic() throws Exception JavaDoc
251    {
252       // Set up the workers
253
State state = new State(getThreadCount());
254       MessageReplier replier = new MessageReplier(state);
255
256       Thread JavaDoc[] threads = new Thread JavaDoc[getThreadCount()];
257       for (int i = 0; i < threads.length; ++i)
258           threads[i] = new Thread JavaDoc(new MessageTopicSender(state));
259
260
261       // Register the message listener
262
Topic JavaDoc topic = (Topic JavaDoc)context.lookup(TEST_TOPIC);
263       TopicSession JavaDoc session = topicConnection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
264       TopicSubscriber JavaDoc subscriber = session.createSubscriber(topic);
265       subscriber.setMessageListener(replier);
266       topicConnection.start();
267       queueConnection.start();
268
269       // Start the senders
270
for (int i = 0; i < threads.length; ++i)
271           threads[i].start();
272
273       // Wait for it to finish
274
state.waitForFinish();
275
276       // Report the result
277
for (Iterator JavaDoc i = state.errors.iterator(); i.hasNext();)
278          getLog().error("Error", (Throwable JavaDoc) i.next());
279       if (state.errors.size() > 0)
280          throw new RuntimeException JavaDoc("Test failed with " + state.errors.size() + " errors");
281    }
282
283    protected void setUp() throws Exception JavaDoc
284    {
285       getLog().info("Starting test: " + getName());
286
287       context = getInitialContext();
288
289       QueueConnectionFactory JavaDoc queueFactory = (QueueConnectionFactory JavaDoc)context.lookup(QUEUE_FACTORY);
290       queueConnection = queueFactory.createQueueConnection();
291
292       TopicConnectionFactory JavaDoc topicFactory = (TopicConnectionFactory JavaDoc)context.lookup(TOPIC_FACTORY);
293       topicConnection = topicFactory.createTopicConnection();
294
295       getLog().debug("Connection to JBossMQ established.");
296    }
297
298    protected void tearDown() throws Exception JavaDoc
299    {
300       getLog().info("Ended test: " + getName());
301       queueConnection.close();
302       topicConnection.close();
303    }
304
305    private void drainQueue() throws Exception JavaDoc
306    {
307       QueueSession JavaDoc session = queueConnection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
308       Queue JavaDoc queue = (Queue JavaDoc)context.lookup(TEST_QUEUE);
309
310       QueueReceiver JavaDoc receiver = session.createReceiver(queue);
311       queueConnection.start();
312       Message JavaDoc message = receiver.receive(50);
313       int c = 0;
314       while (message != null)
315       {
316          message = receiver.receive(50);
317          c++;
318       }
319
320       if (c != 0)
321          getLog().debug(" Drained " + c + " messages from the queue");
322       session.close();
323       queueConnection.stop();
324
325    }
326
327    public static junit.framework.Test suite() throws Exception JavaDoc
328    {
329        ClassLoader JavaDoc loader = Thread.currentThread().getContextClassLoader();
330        return getDeploySetup(SendReplyPerfStressTestCase.class,
331                loader.getResource("messaging/test-destinations-service.xml").toString());
332    }
333
334 }
335
Popular Tags