fix a thread pool exception

master
yihua.huang 2013-09-22 23:57:15 +08:00
parent 3c79d031bd
commit fba330872b
3 changed files with 366 additions and 363 deletions

View File

@ -21,22 +21,27 @@ import java.util.concurrent.atomic.AtomicInteger;
/** /**
* Entrance of a crawler.<br> * Entrance of a crawler.<br>
* A spider contains four modules: Downloader, Scheduler, PageProcessor and Pipeline.<br> * A spider contains four modules: Downloader, Scheduler, PageProcessor and
* Every module is a field of Spider. <br> * Pipeline.<br>
* The modules are defined in interface. <br> * Every module is a field of Spider. <br>
* You can customize a spider with various implementations of them. <br> * The modules are defined in interface. <br>
* Examples: <br> * You can customize a spider with various implementations of them. <br>
* Examples: <br>
* <br> * <br>
* A simple crawler: <br> * A simple crawler: <br>
* Spider.create(new SimplePageProcessor("http://my.oschina.net/", "http://my.oschina.net/*blog/*")).run();<br> * Spider.create(new SimplePageProcessor("http://my.oschina.net/",
* "http://my.oschina.net/*blog/*")).run();<br>
* <br> * <br>
* Store results to files by FilePipeline: <br> * Store results to files by FilePipeline: <br>
* Spider.create(new SimplePageProcessor("http://my.oschina.net/", "http://my.oschina.net/*blog/*")) <br> * Spider.create(new SimplePageProcessor("http://my.oschina.net/",
* .pipeline(new FilePipeline("/data/temp/webmagic/")).run(); <br> * "http://my.oschina.net/*blog/*")) <br>
* .pipeline(new FilePipeline("/data/temp/webmagic/")).run(); <br>
* <br> * <br>
* Use FileCacheQueueScheduler to store urls and cursor in files, so that a Spider can resume the status when shutdown. <br> * Use FileCacheQueueScheduler to store urls and cursor in files, so that a
* Spider.create(new SimplePageProcessor("http://my.oschina.net/", "http://my.oschina.net/*blog/*")) <br> * Spider can resume the status when shutdown. <br>
* .scheduler(new FileCacheQueueScheduler("/data/temp/webmagic/cache/")).run(); <br> * Spider.create(new SimplePageProcessor("http://my.oschina.net/",
* "http://my.oschina.net/*blog/*")) <br>
* .scheduler(new FileCacheQueueScheduler("/data/temp/webmagic/cache/")).run(); <br>
* *
* @author code4crafter@gmail.com <br> * @author code4crafter@gmail.com <br>
* @see Downloader * @see Downloader
@ -47,373 +52,381 @@ import java.util.concurrent.atomic.AtomicInteger;
*/ */
public class Spider implements Runnable, Task { public class Spider implements Runnable, Task {
protected Downloader downloader; protected Downloader downloader;
protected List<Pipeline> pipelines = new ArrayList<Pipeline>(); protected List<Pipeline> pipelines = new ArrayList<Pipeline>();
protected PageProcessor pageProcessor; protected PageProcessor pageProcessor;
protected List<String> startUrls; protected List<String> startUrls;
protected Site site; protected Site site;
protected String uuid; protected String uuid;
protected Scheduler scheduler = new QueueScheduler(); protected Scheduler scheduler = new QueueScheduler();
protected Logger logger = Logger.getLogger(getClass()); protected Logger logger = Logger.getLogger(getClass());
protected ExecutorService executorService; protected ExecutorService executorService;
protected int threadNum = 1; protected int threadNum = 1;
protected AtomicInteger stat = new AtomicInteger(STAT_INIT); protected AtomicInteger stat = new AtomicInteger(STAT_INIT);
protected final static int STAT_INIT = 0; protected final static int STAT_INIT = 0;
protected final static int STAT_RUNNING = 1; protected final static int STAT_RUNNING = 1;
protected final static int STAT_STOPPED = 2; protected final static int STAT_STOPPED = 2;
/** /**
* create a spider with pageProcessor. * create a spider with pageProcessor.
* *
* @param pageProcessor * @param pageProcessor
* @return new spider * @return new spider
* @see PageProcessor * @see PageProcessor
*/ */
public static Spider create(PageProcessor pageProcessor) { public static Spider create(PageProcessor pageProcessor) {
return new Spider(pageProcessor); return new Spider(pageProcessor);
} }
/** /**
* create a spider with pageProcessor. * create a spider with pageProcessor.
* *
* @param pageProcessor * @param pageProcessor
*/ */
public Spider(PageProcessor pageProcessor) { public Spider(PageProcessor pageProcessor) {
this.pageProcessor = pageProcessor; this.pageProcessor = pageProcessor;
this.site = pageProcessor.getSite(); this.site = pageProcessor.getSite();
this.startUrls = pageProcessor.getSite().getStartUrls(); this.startUrls = pageProcessor.getSite().getStartUrls();
} }
/** /**
* Set startUrls of Spider.<br> * Set startUrls of Spider.<br>
* Prior to startUrls of Site. * Prior to startUrls of Site.
* *
* @param startUrls * @param startUrls
* @return this * @return this
*/ */
public Spider startUrls(List<String> startUrls) { public Spider startUrls(List<String> startUrls) {
checkIfRunning(); checkIfRunning();
this.startUrls = startUrls; this.startUrls = startUrls;
return this; return this;
} }
/** /**
* Set an uuid for spider.<br> * Set an uuid for spider.<br>
* Default uuid is domain of site.<br> * Default uuid is domain of site.<br>
* *
* @param uuid * @param uuid
* @return this * @return this
*/ */
public Spider setUUID(String uuid) { public Spider setUUID(String uuid) {
this.uuid = uuid; this.uuid = uuid;
return this; return this;
} }
/** /**
* set scheduler for Spider * set scheduler for Spider
* *
* @param scheduler * @param scheduler
* @return this * @return this
* @Deprecated * @Deprecated
* @see #setScheduler(us.codecraft.webmagic.scheduler.Scheduler) * @see #setScheduler(us.codecraft.webmagic.scheduler.Scheduler)
*/ */
public Spider scheduler(Scheduler scheduler) { public Spider scheduler(Scheduler scheduler) {
return setScheduler(scheduler); return setScheduler(scheduler);
} }
/** /**
* set scheduler for Spider * set scheduler for Spider
* *
* @param scheduler * @param scheduler
* @return this * @return this
* @see Scheduler * @see Scheduler
* @since 0.2.1 * @since 0.2.1
*/ */
public Spider setScheduler(Scheduler scheduler) { public Spider setScheduler(Scheduler scheduler) {
checkIfRunning(); checkIfRunning();
this.scheduler = scheduler; this.scheduler = scheduler;
return this; return this;
} }
/** /**
* add a pipeline for Spider * add a pipeline for Spider
* *
* @param pipeline * @param pipeline
* @return this * @return this
* @see #setPipeline(us.codecraft.webmagic.pipeline.Pipeline) * @see #setPipeline(us.codecraft.webmagic.pipeline.Pipeline)
* @deprecated * @deprecated
*/ */
public Spider pipeline(Pipeline pipeline) { public Spider pipeline(Pipeline pipeline) {
return addPipeline(pipeline); return addPipeline(pipeline);
} }
/** /**
* add a pipeline for Spider * add a pipeline for Spider
* *
* @param pipeline * @param pipeline
* @return this * @return this
* @see Pipeline * @see Pipeline
* @since 0.2.1 * @since 0.2.1
*/ */
public Spider addPipeline(Pipeline pipeline) { public Spider addPipeline(Pipeline pipeline) {
checkIfRunning(); checkIfRunning();
this.pipelines.add(pipeline); this.pipelines.add(pipeline);
return this; return this;
} }
/** /**
* clear the pipelines set * clear the pipelines set
* *
* @return this * @return this
*/ */
public Spider clearPipeline() { public Spider clearPipeline() {
pipelines = new ArrayList<Pipeline>(); pipelines = new ArrayList<Pipeline>();
return this; return this;
} }
/** /**
* set the downloader of spider * set the downloader of spider
* *
* @param downloader * @param downloader
* @return this * @return this
* @see #setDownloader(us.codecraft.webmagic.downloader.Downloader) * @see #setDownloader(us.codecraft.webmagic.downloader.Downloader)
* @deprecated * @deprecated
*/ */
public Spider downloader(Downloader downloader) { public Spider downloader(Downloader downloader) {
return setDownloader(downloader); return setDownloader(downloader);
} }
/** /**
* set the downloader of spider * set the downloader of spider
* *
* @param downloader * @param downloader
* @return this * @return this
* @see Downloader * @see Downloader
*/ */
public Spider setDownloader(Downloader downloader) { public Spider setDownloader(Downloader downloader) {
checkIfRunning(); checkIfRunning();
this.downloader = downloader; this.downloader = downloader;
return this; return this;
} }
protected void checkComponent() { protected void checkComponent() {
if (downloader == null) { if (downloader == null) {
this.downloader = new HttpClientDownloader(); this.downloader = new HttpClientDownloader();
} }
if (pipelines.isEmpty()) { if (pipelines.isEmpty()) {
pipelines.add(new ConsolePipeline()); pipelines.add(new ConsolePipeline());
} }
downloader.setThread(threadNum); downloader.setThread(threadNum);
} }
@Override @Override
public void run() { public void run() {
if (!stat.compareAndSet(STAT_INIT, STAT_RUNNING) if (!stat.compareAndSet(STAT_INIT, STAT_RUNNING) && !stat.compareAndSet(STAT_STOPPED, STAT_RUNNING)) {
&& !stat.compareAndSet(STAT_STOPPED, STAT_RUNNING)) { throw new IllegalStateException("Spider is already running!");
throw new IllegalStateException("Spider is already running!"); }
} checkComponent();
checkComponent(); if (startUrls != null) {
if (startUrls != null) { for (String startUrl : startUrls) {
for (String startUrl : startUrls) { scheduler.push(new Request(startUrl), this);
scheduler.push(new Request(startUrl), this); }
} startUrls.clear();
startUrls.clear(); }
} Request request = scheduler.poll(this);
Request request = scheduler.poll(this); logger.info("Spider " + getUUID() + " started!");
//single thread // single thread
if (threadNum <= 1) { if (threadNum <= 1) {
while (request != null && stat.compareAndSet(STAT_RUNNING, STAT_RUNNING)) { while (request != null && stat.compareAndSet(STAT_RUNNING, STAT_RUNNING)) {
processRequest(request); processRequest(request);
request = scheduler.poll(this); request = scheduler.poll(this);
} }
} else { } else {
synchronized (this) { synchronized (this) {
this.executorService = ThreadUtils.newFixedThreadPool(threadNum); this.executorService = ThreadUtils.newFixedThreadPool(threadNum);
} }
//multi thread // multi thread
final AtomicInteger threadAlive = new AtomicInteger(0); final AtomicInteger threadAlive = new AtomicInteger(0);
while (true && stat.compareAndSet(STAT_RUNNING, STAT_RUNNING)) { while (true && stat.compareAndSet(STAT_RUNNING, STAT_RUNNING)) {
if (request == null) { if (request == null) {
//when no request found but some thread is alive, sleep a while. // when no request found but some thread is alive, sleep a
try { // while.
Thread.sleep(100); try {
} catch (InterruptedException e) { Thread.sleep(100);
} } catch (InterruptedException e) {
} else { }
final Request requestFinal = request; } else {
threadAlive.incrementAndGet(); final Request requestFinal = request;
executorService.execute(new Runnable() { threadAlive.incrementAndGet();
@Override executorService.execute(new Runnable() {
public void run() { @Override
processRequest(requestFinal); public void run() {
threadAlive.decrementAndGet(); processRequest(requestFinal);
} threadAlive.decrementAndGet();
}); }
} });
request = scheduler.poll(this); }
if (threadAlive.get() == 0) { request = scheduler.poll(this);
request = scheduler.poll(this); if (threadAlive.get() == 0) {
if (request == null) { request = scheduler.poll(this);
break; if (request == null) {
} break;
} }
} }
executorService.shutdown(); }
} executorService.shutdown();
stat.compareAndSet(STAT_RUNNING, STAT_STOPPED); }
//release some resources stat.compareAndSet(STAT_RUNNING, STAT_STOPPED);
destroy(); // release some resources
} destroy();
}
protected void destroy() { protected void destroy() {
destroyEach(downloader); destroyEach(downloader);
destroyEach(pageProcessor); destroyEach(pageProcessor);
for (Pipeline pipeline : pipelines) { for (Pipeline pipeline : pipelines) {
destroyEach(pipeline); destroyEach(pipeline);
} }
} }
private void destroyEach(Object object) { private void destroyEach(Object object) {
if (object instanceof Closeable) { if (object instanceof Closeable) {
try { try {
((Closeable) object).close(); ((Closeable) object).close();
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
} }
} }
/** /**
* Process specific urls without url discovering. * Process specific urls without url discovering.
* *
* @param urls urls to process * @param urls
*/ * urls to process
public void test(String... urls) { */
checkComponent(); public void test(String... urls) {
if (urls.length > 0) { checkComponent();
for (String url : urls) { if (urls.length > 0) {
processRequest(new Request(url)); for (String url : urls) {
} processRequest(new Request(url));
} }
} }
}
protected void processRequest(Request request) { protected void processRequest(Request request) {
Page page = downloader.download(request, this); Page page = downloader.download(request, this);
if (page == null) { if (page == null) {
sleep(site.getSleepTime()); sleep(site.getSleepTime());
return; return;
} }
//for cycle retry // for cycle retry
if (page.getHtml() == null) { if (page.getHtml() == null) {
addRequest(page); addRequest(page);
sleep(site.getSleepTime()); sleep(site.getSleepTime());
return; return;
} }
pageProcessor.process(page); pageProcessor.process(page);
addRequest(page); addRequest(page);
if (!page.getResultItems().isSkip()) { if (!page.getResultItems().isSkip()) {
for (Pipeline pipeline : pipelines) { for (Pipeline pipeline : pipelines) {
pipeline.process(page.getResultItems(), this); pipeline.process(page.getResultItems(), this);
} }
} }
sleep(site.getSleepTime()); sleep(site.getSleepTime());
} }
protected void sleep(int time) { protected void sleep(int time) {
try { try {
Thread.sleep(time); Thread.sleep(time);
} catch (InterruptedException e) { } catch (InterruptedException e) {
e.printStackTrace(); e.printStackTrace();
} }
} }
protected void addRequest(Page page) { protected void addRequest(Page page) {
if (CollectionUtils.isNotEmpty(page.getTargetRequests())) { if (CollectionUtils.isNotEmpty(page.getTargetRequests())) {
for (Request request : page.getTargetRequests()) { for (Request request : page.getTargetRequests()) {
scheduler.push(request, this); scheduler.push(request, this);
} }
} }
} }
protected void checkIfRunning() { protected void checkIfRunning() {
if (!stat.compareAndSet(STAT_INIT, STAT_INIT) && !stat.compareAndSet(STAT_STOPPED, STAT_STOPPED)) { if (!stat.compareAndSet(STAT_INIT, STAT_INIT) && !stat.compareAndSet(STAT_STOPPED, STAT_STOPPED)) {
throw new IllegalStateException("Spider is already running!"); throw new IllegalStateException("Spider is already running!");
} }
} }
public void runAsync() { public void runAsync() {
Thread thread = new Thread(this); Thread thread = new Thread(this);
thread.setDaemon(false); thread.setDaemon(false);
thread.start(); thread.start();
} }
public void start() { public void start() {
runAsync(); runAsync();
} }
public void stop() { public void stop() {
stat.compareAndSet(STAT_RUNNING, STAT_STOPPED); if (stat.compareAndSet(STAT_RUNNING, STAT_STOPPED)) {
executorService.shutdown(); if (executorService != null) {
} executorService.shutdown();
}
logger.info("Spider " + getUUID() + " stop success!");
} else {
logger.info("Spider " + getUUID() + " stop fail!");
}
}
public void stopAndDestroy() { public void stopAndDestroy() {
stop(); stop();
destroy(); destroy();
} }
/** /**
* start with more than one threads * start with more than one threads
* *
* @param threadNum * @param threadNum
* @return this * @return this
*/ */
public Spider thread(int threadNum) { public Spider thread(int threadNum) {
checkIfRunning(); checkIfRunning();
this.threadNum = threadNum; this.threadNum = threadNum;
if (threadNum <= 0) { if (threadNum <= 0) {
throw new IllegalArgumentException("threadNum should be more than one!"); throw new IllegalArgumentException("threadNum should be more than one!");
} }
if (threadNum == 1) { if (threadNum == 1) {
return this; return this;
} }
return this; return this;
} }
/** /**
* switch off xsoup * switch off xsoup
* *
* @return * @return
*/ */
public static void xsoupOff() { public static void xsoupOff() {
EnvironmentUtil.setUseXsoup(false); EnvironmentUtil.setUseXsoup(false);
} }
@Override @Override
public String getUUID() { public String getUUID() {
if (uuid != null) { if (uuid != null) {
return uuid; return uuid;
} }
if (site != null) { if (site != null) {
return site.getDomain(); return site.getDomain();
} }
return null; return null;
} }
@Override @Override
public Site getSite() { public Site getSite() {
return site; return site;
} }
} }

View File

@ -1,7 +1,7 @@
package us.codecraft.webmagic.utils; package us.codecraft.webmagic.utils;
import java.util.concurrent.ExecutorService; import java.util.concurrent.ExecutorService;
import java.util.concurrent.LinkedBlockingQueue; import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.ThreadPoolExecutor; import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
@ -11,22 +11,11 @@ import java.util.concurrent.TimeUnit;
*/ */
public class ThreadUtils { public class ThreadUtils {
public static ExecutorService newFixedThreadPool(int threadSize) { public static ExecutorService newFixedThreadPool(int threadSize) {
return new ThreadPoolExecutor(threadSize, threadSize, 0L, TimeUnit.MILLISECONDS, if (threadSize <= 1) {
new LinkedBlockingQueue<Runnable>(1) { throw new IllegalArgumentException("ThreadSize must be greater than 1!");
}
private static final long serialVersionUID = -9028058603126367678L; return new ThreadPoolExecutor(threadSize - 1, threadSize - 1, 0L, TimeUnit.MILLISECONDS,
new SynchronousQueue<Runnable>(), new ThreadPoolExecutor.CallerRunsPolicy());
@Override }
public boolean offer(Runnable e) {
try {
put(e);
return true;
} catch (InterruptedException ie) {
Thread.currentThread().interrupt();
}
return false;
}
});
}
} }

View File

@ -18,11 +18,12 @@ public class SpiderTest {
public void process(ResultItems resultItems, Task task) { public void process(ResultItems resultItems, Task task) {
System.out.println(1); System.out.println(1);
} }
}); }).thread(2);
spider.start(); spider.start();
Thread.sleep(10000); Thread.sleep(10000);
spider.stop(); spider.stop();
// spider.run(); Thread.sleep(10000);
spider.start();
Thread.sleep(10000); Thread.sleep(10000);
} }
} }