1 | /* |
2 | * Copyright 2002-2007 the original author or authors. |
3 | * |
4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
5 | * you may not use this file except in compliance with the License. |
6 | * You may obtain a copy of the License at |
7 | * |
8 | * http://www.apache.org/licenses/LICENSE-2.0 |
9 | * |
10 | * Unless required by applicable law or agreed to in writing, software |
11 | * distributed under the License is distributed on an "AS IS" BASIS, |
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
13 | * See the License for the specific language governing permissions and |
14 | * limitations under the License. |
15 | */ |
16 | |
17 | package org.springframework.batch.repeat.support; |
18 | |
19 | import edu.emory.mathcs.backport.java.util.concurrent.BlockingQueue; |
20 | import edu.emory.mathcs.backport.java.util.concurrent.LinkedBlockingQueue; |
21 | import edu.emory.mathcs.backport.java.util.concurrent.Semaphore; |
22 | |
23 | /** |
24 | * An implementation of the {@link ResultQueue} that uses Backport Concurrent Utilities. |
25 | * |
26 | * @author Ben Hale |
27 | */ |
28 | class BackportConcurrentResultQueue extends AbstractResultQueue implements RepeatInternalState { |
29 | |
30 | // Accumulation of result objects as they finish. |
31 | private final BlockingQueue results; |
32 | |
33 | // Accumulation of dummy objects flagging expected results in the future. |
34 | private final Semaphore waits; |
35 | |
36 | BackportConcurrentResultQueue(int throttleLimit) { |
37 | results = new LinkedBlockingQueue(); |
38 | waits = new Semaphore(throttleLimit); |
39 | } |
40 | |
41 | protected void addResult(ResultHolder resultHolder) { |
42 | results.add(resultHolder); |
43 | } |
44 | |
45 | protected void aquireWait() throws InterruptedException { |
46 | waits.acquire(); |
47 | } |
48 | |
49 | protected void releaseWait() { |
50 | waits.release(); |
51 | } |
52 | |
53 | protected ResultHolder takeResult() throws InterruptedException { |
54 | return (ResultHolder) results.take(); |
55 | } |
56 | |
57 | public boolean isEmpty() { |
58 | return results.isEmpty(); |
59 | } |
60 | |
61 | } |