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 org.springframework.batch.repeat.RepeatException; |
20 | |
21 | /** |
22 | * Abstract implementation that can be extended for both Backport Concurrent and |
23 | * JDK 5 Concurrent. |
24 | * |
25 | * @author Ben Hale |
26 | * @author Dave Syer |
27 | */ |
28 | abstract class AbstractResultQueue extends RepeatInternalStateSupport implements ResultQueue { |
29 | |
30 | // Arbitrary lock object. |
31 | private final Object lock = new Object(); |
32 | |
33 | // Counter to monitor the difference between expected and actually collected |
34 | // results. When this reaches zero there are really no more results. |
35 | private volatile int count = 0; |
36 | |
37 | public boolean isExpecting() { |
38 | synchronized (lock) { |
39 | // Base the decision about whether we expect more results on a |
40 | // counter of the number of expected results actually collected. |
41 | return count > 0; |
42 | } |
43 | } |
44 | |
45 | public void expect() { |
46 | try { |
47 | synchronized (lock) { |
48 | aquireWait(); |
49 | count++; |
50 | } |
51 | } |
52 | catch (InterruptedException e) { |
53 | Thread.currentThread().interrupt(); |
54 | throw new RepeatException("InterruptedException waiting for to acquire lock on input."); |
55 | } |
56 | } |
57 | |
58 | public void put(ResultHolder holder) { |
59 | // There should be no need to block here, or to use offer() |
60 | addResult(holder); |
61 | // Take from the waits queue now to allow another result to |
62 | // accumulate. But don't decrement the counter. |
63 | releaseWait(); |
64 | } |
65 | |
66 | public ResultHolder take() { |
67 | ResultHolder value; |
68 | try { |
69 | synchronized (lock) { |
70 | value = takeResult(); |
71 | // Decrement the counter only when the result is collected. |
72 | count--; |
73 | } |
74 | } |
75 | catch (InterruptedException e) { |
76 | Thread.currentThread().interrupt(); |
77 | throw new RepeatException("InterruptedException while waiting for result."); |
78 | } |
79 | return value; |
80 | } |
81 | |
82 | /** |
83 | * Acquire permission for one more task on the queue. |
84 | * |
85 | * @throws InterruptedException |
86 | */ |
87 | protected abstract void aquireWait() throws InterruptedException; |
88 | |
89 | /** |
90 | * Release the permit that we were holding while a task was processed. |
91 | */ |
92 | protected abstract void releaseWait(); |
93 | |
94 | /** |
95 | * Add a {@link ResultHolder} with a finished result to the queue for |
96 | * collection. Should not block. May throw an exception to signal that the |
97 | * queue is full, but that would be an unexpected condition. |
98 | * |
99 | * @param resultHolder a {@link ResultHolder} |
100 | */ |
101 | protected abstract void addResult(ResultHolder resultHolder); |
102 | |
103 | /** |
104 | * Obtain a result from the queue, blocking until one becomes available. |
105 | * |
106 | * @return a {@link ResultHolder} with the completed result |
107 | * @throws InterruptedException if an interrupt is signalled |
108 | */ |
109 | protected abstract ResultHolder takeResult() throws InterruptedException; |
110 | |
111 | } |