SpiderImpl.java
上传用户:qing5858
上传日期:2015-10-27
资源大小:6056k
文件大小:4k
源码类别:

搜索引擎

开发平台:

Java

  1. package net.javacoding.jspider.core.impl;
  2. import net.javacoding.jspider.api.event.engine.*;
  3. import net.javacoding.jspider.core.Spider;
  4. import net.javacoding.jspider.core.SpiderContext;
  5. import net.javacoding.jspider.core.logging.Log;
  6. import net.javacoding.jspider.core.logging.LogFactory;
  7. import net.javacoding.jspider.core.task.dispatch.DispatchSpiderTasks;
  8. import net.javacoding.jspider.core.task.dispatch.DispatchThinkerTasks;
  9. import net.javacoding.jspider.core.threading.ThreadPoolMonitorThread;
  10. import net.javacoding.jspider.core.threading.WorkerThreadPool;
  11. import net.javacoding.jspider.core.util.config.*;
  12. /**
  13.  *
  14.  * $Id: SpiderImpl.java,v 1.18 2003/04/02 20:55:06 vanrogu Exp $
  15.  *
  16.  * @author G黱ther Van Roey
  17.  */
  18. public class SpiderImpl implements Spider {
  19.     public static final int DEFAULT_MONITORING_INTERVAL = 1000;
  20.     protected WorkerThreadPool spiders;
  21.     protected WorkerThreadPool thinkers;
  22.     public SpiderImpl(SpiderContext context, int spiderThreads, int thinkerThreads) {
  23.         LogFactory.getLog(Spider.class).info("Spider born - threads: spiders: " + spiderThreads + ", thinkers: " + thinkerThreads);
  24.         spiders = new WorkerThreadPool("Spiders", "Spider", spiderThreads);
  25.         thinkers = new WorkerThreadPool("Thinkers", "Thinker", thinkerThreads);
  26.         PropertySet props = ConfigurationFactory.getConfiguration().getJSpiderConfiguration();
  27.         PropertySet threadProps = new MappedPropertySet ( ConfigConstants.CONFIG_THREADING, props);
  28.         PropertySet spidersProps = new MappedPropertySet ( ConfigConstants.CONFIG_THREADING_SPIDERS, threadProps);
  29.         PropertySet thinkerProps = new MappedPropertySet ( ConfigConstants.CONFIG_THREADING_THINKERS, threadProps);
  30.         PropertySet spidersMonitoringProps = new MappedPropertySet ( ConfigConstants.CONFIG_THREADING_MONITORING, spidersProps);
  31.         PropertySet thinkerMonitoringProps = new MappedPropertySet ( ConfigConstants.CONFIG_THREADING_MONITORING, thinkerProps);
  32.         if (spidersMonitoringProps.getBoolean(ConfigConstants.CONFIG_THREADING_MONITORING_ENABLED, false)) {
  33.             int interval = spidersMonitoringProps.getInteger(ConfigConstants.CONFIG_THREADING_MONITORING_INTERVAL, DEFAULT_MONITORING_INTERVAL);
  34.             new ThreadPoolMonitorThread(context.getEventDispatcher(), interval, spiders);
  35.         }
  36.         if (thinkerMonitoringProps.getBoolean(ConfigConstants.CONFIG_THREADING_MONITORING_ENABLED, false)) {
  37.             int interval = thinkerMonitoringProps.getInteger(ConfigConstants.CONFIG_THREADING_MONITORING_INTERVAL, DEFAULT_MONITORING_INTERVAL);
  38.             new ThreadPoolMonitorThread(context.getEventDispatcher(), interval, thinkers);
  39.         }
  40.     }
  41.     public void crawl(SpiderContext context) {
  42.         long start = System.currentTimeMillis();
  43.         context.getEventDispatcher().dispatch(new SpideringStartedEvent(context.getBaseURL()));
  44.         DispatchSpiderTasks dispatchSpiderTask = new DispatchSpiderTasks(spiders, context);
  45.         DispatchThinkerTasks dispatchThinkerTask = new DispatchThinkerTasks(thinkers, context);
  46.         synchronized (dispatchSpiderTask) {
  47.             context.getAgent().start();
  48.             spiders.assignGroupTask(dispatchSpiderTask);
  49.             thinkers.assignGroupTask(dispatchThinkerTask);
  50.             try {
  51.                 // now wait for the spidering to be ended.
  52.                 dispatchSpiderTask.wait();
  53.             } catch (InterruptedException e) {
  54.                 Thread.currentThread().interrupt();
  55.             }
  56.         }
  57.         Log log = LogFactory.getLog(Spider.class);
  58.         log.debug("Stopping spider workers...");
  59.         spiders.stopAll();
  60.         log.info("Stopped spider workers...");
  61.         log.debug("Stopping thinker workers...");
  62.         thinkers.stopAll();
  63.         log.info("Stopped thinker workers...");
  64.         context.getEventDispatcher().dispatch(new SpideringSummaryEvent(context.getStorage().getSummary()));
  65.         context.getEventDispatcher().dispatch(new SpideringStoppedEvent(context.getStorage()));
  66.         context.getEventDispatcher().shutdown();
  67.         log.info("Spidering done!");
  68.         log.info("Elapsed time : " + (System.currentTimeMillis() - start));
  69.     }
  70. }