Skip to content

Commit 9471d7c

Browse files
committed
Add CountDownLatch to TaskExecutorRepeatTemplate
1 parent 3281cc7 commit 9471d7c

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

src/main/java_spring_batch_5.1.2_fix_latch/org/springframework/batch/repeat/support/TaskExecutorRepeatTemplate.java

+14-3
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
package org.springframework.batch.repeat.support;
1818

19+
import java.util.concurrent.CountDownLatch;
20+
1921
import org.springframework.batch.repeat.RepeatCallback;
2022
import org.springframework.batch.repeat.RepeatContext;
2123
import org.springframework.batch.repeat.RepeatException;
@@ -59,6 +61,8 @@ public class TaskExecutorRepeatTemplate extends RepeatTemplate {
5961

6062
private TaskExecutor taskExecutor = new SyncTaskExecutor();
6163

64+
private final CountDownLatch latch = new CountDownLatch(1);
65+
6266
/**
6367
* Public setter for the throttle limit. The throttle limit is the largest number of
6468
* concurrent tasks that can be executing at one time - if a new task arrives and the
@@ -110,7 +114,7 @@ protected RepeatStatus getNextResult(RepeatContext context, RepeatCallback callb
110114
* Wrap the callback in a runnable that will add its result to the queue when
111115
* it is ready.
112116
*/
113-
runnable = new ExecutingRunnable(callback, context, queue);
117+
runnable = new ExecutingRunnable(callback, context, queue, latch);
114118

115119
/*
116120
* Tell the runnable that it can expect a result. This could have been
@@ -133,6 +137,9 @@ protected RepeatStatus getNextResult(RepeatContext context, RepeatCallback callb
133137
/*
134138
* Keep going until we get a result that is finished, or early termination...
135139
*/
140+
logger.debug("Waiting for latch : " + latch.hashCode() + " (count=" + latch.getCount() + ")");
141+
latch.await();
142+
logger.debug("Latch released : " + latch.hashCode() + " (count=" + latch.getCount() + ")");
136143
}
137144
while (queue.isEmpty() && !isComplete(context));
138145

@@ -216,14 +223,15 @@ private class ExecutingRunnable implements Runnable, ResultHolder {
216223

217224
private volatile Throwable error;
218225

219-
public ExecutingRunnable(RepeatCallback callback, RepeatContext context, ResultQueue<ResultHolder> queue) {
226+
private CountDownLatch latch;
227+
public ExecutingRunnable(RepeatCallback callback, RepeatContext context, ResultQueue<ResultHolder> queue, CountDownLatch latch) {
220228

221229
super();
222230

223231
this.callback = callback;
224232
this.context = context;
225233
this.queue = queue;
226-
234+
this.latch = latch;
227235
}
228236

229237
/**
@@ -272,6 +280,9 @@ public void run() {
272280

273281
queue.put(this);
274282

283+
logger.debug("Will count down the latch : " + latch.hashCode() + " (count=" + latch.getCount() + ")");
284+
this.latch.countDown();
285+
logger.debug("Latch updated : " + latch.hashCode() + " (count=" + latch.getCount() + ")");
275286
}
276287
}
277288

0 commit comments

Comments
 (0)