-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
53 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 30 additions & 5 deletions
35
webmagic-core/src/main/java/us/codecraft/webmagic/scheduler/QueueScheduler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,51 @@ | ||
package us.codecraft.webmagic.scheduler; | ||
|
||
import us.codecraft.webmagic.Request; | ||
import us.codecraft.webmagic.Task; | ||
|
||
import java.util.concurrent.BlockingQueue; | ||
import java.util.concurrent.LinkedBlockingQueue; | ||
|
||
import us.codecraft.webmagic.Request; | ||
import us.codecraft.webmagic.Site; | ||
import us.codecraft.webmagic.Task; | ||
|
||
/** | ||
* Basic Scheduler implementation.<br> | ||
* Store urls to fetch in LinkedBlockingQueue and remove duplicate urls by HashMap. | ||
* | ||
* Note: if you use this {@link QueueScheduler} | ||
* with {@link Site#getCycleRetryTimes()} enabled, you may encountered dead-lock | ||
* when the queue is full. | ||
* | ||
* @author [email protected] <br> | ||
* @since 0.1.0 | ||
*/ | ||
public class QueueScheduler extends DuplicateRemovedScheduler implements MonitorableScheduler { | ||
|
||
private BlockingQueue<Request> queue = new LinkedBlockingQueue<Request>(); | ||
private final BlockingQueue<Request> queue; | ||
|
||
public QueueScheduler() { | ||
this.queue = new LinkedBlockingQueue<>(); | ||
} | ||
|
||
/** | ||
* Creates a {@code QueueScheduler} with the given (fixed) capacity. | ||
* | ||
* @param capacity the capacity of this queue, | ||
* see {@link LinkedBlockingQueue#LinkedBlockingQueue(int)} | ||
* @since 0.8.0 | ||
*/ | ||
public QueueScheduler(int capacity) { | ||
this.queue = new LinkedBlockingQueue<>(capacity); | ||
} | ||
|
||
@Override | ||
public void pushWhenNoDuplicate(Request request, Task task) { | ||
queue.add(request); | ||
logger.trace("Remaining capacity: {}", this.queue.remainingCapacity()); | ||
|
||
try { | ||
queue.put(request); | ||
} catch (InterruptedException e) { | ||
Thread.currentThread().interrupt(); | ||
} | ||
} | ||
|
||
@Override | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters