add worker as container
parent
6577a75892
commit
6201fd6966
|
@ -16,6 +16,18 @@
|
||||||
<artifactId>webmagic-avalon-common</artifactId>
|
<artifactId>webmagic-avalon-common</artifactId>
|
||||||
<version>${project.version}</version>
|
<version>${project.version}</version>
|
||||||
</dependency>
|
</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>
|
</dependencies>
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
|
|
|
@ -0,0 +1,46 @@
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,27 @@
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
|
@ -643,6 +643,11 @@ public class Spider implements Runnable, Task {
|
||||||
return uuid;
|
return uuid;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Spider setExecutorService(ExecutorService executorService) {
|
||||||
|
this.executorService = executorService;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Site getSite() {
|
public Site getSite() {
|
||||||
return site;
|
return site;
|
||||||
|
|
Loading…
Reference in New Issue