Skip to content

Commit aafecb9

Browse files
doontagifmbenhassine
authored andcommitted
Fix unfinished step in parallel flow
Resolves #3939 Signed-off-by: Mahmoud Ben Hassine <[email protected]>
1 parent bd00cde commit aafecb9

File tree

2 files changed

+49
-5
lines changed

2 files changed

+49
-5
lines changed

spring-batch-core/src/main/java/org/springframework/batch/core/job/flow/support/state/SplitState.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2006-2023 the original author or authors.
2+
* Copyright 2006-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -19,6 +19,7 @@
1919
import java.util.Arrays;
2020
import java.util.Collection;
2121
import java.util.Collections;
22+
import java.util.List;
2223
import java.util.concurrent.ExecutionException;
2324
import java.util.concurrent.Future;
2425
import java.util.concurrent.FutureTask;
@@ -119,7 +120,7 @@ public FlowExecutionStatus handle(final FlowExecutor executor) throws Exception
119120
FlowExecutionStatus parentSplitStatus = parentSplit == null ? null : parentSplit.handle(executor);
120121

121122
Collection<FlowExecution> results = new ArrayList<>();
122-
123+
List<Exception> exceptions = new ArrayList<>();
123124
// Could use a CompletionService here?
124125
for (Future<FlowExecution> task : tasks) {
125126
try {
@@ -129,14 +130,18 @@ public FlowExecutionStatus handle(final FlowExecutor executor) throws Exception
129130
// Unwrap the expected exceptions
130131
Throwable cause = e.getCause();
131132
if (cause instanceof Exception) {
132-
throw (Exception) cause;
133+
exceptions.add((Exception) cause);
133134
}
134135
else {
135-
throw e;
136+
exceptions.add(e);
136137
}
137138
}
138139
}
139140

141+
if (!exceptions.isEmpty()) {
142+
throw exceptions.get(0);
143+
}
144+
140145
FlowExecutionStatus flowExecutionStatus = doAggregation(results, executor);
141146
if (parentSplitStatus != null) {
142147
return Collections.max(Arrays.asList(flowExecutionStatus, parentSplitStatus));

spring-batch-core/src/test/java/org/springframework/batch/core/job/builder/FlowJobBuilderTests.java

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2023 the original author or authors.
2+
* Copyright 2012-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,11 +16,14 @@
1616
package org.springframework.batch.core.job.builder;
1717

1818
import java.util.Arrays;
19+
import java.util.concurrent.CountDownLatch;
20+
import java.util.concurrent.TimeUnit;
1921

2022
import javax.sql.DataSource;
2123

2224
import static org.junit.jupiter.api.Assertions.assertEquals;
2325

26+
import org.junit.jupiter.api.Assertions;
2427
import org.junit.jupiter.api.BeforeEach;
2528
import org.junit.jupiter.api.Test;
2629
import org.springframework.batch.core.BatchStatus;
@@ -45,6 +48,8 @@
4548
import org.springframework.batch.core.step.StepSupport;
4649
import org.springframework.batch.core.step.builder.StepBuilder;
4750
import org.springframework.batch.item.support.ListItemReader;
51+
import org.springframework.batch.repeat.RepeatStatus;
52+
import org.springframework.batch.support.transaction.ResourcelessTransactionManager;
4853
import org.springframework.beans.factory.annotation.Value;
4954
import org.springframework.context.ApplicationContext;
5055
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
@@ -369,4 +374,38 @@ public JdbcTransactionManager transactionManager(DataSource dataSource) {
369374

370375
}
371376

377+
@Test
378+
public void testBuildSplitWithParallelFlow() throws InterruptedException {
379+
CountDownLatch countDownLatch = new CountDownLatch(1);
380+
Step longExecutingStep = new StepBuilder("longExecutingStep", jobRepository).tasklet((stepContribution, b) -> {
381+
Thread.sleep(500L);
382+
return RepeatStatus.FINISHED;
383+
}, new ResourcelessTransactionManager()).build();
384+
385+
Step interruptedStep = new StepBuilder("interruptedStep", jobRepository).tasklet((stepContribution, b) -> {
386+
stepContribution.getStepExecution().setTerminateOnly();
387+
return RepeatStatus.FINISHED;
388+
}, new ResourcelessTransactionManager()).build();
389+
390+
Step nonExecutableStep = new StepBuilder("nonExecutableStep", jobRepository).tasklet((stepContribution, b) -> {
391+
countDownLatch.countDown();
392+
return RepeatStatus.FINISHED;
393+
}, new ResourcelessTransactionManager()).build();
394+
395+
Flow twoStepFlow = new FlowBuilder<SimpleFlow>("twoStepFlow").start(longExecutingStep)
396+
.next(nonExecutableStep)
397+
.build();
398+
Flow interruptedFlow = new FlowBuilder<SimpleFlow>("interruptedFlow").start(interruptedStep).build();
399+
400+
Flow splitFlow = new FlowBuilder<Flow>("splitFlow").split(new SimpleAsyncTaskExecutor())
401+
.add(interruptedFlow, twoStepFlow)
402+
.build();
403+
FlowJobBuilder jobBuilder = new JobBuilder("job", jobRepository).start(splitFlow).build();
404+
jobBuilder.preventRestart().build().execute(execution);
405+
406+
boolean isExecutedNonExecutableStep = countDownLatch.await(1, TimeUnit.SECONDS);
407+
assertEquals(BatchStatus.STOPPED, execution.getStatus());
408+
Assertions.assertFalse(isExecutedNonExecutableStep);
409+
}
410+
372411
}

0 commit comments

Comments
 (0)