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
b4fcf411
Commit
b4fcf411
authored
Oct 31, 2013
by
yihua.huang
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add exit when comlete option
parent
35288787
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
46 additions
and
7 deletions
+46
-7
Spider.java
...agic-core/src/main/java/us/codecraft/webmagic/Spider.java
+45
-6
OschinaBlogPageProcesser.java
.../webmagic/processor/example/OschinaBlogPageProcesser.java
+1
-1
No files found.
webmagic-core/src/main/java/us/codecraft/webmagic/Spider.java
View file @
b4fcf411
...
...
@@ -18,6 +18,8 @@ import java.util.ArrayList;
import
java.util.List
;
import
java.util.concurrent.ExecutorService
;
import
java.util.concurrent.atomic.AtomicInteger
;
import
java.util.concurrent.locks.Condition
;
import
java.util.concurrent.locks.ReentrantLock
;
/**
* Entrance of a crawler.<br>
...
...
@@ -74,7 +76,7 @@ public class Spider implements Runnable, Task {
protected
AtomicInteger
stat
=
new
AtomicInteger
(
STAT_INIT
);
protected
boolean
exitWhenComplete
=
fals
e
;
protected
boolean
exitWhenComplete
=
tru
e
;
protected
final
static
int
STAT_INIT
=
0
;
...
...
@@ -82,6 +84,10 @@ public class Spider implements Runnable, Task {
protected
final
static
int
STAT_STOPPED
=
2
;
private
ReentrantLock
newUrlLock
=
new
ReentrantLock
();
private
Condition
newUrlCondition
=
newUrlLock
.
newCondition
();
/**
* create a spider with pageProcessor.
*
...
...
@@ -245,11 +251,15 @@ public class Spider implements Runnable, Task {
if
(
threadAlive
.
get
()
==
0
&&
exitWhenComplete
)
{
break
;
}
// when no request found but some thread is alive, sleep a
// while.
// wait until new url added
try
{
Thread
.
sleep
(
100
);
}
catch
(
InterruptedException
e
)
{
newUrlLock
.
lock
();
try
{
newUrlCondition
.
await
();
}
catch
(
InterruptedException
e
)
{
}
}
finally
{
newUrlLock
.
unlock
();
}
}
else
{
final
Request
requestFinal
=
request
;
...
...
@@ -263,6 +273,7 @@ public class Spider implements Runnable, Task {
logger
.
error
(
"download "
+
requestFinal
+
" error"
,
e
);
}
finally
{
threadAlive
.
decrementAndGet
();
signalNewUrl
();
}
}
});
...
...
@@ -351,11 +362,16 @@ public class Spider implements Runnable, Task {
protected
void
addRequest
(
Page
page
)
{
if
(
CollectionUtils
.
isNotEmpty
(
page
.
getTargetRequests
()))
{
for
(
Request
request
:
page
.
getTargetRequests
())
{
scheduler
.
push
(
request
,
this
);
addRequest
(
request
);
}
}
}
private
void
addRequest
(
Request
request
)
{
scheduler
.
push
(
request
,
this
);
}
protected
void
checkIfRunning
()
{
if
(
stat
.
get
()
==
STAT_RUNNING
)
{
throw
new
IllegalStateException
(
"Spider is already running!"
);
...
...
@@ -368,6 +384,29 @@ public class Spider implements Runnable, Task {
thread
.
start
();
}
/**
* Add urls to crawl.<br/>
*
* @param urls
* @return
*/
public
Spider
addUrl
(
String
...
urls
)
{
for
(
String
url
:
urls
)
{
addRequest
(
new
Request
(
url
));
}
signalNewUrl
();
return
this
;
}
private
void
signalNewUrl
()
{
try
{
newUrlLock
.
lock
();
newUrlCondition
.
signalAll
();
}
finally
{
newUrlLock
.
unlock
();
}
}
public
void
start
()
{
runAsync
();
}
...
...
webmagic-core/src/main/java/us/codecraft/webmagic/processor/example/OschinaBlogPageProcesser.java
View file @
b4fcf411
...
...
@@ -34,6 +34,6 @@ public class OschinaBlogPageProcesser implements PageProcessor {
}
public
static
void
main
(
String
[]
args
)
{
Spider
.
create
(
new
OschinaBlogPageProcesser
()).
thread
(
2
).
run
();
Spider
.
create
(
new
OschinaBlogPageProcesser
()).
thread
(
10
).
run
();
}
}
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