KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > jmeter > threads > ThreadGroup


1 // $Header: /home/cvs/jakarta-jmeter/src/core/org/apache/jmeter/threads/ThreadGroup.java,v 1.24 2004/02/18 23:53:33 sebb Exp $
2
/*
3  * Copyright 2001-2004 The Apache Software Foundation.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17 */

18
19 package org.apache.jmeter.threads;
20
21 import java.io.Serializable JavaDoc;
22 import java.util.Collections JavaDoc;
23 import java.util.Iterator JavaDoc;
24 import java.util.LinkedList JavaDoc;
25 import java.util.List JavaDoc;
26
27 import org.apache.jmeter.control.Controller;
28 import org.apache.jmeter.control.LoopController;
29 import org.apache.jmeter.engine.event.LoopIterationListener;
30 import org.apache.jmeter.samplers.RemoteSampleListener;
31 import org.apache.jmeter.samplers.SampleEvent;
32 import org.apache.jmeter.samplers.SampleListener;
33 import org.apache.jmeter.samplers.Sampler;
34 import org.apache.jmeter.testelement.AbstractTestElement;
35 import org.apache.jmeter.testelement.TestElement;
36 import org.apache.jmeter.testelement.property.IntegerProperty;
37 import org.apache.jmeter.testelement.property.LongProperty;
38 import org.apache.jmeter.testelement.property.BooleanProperty;
39 import org.apache.jmeter.testelement.property.TestElementProperty;
40 import org.apache.jorphan.logging.LoggingManager;
41 import org.apache.log.Logger;
42
43 /**
44  * ThreadGroup
45  *
46  * @author Michael Stover
47  * @version $Id: ThreadGroup.java,v 1.24 2004/02/18 23:53:33 sebb Exp $
48  */

49 public class ThreadGroup
50     extends AbstractTestElement
51     implements SampleListener, Serializable JavaDoc, Controller
52 {
53     private static Logger log = LoggingManager.getLoggerForClass();
54
55     public final static String JavaDoc NUM_THREADS = "ThreadGroup.num_threads";
56     public final static String JavaDoc RAMP_TIME = "ThreadGroup.ramp_time";
57     public final static String JavaDoc MAIN_CONTROLLER = "ThreadGroup.main_controller";
58
59     public final static String JavaDoc SCHEDULER = "ThreadGroup.scheduler";
60     public final static String JavaDoc START_TIME= "ThreadGroup.start_time";
61     public final static String JavaDoc END_TIME= "ThreadGroup.end_time";
62     public final static String JavaDoc DURATION = "ThreadGroup.duration";
63     public final static String JavaDoc DELAY = "ThreadGroup.delay";
64
65
66     /* Action to be taken when a Sampler error occurs*/
67     public final static String JavaDoc ON_SAMPLE_ERROR= "ThreadGroup.on_sample_error"; //int
68
public final static String JavaDoc ON_SAMPLE_ERROR_CONTINUE = "continue";
69     public final static String JavaDoc ON_SAMPLE_ERROR_STOPTHREAD = "stopthread";
70     public final static String JavaDoc ON_SAMPLE_ERROR_STOPTEST = "stoptest";
71
72     private final static int DEFAULT_NUM_THREADS = 1;
73     private final static int DEFAULT_RAMP_UP = 0;
74     private SampleQueue queue = null;
75     private LinkedList JavaDoc listeners = new LinkedList JavaDoc();
76     private LinkedList JavaDoc remoteListeners = new LinkedList JavaDoc();
77
78     /**
79      * No-arg constructor.
80      */

81     public ThreadGroup()
82     {
83     }
84
85     /**
86      * Set the nuber of threads.
87      *
88      * @param numThreads the number of threads.
89      */

90     public void setNumThreads(int numThreads)
91     {
92         setProperty(new IntegerProperty(NUM_THREADS, numThreads));
93     }
94
95     public boolean isDone()
96     {
97         return getSamplerController().isDone();
98     }
99
100     public Sampler next()
101     {
102         return getSamplerController().next();
103     }
104
105
106
107     /**
108      * Set the Scheduler value.
109      *
110      * @param Scheduler the Scheduler value.
111      */

112     public void setScheduler(boolean Scheduler)
113     {
114         setProperty(new BooleanProperty(SCHEDULER,Scheduler));
115     }
116
117     /**
118      * Get the Scheduler value.
119      *
120      * @return the Scheduler value.
121      */

122     public boolean getScheduler()
123     {
124         return getPropertyAsBoolean(SCHEDULER);
125     }
126
127     /**
128      * Set the StartTime value.
129      *
130      * @param stime - the StartTime value.
131      */

132     public void setStartTime(long stime)
133     {
134         setProperty(new LongProperty(START_TIME,stime));
135     }
136
137     /**
138      * Get the start time value.
139      *
140      * @return the start time value.
141      */

142     public long getStartTime()
143     {
144         return getPropertyAsLong(START_TIME);
145     }
146
147     /**
148      * Get the duration
149      *
150      * @return the duration (in secs)
151      */

152     public long getDuration()
153     {
154         return getPropertyAsLong(DURATION);
155     }
156
157     /**
158      * Set the duration
159      *
160      * @param duration in seconds
161      */

162     public void setDuration(long duration)
163     {
164         setProperty(new LongProperty(DURATION,duration));
165     }
166
167     /**
168      * Get the delay
169      *
170      * @return the delay (in secs)
171      */

172     public long getDelay()
173     {
174         return getPropertyAsLong(DELAY);
175     }
176
177     /**
178      * Set the delay
179      *
180      * @param delay in seconds
181      */

182     public void setDelay(long delay)
183     {
184         setProperty(new LongProperty(DELAY,delay));
185     }
186
187
188
189     /**
190      * Set the EndTime value.
191      *
192      * @param etime - the EndTime value.
193      */

194     public void setEndTime(long etime)
195     {
196         setProperty(new LongProperty(END_TIME, etime));
197     }
198
199     /**
200      * Get the end time value.
201      *
202      * @return the end time value.
203      */

204     public long getEndTime()
205     {
206         return getPropertyAsLong(END_TIME);
207     }
208
209     /**
210      * Set the ramp-up value.
211      *
212      * @param rampUp the ramp-up value.
213      */

214     public void setRampUp(int rampUp)
215     {
216         setProperty(new IntegerProperty(RAMP_TIME,rampUp));
217     }
218
219     /**
220      * Get the ramp-up value.
221      *
222      * @return the ramp-up value.
223      */

224     public int getRampUp()
225     {
226         return getPropertyAsInt(ThreadGroup.RAMP_TIME);
227     }
228
229     /**
230      * Get the sampler controller.
231      *
232      * @return the sampler controller.
233      */

234     public Controller getSamplerController()
235     {
236         return (Controller) getProperty(MAIN_CONTROLLER).getObjectValue();
237     }
238
239     /**
240      * Set the sampler controller.
241      *
242      * @param c the sampler controller.
243      */

244     public void setSamplerController(LoopController c)
245     {
246         c.setContinueForever(false);
247         setProperty(new TestElementProperty(MAIN_CONTROLLER, c));
248     }
249
250     /**
251      * Get the number of threads.
252      *
253      * @return the number of threads.
254      */

255     public int getNumThreads()
256     {
257         return this.getPropertyAsInt(ThreadGroup.NUM_THREADS);
258     }
259
260     /**
261      * Get the default number of threads.
262      *
263      * @return the default number of threads.
264      */

265     public int getDefaultNumThreads()
266     {
267         return DEFAULT_NUM_THREADS;
268     }
269
270     /**
271      * Get the default ramp-up value.
272      *
273      * @return the default ramp-up value (in seconds).
274      */

275     public int getDefaultRampUp()
276     {
277         return DEFAULT_RAMP_UP;
278     }
279
280     /**
281      * Add a test element.
282      *
283      * @param child the test element to add.
284      */

285     public void addTestElement(TestElement child)
286     {
287         getSamplerController().addTestElement(child);
288     }
289
290     /**
291      * A sample has occurred.
292      *
293      *@param e the sample event.
294      */

295     public void sampleOccurred(SampleEvent e)
296     {
297         if (queue == null)
298         {
299             queue = new SampleQueue();
300             Thread JavaDoc thread = new Thread JavaDoc(queue);
301             //thread.setPriority(Thread.MAX_PRIORITY);
302
thread.start();
303         }
304         queue.sampleOccurred(e);
305     }
306
307     /**
308      * A sample has started.
309      *
310      *@param e the sample event.
311      */

312     public void sampleStarted(SampleEvent e)
313     {
314     }
315
316     /**
317      * A sample has stopped.
318      *
319      * @param e the sample event
320      */

321     public void sampleStopped(SampleEvent e)
322     {
323     }
324
325     /**
326      * Separate thread to deliver all SampleEvents. This ensures that sample
327      * listeners will get sample events one at a time and can thus ignore thread
328      * issues.
329      *
330      * @author Mike Stover
331      * @version $Id: ThreadGroup.java,v 1.24 2004/02/18 23:53:33 sebb Exp $
332      */

333     private class SampleQueue implements Runnable JavaDoc, Serializable JavaDoc
334     {
335         List JavaDoc occurredQ = Collections.synchronizedList(new LinkedList JavaDoc());
336
337         /**
338          * No-arg constructor.
339          */

340         public SampleQueue()
341         {
342         }
343
344         /**
345          * A sample occurred.
346          *
347          * @param e the sample event.
348          */

349         public synchronized void sampleOccurred(SampleEvent e)
350         {
351             occurredQ.add(e);
352             this.notifyAll();
353         }
354
355         /**
356          * Run the thread.
357          *
358          * @see java.lang.Runnable#run()
359          */

360         public void run()
361         {
362             SampleEvent event = null;
363             while (true)
364             {
365                 try
366                 {
367                     event = (SampleEvent) occurredQ.remove(0);
368                 }
369                 catch (Exception JavaDoc ex)
370                 {
371                     waitForSamples();
372                     continue;
373                 }
374                 try
375                 {
376                     if (event != null)
377                     {
378                         Iterator JavaDoc iter = listeners.iterator();
379                         while (iter.hasNext())
380                         {
381                             ((SampleListener) iter.next()).sampleOccurred(
382                                 event);
383                         }
384                         iter = remoteListeners.iterator();
385                         while (iter.hasNext())
386                         {
387                             try
388                             {
389                                 (
390                                     (RemoteSampleListener) iter
391                                         .next())
392                                         .sampleOccurred(
393                                     event);
394                             }
395                             catch (Exception JavaDoc ex)
396                             {
397                                 log.error("", ex);
398                             }
399                         }
400                     }
401                     else
402                     {
403                         waitForSamples();
404                     }
405                 }
406                 catch (Throwable JavaDoc ex)
407                 {
408                     log.error("", ex);
409                 }
410
411             }
412         }
413
414         private synchronized void waitForSamples()
415         {
416             try
417             {
418                 this.wait();
419             }
420             catch (Exception JavaDoc ex)
421             {
422                 log.error("", ex);
423             }
424         }
425     }
426     
427     /* (non-Javadoc)
428      * @see Controller#addIterationListener(LoopIterationListener)
429      */

430     public void addIterationListener(LoopIterationListener lis)
431     {
432         getSamplerController().addIterationListener(lis);
433     }
434
435     /* (non-Javadoc)
436      * @see Controller#initialize()
437      */

438     public void initialize()
439     {
440         getSamplerController().initialize();
441     }
442
443     /**
444      * Check if a sampler error should cause thread to stop.
445      *
446      * @return true if should stop
447      */

448     public boolean getOnErrorStopThread()
449     {
450         return getPropertyAsString(ThreadGroup.ON_SAMPLE_ERROR)
451             .equalsIgnoreCase(ON_SAMPLE_ERROR_STOPTHREAD);
452     }
453
454     /**
455      * Check if a sampler error should cause test to stop.
456      *
457      * @return true if should stop
458      */

459     public boolean getOnErrorStopTest()
460     {
461         return getPropertyAsString(ThreadGroup.ON_SAMPLE_ERROR)
462             .equalsIgnoreCase(ON_SAMPLE_ERROR_STOPTEST);
463     }
464
465 }
466
Popular Tags