使用TimerTask时,crawler4j无法正常工作

时间:2014-11-20 07:23:24

标签: java timer timertask crawler4j

我们一直在尝试使用抓取工具,以便我们可以按特定时间间隔抓取特定网站。为此,我们一直在尝试将爬虫纳入计时器。但是在第一次使用计时器成功爬行之后,它总是在控制台中说:

看起来没有线程在工作,等待10秒钟以确保...... 没有线程正在工作,没有更多的URL在队列中等待另外10秒以确保... 所有抓取工具都已停止。完成过程...... 在最后清理之前等待10秒...... CrawlerScheduler结束于:11月19日星期三18:41:36 IST 2014

使用计时器进行每次后续抓取。爬虫无法再次运行。我们通过源代码找出原因,但失败了。

这是代码:

public class CrawlerScheduler扩展了TimerTask {

@Override
public void run() {
    try {
        System.out.println("CrawlerScheduler started at:"+new Date());
        int numberOfCrawlers = 1;
        String crawlStorageFolder = ".";       
        CrawlConfig crawlConfig = new CrawlConfig();
        crawlConfig.setCrawlStorageFolder(crawlStorageFolder);
        PageFetcher pageFetcher = new PageFetcher(crawlConfig);       
        RobotstxtConfig robotstxtConfig = new RobotstxtConfig();
        RobotstxtServer robotstxtServer = new RobotstxtServer(robotstxtConfig, pageFetcher);
        CrawlController controller = new CrawlController(crawlConfig, pageFetcher, robotstxtServer);       
        controller.addSeed("http://wwwnc.cdc.gov/travel/destinations/list");       
        controller.start(Crawler.class, numberOfCrawlers);           
        System.out.println("CrawlerScheduler finished at:"+new Date());
    } catch (Exception ex) {
        Logger.getLogger(CrawlerScheduler.class.getName()).log(Level.SEVERE, null, ex);
    }
}

public static void main(String ar[]){
    TimerTask timerTask = new CrawlerScheduler();
    Timer timer = new Timer();
    timer.scheduleAtFixedRate(timerTask,10,6*60*1000); 
    try {
        Thread.sleep(3000);
    } catch (InterruptedException ex) {
        Logger.getLogger(CrawlerScheduler.class.getName()).log(Level.SEVERE, null, ex);
    }
}

}

0 个答案:

没有答案