httpClient多執行緒請求
阿新 • • 發佈:2018-12-11
1
package generate.httpclient;
2
3
import java.util.List;
4
import java.util.concurrent.ExecutorService;
5
import java.util.concurrent.Executors;
6
7
import org.apache.http.HttpEntity;
8
import org.apache.http.HttpResponse;
9
import org.apache.http.client.HttpClient;
10
import org.apache.http.client.methods.HttpGet;
11
import org.apache.http.conn.ClientConnectionManager;
12
import org.apache.http.conn.params.ConnManagerParams;
13
import org.apache.http.conn.scheme.PlainSocketFactory;
14
import org.apache.http.conn.scheme.Scheme;
15
import org.apache.http.conn.scheme.SchemeRegistry;
16
import org.apache.http.impl.client.DefaultHttpClient;
17
import org.apache.http.impl.conn.PoolingClientConnectionManager;
18
import org.apache.http.params.BasicHttpParams;
19
import org.apache.http.params.HttpConnectionParams;
20
import org.apache.http.params.HttpParams;
21
import org.apache.http.protocol.BasicHttpContext;
22
import org.apache.http.protocol.HttpContext;
23
import org.apache.http.util.EntityUtils;
24
25
public
class ThreadPoolHttpClient {
26
//
執行緒池
27
private ExecutorService exe =
null;
28
//
執行緒池的容量
29
private
static
final
int POOL_SIZE = 20;
30
private HttpClient client =
null;
31 String[] urls=
null;
32
public ThreadPoolHttpClient(String[] urls){
33
this.urls=urls;
34 }
35
public
void test()
throws Exception {
36 exe = Executors.newFixedThreadPool(POOL_SIZE);
37 HttpParams params =
new BasicHttpParams();
38
/*
從連線池中取連線的超時時間
*/
39 ConnManagerParams.setTimeout(params, 1000);
40
/*
連線超時
*/
41 HttpConnectionParams.setConnectionTimeout(params, 2000);
42
/*
請求超時
*/
43 HttpConnectionParams.setSoTimeout(params, 4000);
44 SchemeRegistry schemeRegistry =
new SchemeRegistry();
45 schemeRegistry.register(
46
new Scheme("http", 80, PlainSocketFactory.getSocketFactory()));
47
48
//
ClientConnectionManager cm = new PoolingClientConnectionManager(schemeRegistry);
49
PoolingClientConnectionManager cm=
new PoolingClientConnectionManager(schemeRegistry);
50 cm.setMaxTotal(10);
51
final HttpClient httpClient =
new DefaultHttpClient(cm,params);
52
53
//
URIs to perform GETs on
54
final String[] urisToGet = urls;
55
/* 有多少url建立多少執行緒,url多時機子撐不住
56
// create a thread for each URI
57
GetThread[] threads = new GetThread[urisToGet.length];
58
for (int i = 0; i < threads.length; i++) {
59
HttpGet httpget = new HttpGet(urisToGet[i]);
60
threads[i] = new GetThread(httpClient, httpget);
61
}
62
// start the threads
63
for (int j = 0; j < threads.length; j++) {
64
threads[j].start();
65
}
66
67
// join the threads,等待所有請求完成
68
for (int j = 0; j < threads.length; j++) {
69
threads[j].join();
70
}
71
使用執行緒池*/
72
for (
int i = 0; i < urisToGet.length; i++) {
73
final
int j=i;
74 System.out.println(j);
75 HttpGet httpget =
new HttpGet(urisToGet[i]);
76 exe.execute(
new GetThread(httpClient, httpget));
77 }
78
79
80
//
建立執行緒池,每次呼叫POOL_SIZE
81
/*
82
for (int i = 0; i < urisToGet.length; i++) {
83
final int j=i;
84
System.out.println(j);
85
exe.execute(new Thread() {
86
@Override
87
public void run() {
88
this.setName("threadsPoolClient"+j);
89
90
try {
91
this.sleep(100);
92
System.out.println(j);
93
} catch (InterruptedException e) {
94
// TODO Auto-generated catch block
95
e.printStackTrace();
96
}
97
98
HttpGet httpget = new HttpGet(urisToGet[j]);
99
new GetThread(httpClient, httpget).get();
100
}
101
102
103
104
});
105
}
106
107
*/
108
//
exe.shutdown();
109
System.out.println("Done");
110 }
111
static
class GetThread
extends Thread{
112
113
private
final HttpClient httpClient;
114
private
final HttpContext context;
115
private
final HttpGet httpget;
116
117
public GetThread(HttpClient httpClient, HttpGet httpget) {
118
this.httpClient = httpClient;
119
this.context =
new BasicHttpContext();
120
this.httpget = httpget;
121 }
122 @Override
123
public
void run(){
124
this.setName("threadsPoolClient");
125
try {
126 Thread.sleep(5000);
127 }
catch (InterruptedException e) {
128
//
TODO Auto-generated catch block
129
e.printStackTrace();
130 }
131 get();
132 }
133
134
public
void get() {
135
try {
136 HttpResponse response =
this.httpClient.execute(
this.httpget,
this.context);
137 HttpEntity entity = response.getEntity();
138
if (entity !=
null) {
139 System.out.println(
this.httpget.getURI()+": status"+response.getStatusLine().toString());
140 }
141
//
ensure the connection gets released to the manager
142
EntityUtils.consume(entity);
143 }
catch (Exception ex) {
144
this.httpget.abort();
145 }
finally{
146 httpget.releaseConnection();
147 }
148 }
149 }
150 }