Skip to content

Commit ac912e8

Browse files
committed
Revise QueueScheduler to support capacity-restricted.
1 parent 5d5f3bf commit ac912e8

File tree

1 file changed

+21
-2
lines changed

1 file changed

+21
-2
lines changed

webmagic-core/src/main/java/us/codecraft/webmagic/scheduler/QueueScheduler.java

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,30 @@
1616
*/
1717
public class QueueScheduler extends DuplicateRemovedScheduler implements MonitorableScheduler {
1818

19-
private BlockingQueue<Request> queue = new LinkedBlockingQueue<Request>();
19+
private final BlockingQueue<Request> queue;
20+
21+
public QueueScheduler() {
22+
this.queue = new LinkedBlockingQueue<>();
23+
}
24+
25+
/**
26+
* Creates a {@code QueueScheduler} with the given (fixed) capacity.
27+
*
28+
* @param capacity the capacity of this queue,
29+
* see {@link LinkedBlockingQueue#LinkedBlockingQueue(int)}
30+
* @since 0.8.0
31+
*/
32+
public QueueScheduler(int capacity) {
33+
this.queue = new LinkedBlockingQueue<>(capacity);
34+
}
2035

2136
@Override
2237
public void pushWhenNoDuplicate(Request request, Task task) {
23-
queue.add(request);
38+
try {
39+
queue.put(request);
40+
} catch (InterruptedException e) {
41+
Thread.currentThread().interrupt();
42+
}
2443
}
2544

2645
@Override

0 commit comments

Comments
 (0)