com.sun.faban.driver.transport.util
Class MultipleTransport

java.lang.Object
  extended by com.sun.faban.driver.transport.util.MultipleTransport
All Implemented Interfaces:
Runnable

public class MultipleTransport
extends Object
implements Runnable

This class is used to load multiple HTTP requests, emulating a browser. It is configured with a number of threads, and requests are loaded simultaneously by those threads. Typical usage of this class:

    MultipleTransport mt = new MultipleTransport(2);
    mt.addURL("http://myhost/index.html");
    mt.addURL("http://myhost/image1.gif");
    mt.addURL("http://myhost/image2.gif");
    mt.addURL("http://myhost/image3.gif");
    mt.addURL("http://myhost/image4.gif");
    boolean success = mt.waitForAll();
 
Nothing is actually loaded until the waitForAll() method is called. When that method is called, the first URL is loaded (hence, the first URL is typically the page URL) in the calling thread. Subsequently, the remaining URLs (typically images and other resources) are loaded by the calling thread and the threads created by this class. The total number of simultaneous threads is specified by the constructor of this class, but that includes the calling thread: that creates a more efficient thread usage. After calling the waitForAll() method, the MultipleTransport is reset and can be reused to load a new set of requests; if the requests are loaded from the same host, then the keep-alive semantics of the HTTP transport will be used. When finished with this object, you should call its close() method to terminate its threads; otherwise the threads will leak. I have chosen not to add a finalizer to this class to close the threads, since that particular operation is known to cause issues with the garbage collector. TODO: Need a way to retrieve individual URL status


Constructor Summary
MultipleTransport(int n)
          Create an MultipleTransport that can load n requests simultaneously.
 
Method Summary
 void addURL(String url)
          Add a request to the list of things to be retrieved.
 void addURL(String url, String postData)
          Add a request to the list of things to be retrieved.
 void close()
          Close down the loader, stopping all its threads.
 void run()
          Runs the parallel threads fetching the other URLs.
 String toString()
          Provides a string representation of this transport.
 boolean waitForAll()
          Start loading requests and wait for them all to be loaded.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

MultipleTransport

public MultipleTransport(int n)
Create an MultipleTransport that can load n requests simultaneously.

Parameters:
n - Number of threads to use to load requests. That includes the thread that calls waitForAll(), so we create n-1 threads.
Method Detail

run

public void run()
Runs the parallel threads fetching the other URLs.

Specified by:
run in interface Runnable
See Also:
Runnable.run()

addURL

public void addURL(String url)
Add a request to the list of things to be retrieved. Retrieval does not start until the waitForAll() method is called.

Parameters:
url - The URL to load

addURL

public void addURL(String url,
                   String postData)
Add a request to the list of things to be retrieved. Retrieval does not start until the waitForAll() method is called.

Parameters:
url - The URL to load
postData - Data for a POST URL. If data is null, URL is assumed to be a GET URL.

waitForAll

public boolean waitForAll()
Start loading requests and wait for them all to be loaded. This also clears the object so that it may be reused.

Returns:
boolean Whether or not all images were loaded successfully.

close

public void close()
Close down the loader, stopping all its threads.


toString

public String toString()
Provides a string representation of this transport.

Overrides:
toString in class Object
Returns:
The string representation
See Also:
Object.toString()