commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mbe...@apache.org
Subject cvs commit: jakarta-commons/httpclient/src/examples MultiThreadedExample.java
Date Sun, 02 Mar 2003 23:22:45 GMT
mbecke      2003/03/02 15:22:44

  Added:       httpclient/src/examples MultiThreadedExample.java
  Log:
  Initial checkin.  Added an example that performs GETs from muliple threads.
  
  Revision  Changes    Path
  1.1                  jakarta-commons/httpclient/src/examples/MultiThreadedExample.java
  
  Index: MultiThreadedExample.java
  ===================================================================
  import org.apache.commons.httpclient.HttpClient;
  import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager;
  import org.apache.commons.httpclient.methods.GetMethod;
  
  /**
   * An example that performs GETs from multiple threads.
   * 
   * @author Michael Becke
   */
  public class MultiThreadedExample {
  
      /**
       * Constructor for MultiThreadedExample.
       */
      public MultiThreadedExample() {
          super();
      }
  
      public static void main(String[] args) {
          
          // Create an HttpClient with the MultiThreadedHttpConnectionManager.
          // This connection manager must be used if more than one thread will
          // be using the HttpClient.
          HttpClient httpClient = new HttpClient(new MultiThreadedHttpConnectionManager());
          // Set the default host/protocol for the methods to connect to.
          // This value will only be used if the methods are not given an absolute URI
          httpClient.getHostConfiguration().setHost("jakarta.apache.org", 80, "http");
          
          // create an array of URIs to perform GETs on
          String[] urisToGet = {
              "/",
              "/commons/",
              "/commons/httpclient/",
              "http://cvs.apache.org/viewcvs/jakarta-commons/httpclient/"
          };
          
          // create a thread for each URI
          GetThread[] threads = new GetThread[urisToGet.length];
          for (int i = 0; i < threads.length; i++) {
              GetMethod get = new GetMethod(urisToGet[i]);
              get.setFollowRedirects(true);
              threads[i] = new GetThread(httpClient, get, i + 1);
          }
          
          // start the threads
          for (int j = 0; j < threads.length; j++) {
              threads[j].start();
          }
          
      }
      
      /**
       * A thread that performs a GET.
  	 */
      static class GetThread extends Thread {
          
          private HttpClient httpClient;
          private GetMethod method;
          private int id;
          
          public GetThread(HttpClient httpClient, GetMethod method, int id) {
              this.httpClient = httpClient;
              this.method = method;
              this.id = id;
          }
          
          /**
           * Executes the GetMethod and prints some satus information.
           */
          public void run() {
              
              try {
                  
                  System.out.println(id + " - about to get something from " + method.getURI());
                  // execute the method
                  httpClient.executeMethod(method);
                  
                  System.out.println(id + " - get executed");
                  // get the response body as an array of bytes
                  byte[] bytes = method.getResponseBody();
                  
                  System.out.println(id + " - " + bytes.length + " bytes read");
                  
              } catch (Exception e) {
                  System.out.println(id + " - error: " + e);
              } finally {
                  // always release the connection after we're done 
                  method.releaseConnection();
                  System.out.println(id + " - connection released");
              }
          }
         
      }
      
  }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Mime
View raw message