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
2c97dd90
Commit
2c97dd90
authored
Jul 26, 2013
by
yihua.huang
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix redisScheduler thread problem
parent
95ba7846
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
50 additions
and
8 deletions
+50
-8
RedisScheduler.java
.../java/us/codecraft/webmagic/scheduler/RedisScheduler.java
+35
-7
SeleniumTest.java
...est/java/us/codecraft/webmagic/selenium/SeleniumTest.java
+15
-1
No files found.
webmagic-plugin/webmagic-misc/src/main/java/us/codecraft/webmagic/scheduler/RedisScheduler.java
View file @
2c97dd90
...
...
@@ -7,13 +7,17 @@ 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>
*
* @author yihua.huang@dianping.com <br>
* @date: 13-7-25 <br>
* Time: 上午7:07 <br>
*/
public
class
RedisScheduler
implements
Scheduler
{
public
class
RedisScheduler
implements
Scheduler
{
private
JedisPool
pool
;
...
...
@@ -21,7 +25,11 @@ public class RedisScheduler implements Scheduler{
private
static
final
String
SET_PREFIX
=
"set_"
;
public
RedisScheduler
(
String
host
){
private
ReentrantLock
lock
=
new
ReentrantLock
();
private
Condition
condition
=
lock
.
newCondition
();
public
RedisScheduler
(
String
host
)
{
pool
=
new
JedisPool
(
new
JedisPoolConfig
(),
host
);
}
...
...
@@ -29,10 +37,16 @@ public class RedisScheduler implements Scheduler{
public
synchronized
void
push
(
Request
request
,
Task
task
)
{
Jedis
jedis
=
pool
.
getResource
();
//使用SortedSet进行url去重
if
(
jedis
.
zrank
(
SET_PREFIX
+
task
.
getUUID
(),
request
.
getUrl
())==
null
){
//使用List保存队列
jedis
.
rpush
(
QUEUE_PREFIX
+
task
.
getUUID
(),
request
.
getUrl
());
jedis
.
zadd
(
SET_PREFIX
+
task
.
getUUID
(),
System
.
currentTimeMillis
(),
request
.
getUrl
());
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
();
}
}
pool
.
returnResource
(
jedis
);
}
...
...
@@ -40,7 +54,21 @@ public class RedisScheduler implements Scheduler{
@Override
public
synchronized
Request
poll
(
Task
task
)
{
Jedis
jedis
=
pool
.
getResource
();
String
url
=
jedis
.
lpop
(
QUEUE_PREFIX
+
task
.
getUUID
());
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
);
return
new
Request
(
url
);
}
...
...
webmagic-plugin/webmagic-selenium/src/test/java/us/codecraft/webmagic/selenium/SeleniumTest.java
View file @
2c97dd90
...
...
@@ -6,6 +6,11 @@ import org.openqa.selenium.By;
import
org.openqa.selenium.WebDriver
;
import
org.openqa.selenium.WebElement
;
import
org.openqa.selenium.chrome.ChromeDriver
;
import
org.openqa.selenium.remote.DesiredCapabilities
;
import
java.util.Arrays
;
import
java.util.HashMap
;
import
java.util.Map
;
/**
* @author yihua.huang@dianping.com <br>
...
...
@@ -18,7 +23,16 @@ public class SeleniumTest {
@Test
public
void
testSelenium
()
{
System
.
getProperties
().
setProperty
(
"webdriver.chrome.driver"
,
"/Users/yihua/Downloads/chromedriver"
);
WebDriver
webDriver
=
new
ChromeDriver
();
Map
<
String
,
Object
>
contentSettings
=
new
HashMap
<
String
,
Object
>();
contentSettings
.
put
(
"images"
,
2
);
Map
<
String
,
Object
>
preferences
=
new
HashMap
<
String
,
Object
>();
preferences
.
put
(
"profile.default_content_settings"
,
contentSettings
);
DesiredCapabilities
caps
=
DesiredCapabilities
.
chrome
();
caps
.
setCapability
(
"chrome.prefs"
,
preferences
);
caps
.
setCapability
(
"chrome.switches"
,
Arrays
.
asList
(
"--user-data-dir=/Users/yihua/temp/chrome"
));
WebDriver
webDriver
=
new
ChromeDriver
(
caps
);
webDriver
.
get
(
"http://huaban.com/"
);
WebElement
webElement
=
webDriver
.
findElement
(
By
.
xpath
(
"/html"
));
System
.
out
.
println
(
webElement
.
getAttribute
(
"outerHTML"
));
...
...
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