Heritrix源码分析(十三) Heritrix的控制中心(大脑)CrawlController(二)
转自:http://guoyunsky.iteye.com/blog/650744
1.Heritrix的初始化:
CrawlController的初始化实际上是创建与之相关的各个不见,如日志文件、BDB、处理器、统计跟踪器等。其中日志文件用于记录各个日志、BDB是个嵌入式数据库用于存放URL以及备份数据、处理器之前的博客有介绍、统计跟踪器则主要用来统计跟踪抓取情况,如多少URL被抓取等。由于这些并不复杂,里就不再陈述...
2.Heritrix的启动:/** * Called when the last toethread exits. * 当没有活动状态的线程,则爬虫终止 * */ protected void completeStop() { LOGGER.fine("Entered complete stop."); runProcessorFinalTasks();// 运行所有处理器的最后处理环节 // Ok, now we are ready to exit. sendCrawlStateChangeEvent(FINISHED, this.sExit); // 发送事件表明爬虫结束 synchronized (this.registeredCrawlStatusListeners) { this.registeredCrawlStatusListeners .removeAll(this.registeredCrawlStatusListeners);//移除所有的监听事件 this.registeredCrawlStatusListeners = null; } closeLogFiles();// 关闭所有的日志处理器 // Release reference to logger file handler instances. this.fileHandlers = null; this.uriErrors = null; this.uriProcessing = null; this.localErrors = null; this.runtimeErrors = null; this.progressStats = null; this.reports = null; this.manifest = null; // Do cleanup. this.statistics = null; this.frontier = null; this.disk = null; this.scratchDisk = null; this.order = null; this.scope = null; if (this.settingsHandler != null) { this.settingsHandler.cleanup(); } this.settingsHandler = null; this.reserveMemory = null; this.processorChains = null; if (this.serverCache != null) { this.serverCache.cleanup(); this.serverCache = null; } if (this.checkpointer != null) { // 关闭Checkpointer,这里是关闭CheckPointer定时器 this.checkpointer.cleanup(); this.checkpointer = null; } if (this.bdbEnvironment != null) { // 关闭BDB数据库 try { this.bdbEnvironment.sync(); this.bdbEnvironment.close(); } catch (DatabaseException e) { e.printStackTrace(); } this.bdbEnvironment = null; } this.bigmaps = null; if (this.toePool != null) { this.toePool.cleanup();// 清理线程 } this.toePool = null; LOGGER.fine("Finished crawl."); }
可以看到这段代码是一个清理的过程,同时触发这个方法的缘由是因为没有活动状态的线程.之前说过当Heritrix中的抓取线程ToeThread没有URL可抓取的时候就会自然死亡(不处于活动状态),当没有URL可抓取也就是意味着没有处于活动状态的线程,所以就会触发这个方法,导致抓取的结束。
6.总结:
以上只是介绍了主要的方法和属性,其中还有一些重要的组件没有介绍,如Checkpoint,如果不去看源码可能根本不会注意到Checkpoint。不过以上的介绍大概说明了下Heritrix,你也可以通过这些方法调试一步步深入下去...