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
2bb6f847
Commit
2bb6f847
authored
Jul 26, 2013
by
yihua.huang
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix redis null pointer exception
parent
2c97dd90
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
6 additions
and
30 deletions
+6
-30
RedisScheduler.java
.../java/us/codecraft/webmagic/scheduler/RedisScheduler.java
+6
-30
No files found.
webmagic-plugin/webmagic-misc/src/main/java/us/codecraft/webmagic/scheduler/RedisScheduler.java
View file @
2bb6f847
...
...
@@ -7,9 +7,6 @@ import us.codecraft.webmagic.Request;
import
us.codecraft.webmagic.Task
;
import
us.codecraft.webmagic.schedular.Scheduler
;
import
java.util.concurrent.locks.Condition
;
import
java.util.concurrent.locks.ReentrantLock
;
/**
* 使用redis管理url,构建一个分布式的爬虫。<br>
*
...
...
@@ -25,10 +22,6 @@ public class RedisScheduler implements Scheduler {
private
static
final
String
SET_PREFIX
=
"set_"
;
private
ReentrantLock
lock
=
new
ReentrantLock
();
private
Condition
condition
=
lock
.
newCondition
();
public
RedisScheduler
(
String
host
)
{
pool
=
new
JedisPool
(
new
JedisPoolConfig
(),
host
);
}
...
...
@@ -38,15 +31,9 @@ public class RedisScheduler implements Scheduler {
Jedis
jedis
=
pool
.
getResource
();
//使用SortedSet进行url去重
if
(
jedis
.
zrank
(
SET_PREFIX
+
task
.
getUUID
(),
request
.
getUrl
())
==
null
)
{
try
{
lock
.
lock
();
//使用List保存队列
jedis
.
rpush
(
QUEUE_PREFIX
+
task
.
getUUID
(),
request
.
getUrl
());
jedis
.
zadd
(
SET_PREFIX
+
task
.
getUUID
(),
System
.
currentTimeMillis
(),
request
.
getUrl
());
condition
.
signal
();
}
finally
{
lock
.
unlock
();
}
//使用List保存队列
jedis
.
rpush
(
QUEUE_PREFIX
+
task
.
getUUID
(),
request
.
getUrl
());
jedis
.
zadd
(
SET_PREFIX
+
task
.
getUUID
(),
System
.
currentTimeMillis
(),
request
.
getUrl
());
}
pool
.
returnResource
(
jedis
);
}
...
...
@@ -55,21 +42,10 @@ public class RedisScheduler implements Scheduler {
public
synchronized
Request
poll
(
Task
task
)
{
Jedis
jedis
=
pool
.
getResource
();
String
url
=
jedis
.
lpop
(
QUEUE_PREFIX
+
task
.
getUUID
());
if
(
url
==
null
)
{
try
{
lock
.
lock
();
while
(
url
==
null
)
{
try
{
condition
.
await
();
url
=
jedis
.
lpop
(
QUEUE_PREFIX
+
task
.
getUUID
());
}
catch
(
InterruptedException
e
)
{
}
}
}
finally
{
lock
.
unlock
();
}
}
pool
.
returnResource
(
jedis
);
if
(
url
==
null
){
return
null
;
}
return
new
Request
(
url
);
}
}
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