From 99e8d58c135e1a195665eaaefb86b0e8e225cdda Mon Sep 17 00:00:00 2001 From: Taeik Lim Date: Mon, 11 Apr 2022 23:29:06 +0900 Subject: [PATCH] Prevent race condition when flow transition is not initialized Resolves #4092 --- .../batch/core/job/flow/support/SimpleFlow.java | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/spring-batch-core/src/main/java/org/springframework/batch/core/job/flow/support/SimpleFlow.java b/spring-batch-core/src/main/java/org/springframework/batch/core/job/flow/support/SimpleFlow.java index 2101c171d..f66ce004c 100644 --- a/spring-batch-core/src/main/java/org/springframework/batch/core/job/flow/support/SimpleFlow.java +++ b/spring-batch-core/src/main/java/org/springframework/batch/core/job/flow/support/SimpleFlow.java @@ -48,6 +48,7 @@ import org.springframework.beans.factory.InitializingBean; * @author Dave Syer * @author Michael Minella * @author Mahmoud Ben Hassine + * @author Taeik Lim * @since 2.0 */ public class SimpleFlow implements Flow, InitializingBean { @@ -124,9 +125,7 @@ public class SimpleFlow implements Flow, InitializingBean { */ @Override public void afterPropertiesSet() throws Exception { - if (startState == null) { - initializeTransitions(); - } + initializeTransitionsIfNotInitialized(); } /** @@ -134,9 +133,8 @@ public class SimpleFlow implements Flow, InitializingBean { */ @Override public FlowExecution start(FlowExecutor executor) throws FlowExecutionException { - if (startState == null) { - initializeTransitions(); - } + initializeTransitionsIfNotInitialized(); + State state = startState; String stateName = state.getName(); return resume(stateName, executor); @@ -262,6 +260,12 @@ public class SimpleFlow implements Flow, InitializingBean { return continued; } + private synchronized void initializeTransitionsIfNotInitialized() { + if (startState == null) { + initializeTransitions(); + } + } + /** * Analyse the transitions provided and generate all the information needed to execute * the flow.