/*
* ====================================================================
* The Apache Software License, Version 1.1
*
* Copyright (c) 2001 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution,
* if any, must include the following acknowledgment:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowledgment may appear in the software itself,
* if and wherever such third-party acknowledgments normally appear.
*
* 4. The names "Apache" and "Apache Software Foundation" and
* "Apache JMeter" must not be used to endorse or promote products
* derived from this software without prior written permission. For
* written permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache",
* "Apache JMeter", nor may "Apache" appear in their name, without
* prior written permission of the Apache Software Foundation.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*/
package org.apache.jmeter.threads;
import java.util.*;
import java.io.*;
import org.apache.jmeter.timers.Timer;
import org.apache.jmeter.control.LoopController;
import org.apache.jmeter.control.SamplerController;
import org.apache.jmeter.samplers.RemoteSampleListener;
import org.apache.jmeter.samplers.SampleEvent;
import org.apache.jmeter.samplers.SampleListener;
import org.apache.jmeter.gui.popup.*;
import org.apache.jmeter.gui.*;
import org.apache.jmeter.config.ConfigElement;
import org.apache.jmeter.save.Saveable;
import org.apache.jmeter.util.JMeterUtils;
/************************************************************
* Title: Apache JMeter Description: Copyright: Copyright (c) 2000 Company:
* Apache Foundation
*
*@author Michael Stover
*@created $Date: 2002/03/04 23:08:55 $
*@version 1.0
***********************************************************/
public class ThreadGroup implements SampleListener,JMeterComponentModel,Saveable,
Serializable
{
LoopController control;
List listeners = new LinkedList();
List remoteListeners = new LinkedList();
private final int DEFAULT_NUM_THREADS = 1;
private final int DEFAULT_RAMP_UP = 0;
int numThreads = DEFAULT_NUM_THREADS;
List timers = new LinkedList();
private String name;
private SampleQueue queue = null;
int rampUp = DEFAULT_RAMP_UP;
private int threadsStarted = 0;
private static List addableList;
/************************************************************
* !ToDo (Constructor description)
***********************************************************/
public ThreadGroup()
{
control = new LoopController();
((LoopController)control).setContinueForever(false);
}
public void uncompile()
{
((JMeterComponentModel)control).uncompile();
timers.clear();
this.clearListeners();
this.clearRemoteListeners();
queue = null;
}
/************************************************************
* !ToDo (Method aadescription)
*
*@param numThreads !ToDo (Parameter description)
***********************************************************/
public void setNumThreads(int numThreads)
{
this.numThreads = numThreads;
}
/************************************************************
* !ToDo (Method description)
*
*@param timer !ToDo (Parameter description)
***********************************************************/
public void addTimer(Timer timer)
{
this.timers.add(timer);
}
public Class getTagHandlerClass()
{
return org.apache.jmeter.save.handlers.ThreadGroupHandler.class;
}
/************************************************************
* !ToDo (Method description)
*
*@param newName !ToDo (Parameter description)
***********************************************************/
public void setName(String newName)
{
name = newName;
}
public Class getGuiClass()
{
return org.apache.jmeter.threads.gui.ThreadGroupGui.class;
}
/************************************************************
* !ToDo (Method aadescription)
*
*@param rampUp !ToDo (Parameter description)
***********************************************************/
public void setRampUp(int rampUp)
{
this.rampUp = rampUp;
}
/************************************************************
* !ToDoo (Method description)
*
*@return !ToDo (Return description)
***********************************************************/
public int getRampUp()
{
return this.rampUp;
}
public synchronized int threadNumber()
{
return threadsStarted++;
}
/************************************************************
* !ToDoo (Method description)
*
*@return !ToDo (Return description)
***********************************************************/
public SamplerController getSamplerController()
{
return this.control;
}
public void setSamplerController(LoopController c)
{
control = c;
control.setContinueForever(false);
}
/************************************************************
* !ToDoo (Method description)
*
*@return !ToDo (Return description)
***********************************************************/
public int getNumThreads()
{
return this.numThreads;
}
/************************************************************
* !ToDoo (Method description)
*
*@return !ToDo (Return description)
***********************************************************/
public int getDefaultNumThreads()
{
return this.DEFAULT_NUM_THREADS;
}
/************************************************************
* !ToDoo (Method description)
*
*@return !ToDo (Return description)
***********************************************************/
public int getDefaultRampUp()
{
return this.DEFAULT_RAMP_UP;
}
public synchronized void resetThreadCount()
{
threadsStarted = 0;
}
/************************************************************
* !ToDoo (Method description)
*
*@return !ToDo (Return description)
***********************************************************/
public List getTimers()
{
return this.timers;
}
/************************************************************
* !ToDoo (Method description)
*
*@return !ToDo (Return description)
***********************************************************/
public Collection getListeners()
{
return this.listeners;
}
/************************************************************
* !ToDoo (Method description)
*
*@return !ToDo (Return description)
***********************************************************/
public Collection getRemoteListeners()
{
return this.remoteListeners;
}
/************************************************************
* !ToDoo (Method description)
*
*@return !ToDo (Return description)
***********************************************************/
public String getName()
{
return name;
}
/************************************************************
* !ToDo
*
*@param control !ToDo
***********************************************************/
public void addSamplerController(SamplerController control)
{
this.control.addSamplerController(control);
}
public void addConfigElement(ConfigElement config)
{
this.control.addConfigElement(config);
}
/************************************************************
* !ToDo (Method description)
***********************************************************/
public void clearListeners()
{
listeners.clear();
}
/************************************************************
* !ToDo (Method description)
***********************************************************/
public void clearRemoteListeners()
{
remoteListeners.clear();
}
/************************************************************
* !ToDo
*
*@param listener !ToDo
***********************************************************/
public void addListener(SampleListener listener)
{
this.listeners.add(listener);
}
/************************************************************
* !ToDo
*
*@param listener !ToDo
***********************************************************/
public void addRemoteListener(RemoteSampleListener listener)
{
this.remoteListeners.add(listener);
}
public void addJMeterComponent(JMeterComponentModel child)
{
if (child instanceof Timer)
{
addTimer((Timer)child);
}
else if (child instanceof ConfigElement)
{
addConfigElement((ConfigElement)child);
}
else if (child instanceof SamplerController)
{
addSamplerController((SamplerController)child);
}
else if (child instanceof SampleListener)
{
addListener((SampleListener)child);
}
}
/************************************************************
* !ToDo (Method description)
*
*@param listener !ToDo (Parameter description)
***********************************************************/
public void removeListener(SampleListener listener)
{
this.listeners.remove(listener);
}
/************************************************************
* !ToDo (Method description)
*
*@param e !ToDo (Parameter description)
***********************************************************/
public void sampleOccurred(SampleEvent e)
{
if(queue == null)
{
queue = new SampleQueue();
Thread thread = new Thread(queue);
//thread.setPriority(Thread.MAX_PRIORITY);
thread.start();
}
queue.sampleOccurred(e);
}
/************************************************************
* A sample has started.
*
*@param e !ToDo (Parameter description)
***********************************************************/
public void sampleStarted(SampleEvent e)
{
Iterator iter = listeners.iterator();
while (iter.hasNext())
{
((SampleListener)iter.next()).sampleStarted(e);
}
iter = remoteListeners.iterator();
while (iter.hasNext())
{
try
{
((RemoteSampleListener)iter.next()).sampleStarted(e);
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
}
/************************************************************
* A sample has stopped.
*
*@param e !ToDo (Parameter description)
***********************************************************/
public void sampleStopped(SampleEvent e)
{
Iterator iter = listeners.iterator();
while (iter.hasNext())
{
((SampleListener)iter.next()).sampleStopped(e);
}
iter = remoteListeners.iterator();
while (iter.hasNext())
{
try
{
((RemoteSampleListener)iter.next()).sampleStopped(e);
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
}
public String getClassLabel()
{
// WARNING! This String value must be identical to the String value in
// org.apache.jmeter.control.TestPlan static initializer. If it's not
// you will not be able to add a Thread Group element to a Test Plan.
return JMeterUtils.getResString("threadgroup");
}
/**
* Indicates whether the item is editable. If so, the popup will include a
* "edit" choice which will display the item's gui screen in the main panel.
*/
public boolean isEditable()
{
return true;
}
/**
* Returns the list of object types that can be added to this item.
*/
public Collection getAddList()
{
if(addableList == null)
{
addableList = new LinkedList();
addableList.add(MenuFactory.getTimerMenu());
addableList.add(MenuFactory.getListenerMenu());
addableList.add(MenuFactory.getLogicControllerMenu());
addableList.add(MenuFactory.getGenerativeControllerMenu());
addableList.add(MenuFactory.getConfigElementMenu());
}
return addableList;
}
/**
* Separate thread to deliver all SampleEvents. This ensures that sample listeners
* will get sample events 1 at a time, and can thus ignore thread issues.
*/
private class SampleQueue implements Runnable, Serializable
{
List occurredQ = Collections.synchronizedList(new LinkedList());
public SampleQueue()
{
}
public synchronized void sampleOccurred(SampleEvent e)
{
occurredQ.add(e);
this.notify();
}
public void run()
{
SampleEvent event = null;
while(true)
{
try
{
event = (SampleEvent)occurredQ.remove(0);
}
catch (Exception ex)
{
waitForSamples();
continue;
}
try
{
if(event != null)
{
Iterator iter = listeners.iterator();
while (iter.hasNext())
{
((SampleListener)iter.next()).sampleOccurred(event);
}
iter = remoteListeners.iterator();
while (iter.hasNext())
{
try
{
((RemoteSampleListener)iter.next()).sampleOccurred(event);
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
}
else
waitForSamples();
}
catch (Throwable ex)
{
ex.printStackTrace();
}
}
}
private synchronized void waitForSamples()
{
try
{
this.wait();
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
}
}