webmagic icon indicating copy to clipboard operation
webmagic copied to clipboard

FileCacheQueueScheduler中游标的更新不准确

Open nobrowning opened this issue 7 years ago • 1 comments

使用FileCacheQueueScheduler时,可能会出现游标的值比URL的数量要大的情况,查看了一下源代码,发现问题在这里, `@Override

public synchronized Request poll(Task task) {

    if (!inited.get()) {

        init(task);

    }

    fileCursorWriter.println(cursor.incrementAndGet());

    return queue.poll();

}`

在public synchronized Request poll(Task task)方法中,每次queue.poll()前都要将游标自增1,但是queue.poll()得到可能是null,这就导致了游标比文件中URL数量要大的情况,这在多线程的时候很明显。 我觉得应该加一个非空判断更准确: `public synchronized Request poll(Task task) {

    if(!this.inited.get()) {

        this.init(task);

    }

    if (!queue.isEmpty()){

        fileCursorWriter.println(cursor.incrementAndGet());

        return queue.poll();

    }

   return null;

}`

另外,我觉得在poll的时候没有必要使用PrintWriter去写游标的值,在每次flush前写一下就行了吧

nobrowning avatar Mar 13 '18 06:03 nobrowning

这玩意到现在也没解决。。

lomoye avatar Jul 20 '21 11:07 lomoye