add worker as container
parent
6577a75892
commit
6201fd6966
@ -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);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue