KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > jbossmessaging > 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.jbossmessaging.perf;
23
24 import java.util.ArrayList JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import javax.jms.Message JavaDoc;
27 import javax.jms.MessageListener JavaDoc;
28 import javax.jms.QueueConnection JavaDoc;
29 import javax.jms.QueueConnectionFactory JavaDoc;
30 import javax.jms.QueueReceiver JavaDoc;
31 import javax.jms.QueueSender JavaDoc;
32 import javax.jms.QueueSession JavaDoc;
33 import javax.jms.Session JavaDoc;
34 import javax.jms.TemporaryQueue JavaDoc;
35 import javax.jms.Topic JavaDoc;
36 import javax.jms.TopicConnection JavaDoc;
37 import javax.jms.TopicConnectionFactory JavaDoc;
38 import javax.jms.TopicPublisher JavaDoc;
39 import javax.jms.TopicSession JavaDoc;
40 import javax.jms.TopicSubscriber JavaDoc;
41 import javax.jms.Queue JavaDoc;
42 import javax.naming.Context JavaDoc;
43
44 import org.jboss.test.jbossmessaging.JMSTestCase;
45
46 /**
47  * SendReplyPerfStressTestCase.java
48  * Some send/reply performance tests
49  *
50  * @author <a HREF="mailto:richard.achmatowicz@jboss.com">Richard Achmatowicz</a>
51  * @author
52  * @version
53  */

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

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