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
6201fd69
Commit
6201fd69
authored
Mar 17, 2014
by
yihua.huang
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add worker as container
parent
6577a758
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
90 additions
and
0 deletions
+90
-0
pom.xml
webmagic-avalon/webmagic-worker/pom.xml
+12
-0
Worker.java
...er/src/main/java/us/codecraft/webmagic/worker/Worker.java
+46
-0
WorkerTest.java
...rc/test/java/us/codecraft/webmagic/worker/WorkerTest.java
+27
-0
Spider.java
...agic-core/src/main/java/us/codecraft/webmagic/Spider.java
+5
-0
No files found.
webmagic-avalon/webmagic-worker/pom.xml
View file @
6201fd69
...
...
@@ -16,6 +16,18 @@
<artifactId>
webmagic-avalon-common
</artifactId>
<version>
${project.version}
</version>
</dependency>
<dependency>
<groupId>
junit
</groupId>
<artifactId>
junit
</artifactId>
</dependency>
<dependency>
<groupId>
org.mockito
</groupId>
<artifactId>
mockito-all
</artifactId>
</dependency>
<dependency>
<groupId>
org.aspectj
</groupId>
<artifactId>
aspectjrt
</artifactId>
</dependency>
</dependencies>
<build>
...
...
webmagic-avalon/webmagic-worker/src/main/java/us/codecraft/webmagic/worker/Worker.java
0 → 100644
View file @
6201fd69
package
us
.
codecraft
.
webmagic
.
worker
;
import
us.codecraft.webmagic.Spider
;
import
us.codecraft.webmagic.utils.ThreadUtils
;
import
java.util.Map
;
import
java.util.concurrent.ConcurrentHashMap
;
import
java.util.concurrent.ExecutorService
;
/**
* @author code4crafter@gmail.com
*/
public
class
Worker
{
public
static
final
int
DEFAULT_POOL_SIZE
=
10
;
private
int
poolSize
;
private
ExecutorService
executorService
;
private
Map
<
String
,
Spider
>
spiderMap
;
public
Worker
(
int
poolSize
)
{
this
.
poolSize
=
poolSize
;
this
.
executorService
=
initExecutorService
();
this
.
spiderMap
=
new
ConcurrentHashMap
<
String
,
Spider
>();
}
public
Worker
()
{
this
(
DEFAULT_POOL_SIZE
);
}
protected
ExecutorService
initExecutorService
()
{
return
ThreadUtils
.
newFixedThreadPool
(
poolSize
);
}
public
void
addSpider
(
Spider
spider
)
{
spider
.
setExecutorService
(
executorService
);
spiderMap
.
put
(
spider
.
getUUID
(),
spider
);
}
public
Spider
getSpider
(
String
uuid
){
return
spiderMap
.
get
(
uuid
);
}
}
webmagic-avalon/webmagic-worker/src/test/java/us/codecraft/webmagic/worker/WorkerTest.java
0 → 100644
View file @
6201fd69
package
us
.
codecraft
.
webmagic
.
worker
;
import
org.junit.Test
;
import
us.codecraft.webmagic.Site
;
import
us.codecraft.webmagic.Spider
;
import
us.codecraft.webmagic.processor.PageProcessor
;
import
static
org
.
assertj
.
core
.
api
.
Assertions
.
assertThat
;
import
static
org
.
mockito
.
Mockito
.*;
/**
* @author code4crafter@gmail.com
*/
public
class
WorkerTest
{
@Test
public
void
testWorkerAsSpiderContains
()
throws
Exception
{
PageProcessor
pageProcessor
=
mock
(
PageProcessor
.
class
);
Site
site
=
mock
(
Site
.
class
);
when
(
pageProcessor
.
getSite
()).
thenReturn
(
site
);
when
(
site
.
getDomain
()).
thenReturn
(
"codecraft.us"
);
Worker
worker
=
new
Worker
();
Spider
spider
=
Spider
.
create
(
pageProcessor
);
worker
.
addSpider
(
spider
);
assertThat
(
worker
.
getSpider
(
"codecraft.us"
)).
isEqualTo
(
spider
);
}
}
webmagic-core/src/main/java/us/codecraft/webmagic/Spider.java
View file @
6201fd69
...
...
@@ -643,6 +643,11 @@ public class Spider implements Runnable, Task {
return
uuid
;
}
public
Spider
setExecutorService
(
ExecutorService
executorService
)
{
this
.
executorService
=
executorService
;
return
this
;
}
@Override
public
Site
getSite
()
{
return
site
;
...
...
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