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