001: /*
002: * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//httpclient/src/examples/MultiThreadedExample.java,v 1.3 2004/02/22 18:08:45 olegk Exp $
003: * $Revision: 554236 $
004: * $Date: 2007-07-07 20:15:09 +0200 (Sat, 07 Jul 2007) $
005: *
006: * ====================================================================
007: *
008: * Licensed to the Apache Software Foundation (ASF) under one or more
009: * contributor license agreements. See the NOTICE file distributed with
010: * this work for additional information regarding copyright ownership.
011: * The ASF licenses this file to You under the Apache License, Version 2.0
012: * (the "License"); you may not use this file except in compliance with
013: * the License. You may obtain a copy of the License at
014: *
015: * http://www.apache.org/licenses/LICENSE-2.0
016: *
017: * Unless required by applicable law or agreed to in writing, software
018: * distributed under the License is distributed on an "AS IS" BASIS,
019: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
020: * See the License for the specific language governing permissions and
021: * limitations under the License.
022: * ====================================================================
023: *
024: * This software consists of voluntary contributions made by many
025: * individuals on behalf of the Apache Software Foundation. For more
026: * information on the Apache Software Foundation, please see
027: * <http://www.apache.org/>.
028: *
029: * [Additional notices, if required by prior licensing conditions]
030: *
031: */
032:
033: import org.apache.commons.httpclient.HttpClient;
034: import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager;
035: import org.apache.commons.httpclient.methods.GetMethod;
036:
037: /**
038: * An example that performs GETs from multiple threads.
039: *
040: * @author Michael Becke
041: */
042: public class MultiThreadedExample {
043:
044: /**
045: * Constructor for MultiThreadedExample.
046: */
047: public MultiThreadedExample() {
048: super ();
049: }
050:
051: public static void main(String[] args) {
052:
053: // Create an HttpClient with the MultiThreadedHttpConnectionManager.
054: // This connection manager must be used if more than one thread will
055: // be using the HttpClient.
056: HttpClient httpClient = new HttpClient(
057: new MultiThreadedHttpConnectionManager());
058: // Set the default host/protocol for the methods to connect to.
059: // This value will only be used if the methods are not given an absolute URI
060: httpClient.getHostConfiguration().setHost("jakarta.apache.org",
061: 80, "http");
062:
063: // create an array of URIs to perform GETs on
064: String[] urisToGet = { "/", "/commons/",
065: "/commons/httpclient/",
066: "http://svn.apache.org/viewvc/jakarta/httpcomponents/oac.hc3x/" };
067:
068: // create a thread for each URI
069: GetThread[] threads = new GetThread[urisToGet.length];
070: for (int i = 0; i < threads.length; i++) {
071: GetMethod get = new GetMethod(urisToGet[i]);
072: get.setFollowRedirects(true);
073: threads[i] = new GetThread(httpClient, get, i + 1);
074: }
075:
076: // start the threads
077: for (int j = 0; j < threads.length; j++) {
078: threads[j].start();
079: }
080:
081: }
082:
083: /**
084: * A thread that performs a GET.
085: */
086: static class GetThread extends Thread {
087:
088: private HttpClient httpClient;
089: private GetMethod method;
090: private int id;
091:
092: public GetThread(HttpClient httpClient, GetMethod method, int id) {
093: this .httpClient = httpClient;
094: this .method = method;
095: this .id = id;
096: }
097:
098: /**
099: * Executes the GetMethod and prints some satus information.
100: */
101: public void run() {
102:
103: try {
104:
105: System.out.println(id
106: + " - about to get something from "
107: + method.getURI());
108: // execute the method
109: httpClient.executeMethod(method);
110:
111: System.out.println(id + " - get executed");
112: // get the response body as an array of bytes
113: byte[] bytes = method.getResponseBody();
114:
115: System.out.println(id + " - " + bytes.length
116: + " bytes read");
117:
118: } catch (Exception e) {
119: System.out.println(id + " - error: " + e);
120: } finally {
121: // always release the connection after we're done
122: method.releaseConnection();
123: System.out.println(id + " - connection released");
124: }
125: }
126:
127: }
128:
129: }
|