Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
W
webmagic
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
沈俊林
webmagic
Commits
7fb44d2e
Commit
7fb44d2e
authored
Oct 14, 2013
by
yihua.huang
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
#30 reuse PoolingClientConnectionManager for HttpClientDownloader
parent
5a226387
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
22 additions
and
24 deletions
+22
-24
HttpClientDownloader.java
...s/codecraft/webmagic/downloader/HttpClientDownloader.java
+11
-1
HttpClientPool.java
...java/us/codecraft/webmagic/downloader/HttpClientPool.java
+10
-22
pom.xml
webmagic-samples/pom.xml
+1
-1
No files found.
webmagic-core/src/main/java/us/codecraft/webmagic/downloader/HttpClientDownloader.java
View file @
7fb44d2e
...
...
@@ -34,6 +34,8 @@ public class HttpClientDownloader implements Downloader {
private
Logger
logger
=
Logger
.
getLogger
(
getClass
());
private
HttpClientPool
httpClientPool
;
private
int
poolSize
=
1
;
/**
...
...
@@ -58,6 +60,13 @@ public class HttpClientDownloader implements Downloader {
return
(
Html
)
page
.
getHtml
();
}
private
HttpClientPool
getHttpClientPool
(){
if
(
httpClientPool
==
null
){
httpClientPool
=
new
HttpClientPool
(
poolSize
);
}
return
httpClientPool
;
}
@Override
public
Page
download
(
Request
request
,
Task
task
)
{
Site
site
=
null
;
...
...
@@ -78,7 +87,7 @@ public class HttpClientDownloader implements Downloader {
acceptStatCode
.
add
(
200
);
}
logger
.
info
(
"downloading page "
+
request
.
getUrl
());
HttpClient
httpClient
=
HttpClientPool
.
getInstance
(
poolSize
).
getClient
(
site
);
HttpClient
httpClient
=
getHttpClientPool
(
).
getClient
(
site
);
try
{
HttpGet
httpGet
=
new
HttpGet
(
request
.
getUrl
());
if
(
headers
!=
null
){
...
...
@@ -150,6 +159,7 @@ public class HttpClientDownloader implements Downloader {
@Override
public
void
setThread
(
int
thread
)
{
poolSize
=
thread
;
httpClientPool
=
new
HttpClientPool
(
thread
);
}
private
void
handleGzip
(
HttpResponse
httpResponse
)
{
...
...
webmagic-core/src/main/java/us/codecraft/webmagic/downloader/HttpClientPool.java
View file @
7fb44d2e
...
...
@@ -24,23 +24,19 @@ import java.util.Map;
*/
public
class
HttpClientPool
{
public
static
volatile
HttpClientPool
INSTANCE
;
public
static
HttpClientPool
getInstance
(
int
poolSize
)
{
if
(
INSTANCE
==
null
)
{
synchronized
(
HttpClientPool
.
class
)
{
if
(
INSTANCE
==
null
)
{
INSTANCE
=
new
HttpClientPool
(
poolSize
);
}
}
}
return
INSTANCE
;
}
private
int
poolSize
;
private
HttpClientPool
(
int
poolSize
)
{
private
PoolingClientConnectionManager
connectionManager
;
public
HttpClientPool
(
int
poolSize
)
{
this
.
poolSize
=
poolSize
;
SchemeRegistry
schemeRegistry
=
new
SchemeRegistry
();
schemeRegistry
.
register
(
new
Scheme
(
"http"
,
80
,
PlainSocketFactory
.
getSocketFactory
()));
schemeRegistry
.
register
(
new
Scheme
(
"https"
,
443
,
SSLSocketFactory
.
getSocketFactory
()));
connectionManager
=
new
PoolingClientConnectionManager
(
schemeRegistry
);
connectionManager
.
setMaxTotal
(
poolSize
);
connectionManager
.
setDefaultMaxPerRoute
(
100
);
}
public
HttpClient
getClient
(
Site
site
)
{
...
...
@@ -58,7 +54,6 @@ public class HttpClientPool {
params
.
setIntParameter
(
CoreConnectionPNames
.
CONNECTION_TIMEOUT
,
3000
);
}
params
.
setParameter
(
ClientPNames
.
COOKIE_POLICY
,
CookiePolicy
.
BEST_MATCH
);
HttpProtocolParamBean
paramsBean
=
new
HttpProtocolParamBean
(
params
);
paramsBean
.
setVersion
(
HttpVersion
.
HTTP_1_1
);
...
...
@@ -67,13 +62,6 @@ public class HttpClientPool {
}
paramsBean
.
setUseExpectContinue
(
false
);
SchemeRegistry
schemeRegistry
=
new
SchemeRegistry
();
schemeRegistry
.
register
(
new
Scheme
(
"http"
,
80
,
PlainSocketFactory
.
getSocketFactory
()));
schemeRegistry
.
register
(
new
Scheme
(
"https"
,
443
,
SSLSocketFactory
.
getSocketFactory
()));
PoolingClientConnectionManager
connectionManager
=
new
PoolingClientConnectionManager
(
schemeRegistry
);
connectionManager
.
setMaxTotal
(
poolSize
);
connectionManager
.
setDefaultMaxPerRoute
(
100
);
DefaultHttpClient
httpClient
=
new
DefaultHttpClient
(
connectionManager
,
params
);
if
(
site
!=
null
)
{
generateCookie
(
httpClient
,
site
);
...
...
webmagic-samples/pom.xml
View file @
7fb44d2e
...
...
@@ -5,7 +5,7 @@
<parent>
<artifactId>
webmagic-parent
</artifactId>
<groupId>
us.codecraft
</groupId>
<version>
0.3.
2
</version>
<version>
0.3.
3-SNAPSHOT
</version>
</parent>
<modelVersion>
4.0.0
</modelVersion>
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment