| 1 |
/*
|
| 2 |
* $HeadURL$
|
| 3 |
* $Revision$
|
| 4 |
* $Date$
|
| 5 |
*
|
| 6 |
* ====================================================================
|
| 7 |
*
|
| 8 |
* Licensed to the Apache Software Foundation (ASF) under one or more
|
| 9 |
* contributor license agreements. See the NOTICE file distributed with
|
| 10 |
* this work for additional information regarding copyright ownership.
|
| 11 |
* The ASF licenses this file to You under the Apache License, Version 2.0
|
| 12 |
* (the "License"); you may not use this file except in compliance with
|
| 13 |
* the License. You may obtain a copy of the License at
|
| 14 |
*
|
| 15 |
* http://www.apache.org/licenses/LICENSE-2.0
|
| 16 |
*
|
| 17 |
* Unless required by applicable law or agreed to in writing, software
|
| 18 |
* distributed under the License is distributed on an "AS IS" BASIS,
|
| 19 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 20 |
* See the License for the specific language governing permissions and
|
| 21 |
* limitations under the License.
|
| 22 |
* ====================================================================
|
| 23 |
*
|
| 24 |
* This software consists of voluntary contributions made by many
|
| 25 |
* individuals on behalf of the Apache Software Foundation. For more
|
| 26 |
* information on the Apache Software Foundation, please see
|
| 27 |
* <http://www.apache.org/>.
|
| 28 |
*
|
| 29 |
* [Additional notices, if required by prior licensing conditions]
|
| 30 |
*
|
| 31 |
*/
|
| 32 |
|
| 33 |
import org.apache.commons.httpclient.HttpClient;
|
| 34 |
import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager;
|
| 35 |
import org.apache.commons.httpclient.methods.GetMethod;
|
| 36 |
|
| 37 |
/**
|
| 38 |
* An example that performs GETs from multiple threads.
|
| 39 |
*
|
| 40 |
* @author Michael Becke
|
| 41 |
*/
|
| 42 |
public class MultiThreadedExample {
|
| 43 |
|
| 44 |
/**
|
| 45 |
* Constructor for MultiThreadedExample.
|
| 46 |
*/
|
| 47 |
public MultiThreadedExample() {
|
| 48 |
super();
|
| 49 |
}
|
| 50 |
|
| 51 |
public static void main(String[] args) {
|
| 52 |
|
| 53 |
// Create an HttpClient with the MultiThreadedHttpConnectionManager.
|
| 54 |
// This connection manager must be used if more than one thread will
|
| 55 |
// be using the HttpClient.
|
| 56 |
HttpClient httpClient = new HttpClient(new MultiThreadedHttpConnectionManager());
|
| 57 |
// Set the default host/protocol for the methods to connect to.
|
| 58 |
// This value will only be used if the methods are not given an absolute URI
|
| 59 |
httpClient.getHostConfiguration().setHost("hc.apache.org", 80, "http");
|
| 60 |
|
| 61 |
// create an array of URIs to perform GETs on
|
| 62 |
String[] urisToGet = {
|
| 63 |
"/",
|
| 64 |
"/httpclient-3.x/status.html",
|
| 65 |
"/httpclient-3.x/methods/",
|
| 66 |
"http://svn.apache.org/viewvc/httpcomponents/oac.hc3x/"
|
| 67 |
};
|
| 68 |
|
| 69 |
// create a thread for each URI
|
| 70 |
GetThread[] threads = new GetThread[urisToGet.length];
|
| 71 |
for (int i = 0; i < threads.length; i++) {
|
| 72 |
GetMethod get = new GetMethod(urisToGet[i]);
|
| 73 |
get.setFollowRedirects(true);
|
| 74 |
threads[i] = new GetThread(httpClient, get, i + 1);
|
| 75 |
}
|
| 76 |
|
| 77 |
// start the threads
|
| 78 |
for (int j = 0; j < threads.length; j++) {
|
| 79 |
threads[j].start();
|
| 80 |
}
|
| 81 |
|
| 82 |
}
|
| 83 |
|
| 84 |
/**
|
| 85 |
* A thread that performs a GET.
|
| 86 |
*/
|
| 87 |
static class GetThread extends Thread {
|
| 88 |
|
| 89 |
private HttpClient httpClient;
|
| 90 |
private GetMethod method;
|
| 91 |
private int id;
|
| 92 |
|
| 93 |
public GetThread(HttpClient httpClient, GetMethod method, int id) {
|
| 94 |
this.httpClient = httpClient;
|
| 95 |
this.method = method;
|
| 96 |
this.id = id;
|
| 97 |
}
|
| 98 |
|
| 99 |
/**
|
| 100 |
* Executes the GetMethod and prints some satus information.
|
| 101 |
*/
|
| 102 |
public void run() {
|
| 103 |
|
| 104 |
try {
|
| 105 |
|
| 106 |
System.out.println(id + " - about to get something from " + method.getURI());
|
| 107 |
// execute the method
|
| 108 |
httpClient.executeMethod(method);
|
| 109 |
|
| 110 |
System.out.println(id + " - get executed");
|
| 111 |
// get the response body as an array of bytes
|
| 112 |
byte[] bytes = method.getResponseBody();
|
| 113 |
|
| 114 |
System.out.println(id + " - " + bytes.length + " bytes read");
|
| 115 |
|
| 116 |
} catch (Exception e) {
|
| 117 |
System.out.println(id + " - error: " + e);
|
| 118 |
} finally {
|
| 119 |
// always release the connection after we're done
|
| 120 |
method.releaseConnection();
|
| 121 |
System.out.println(id + " - connection released");
|
| 122 |
}
|
| 123 |
}
|
| 124 |
|
| 125 |
}
|
| 126 |
|
| 127 |
}
|