KickJava   Java API By Example, From Geeks To Geeks.

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


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
24 import javax.jms.DeliveryMode JavaDoc;
25 import javax.jms.Message JavaDoc;
26 import javax.jms.Queue 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.TopicConnection JavaDoc;
34 import javax.jms.TopicConnectionFactory JavaDoc;
35 import javax.management.ObjectName JavaDoc;
36 import javax.naming.Context JavaDoc;
37 import javax.naming.InitialContext JavaDoc;
38
39 import org.jboss.logging.Logger;
40 import org.jboss.test.JBossTestCase;
41
42 import EDU.oswego.cs.dl.util.concurrent.Semaphore;
43
44 /**
45  * JBossMQPerfStressTestCase.java Some simple tests of JBossMQ
46  *
47  * @author <a HREF="mailto:hiram.chirino@jboss.org">Hiram Chirino</a>
48  * @version $Revision: 58115 $
49  */

50
51 public class InvocationLayerStressTest extends JBossTestCase
52 {
53    Context JavaDoc context;
54    QueueConnection JavaDoc queueConnection;
55    TopicConnection JavaDoc topicConnection;
56    static final int WORKER_COUNT = Integer.parseInt(System.getProperty("jbosstest.threadcount", "10"));
57    static final int MESSAGE_COUNT = Integer.parseInt(System.getProperty("jbosstest.iterationcount", "500"));
58    Semaphore exitSemaphore;
59
60    /**
61     * Constructor for the JBossMQPerfStressTestCase object
62     *
63     * @param name Description of Parameter
64     * @exception Exception Description of Exception
65     */

66    public InvocationLayerStressTest(String JavaDoc name) throws Exception JavaDoc
67    {
68       super(name);
69    }
70
71
72    public void createQueue(String JavaDoc name)
73    {
74       try
75       {
76          ObjectName JavaDoc objn = new ObjectName JavaDoc("jboss.mq:service=DestinationManager");
77          getServer().invoke(objn, "createQueue", new Object JavaDoc[]{name, name}, new String JavaDoc[]{String JavaDoc.class.getName(), String JavaDoc.class.getName()});
78       }
79       catch (Exception JavaDoc e)
80       {
81          e.printStackTrace();
82       }
83    }
84
85    public void createTopic(String JavaDoc name)
86    {
87       try
88       {
89          ObjectName JavaDoc objn = new ObjectName JavaDoc("jboss.mq:service=DestinationManager");
90          getServer().invoke(objn, "createTopic", new Object JavaDoc[]{name, name}, new String JavaDoc[]{String JavaDoc.class.getName(), String JavaDoc.class.getName()});
91       }
92       catch (Exception JavaDoc e)
93       {
94          e.printStackTrace();
95       }
96    }
97
98    public void deleteQueue(String JavaDoc name)
99    {
100       try
101       {
102          ObjectName JavaDoc objn = new ObjectName JavaDoc("jboss.mq:service=DestinationManager");
103          getServer().invoke(objn, "destroyQueue", new Object JavaDoc[]{name}, new String JavaDoc[]{String JavaDoc.class.getName()});
104       }
105       catch (Exception JavaDoc e)
106       {
107          e.printStackTrace();
108       }
109    }
110
111    public void deleteTopic(String JavaDoc name)
112    {
113       try
114       {
115          ObjectName JavaDoc objn = new ObjectName JavaDoc("jboss.mq:service=DestinationManager");
116          getServer().invoke(objn, "destroyTopic", new Object JavaDoc[]{name}, new String JavaDoc[]{String JavaDoc.class.getName()});
117       }
118       catch (Exception JavaDoc e)
119       {
120          e.printStackTrace();
121       }
122    }
123
124    protected void connect(String JavaDoc queueLoc, String JavaDoc topicLoc) throws Exception JavaDoc
125    {
126       context = new InitialContext JavaDoc();
127       QueueConnectionFactory JavaDoc queueFactory = (QueueConnectionFactory JavaDoc) context.lookup(queueLoc);
128       queueConnection = queueFactory.createQueueConnection();
129
130       TopicConnectionFactory JavaDoc topicFactory = (TopicConnectionFactory JavaDoc) context.lookup(topicLoc);
131       topicConnection = topicFactory.createTopicConnection();
132    }
133
134    protected void disconnect() throws Exception JavaDoc
135    {
136       queueConnection.close();
137       topicConnection.close();
138    }
139
140
141    class QueueWorker extends Thread JavaDoc
142    {
143       String JavaDoc queueName;
144       Throwable JavaDoc exception;
145       Object JavaDoc signal = new Object JavaDoc();
146       Logger log = Logger.getLogger(QueueWorker.class);
147
148       QueueWorker(String JavaDoc queueName, String JavaDoc ilType)
149       {
150          super(queueName);
151          this.queueName = queueName;
152          this.log = Logger.getLogger("QueueWorker."+queueName+"."+ilType);
153       }
154
155       public void run()
156       {
157          log.info("QueueWorker Running: " + queueName);
158
159          try
160          {
161             work();
162          }
163          catch (Throwable JavaDoc e)
164          {
165             exception = e;
166             log.error("Exception:", e);
167          }
168
169          // Signal the main thread that we are done.
170
log.debug("Notifying main thread: ");
171          exitSemaphore.release();
172
173          log.info("QueueWorker Done: " + queueName);
174       }
175
176       void work() throws Exception JavaDoc
177       {
178          createQueue(queueName);
179          QueueSession JavaDoc session = queueConnection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
180          Queue JavaDoc queue = (Queue JavaDoc) context.lookup(queueName);
181
182          // Send the messages
183
QueueSender JavaDoc sender = session.createSender(queue);
184          sender.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
185          Message JavaDoc message = session.createTextMessage("Test Message");
186          for (int i = 0; i < MESSAGE_COUNT; i++)
187          {
188             sender.send(message);
189             log.debug("Sent message " + i + " to queue :" + queueName);
190          }
191
192          // Receive the messages
193
QueueReceiver JavaDoc receiver = session.createReceiver(queue);
194          for (int i = 0; i < MESSAGE_COUNT; i++)
195          {
196             message = receiver.receive(5000);
197             log.debug("Received message " + i + " from queue :" + queueName);
198             if( message == null )
199                fail("Received of msg timedout");
200          }
201          session.close();
202          deleteQueue(queueName);
203       }
204
205    }
206 }
207
Popular Tags