Skip to content

Commit c36df65

Browse files
quafffmbenhassine
authored andcommitted
Improve MessageChannelPartitionHandler::receiveReplies
1. Message payload not always be `Set`, if you return payload directly, you will see tests failed: ``` [main] ERROR org.springframework.batch.core.step.AbstractStep - Encountered an error executing step step1-manager in job job1 java.lang.ClassCastException: class java.util.ArrayList cannot be cast to class java.util.Set (java.util.ArrayList and java.util.Set are in module java.base of loader 'bootstrap') at org.springframework.batch.integration.partition.MessageChannelPartitionHandler.receiveReplies(MessageChannelPartitionHandler.java:298) at org.springframework.batch.integration.partition.MessageChannelPartitionHandler.doHandle(MessageChannelPartitionHandler.java:244) at org.springframework.batch.core.partition.support.AbstractPartitionHandler.handle(AbstractPartitionHandler.java:60) at org.springframework.batch.core.partition.support.PartitionStep.doExecute(PartitionStep.java:102) at org.springframework.batch.core.step.AbstractStep.execute(AbstractStep.java:230) at org.springframework.batch.core.job.SimpleStepHandler.handleStep(SimpleStepHandler.java:153) at org.springframework.batch.core.job.flow.JobFlowExecutor.executeStep(JobFlowExecutor.java:68) at org.springframework.batch.core.job.flow.support.state.StepState.handle(StepState.java:68) at org.springframework.batch.core.job.flow.support.SimpleFlow.resume(SimpleFlow.java:165) at org.springframework.batch.core.job.flow.support.SimpleFlow.start(SimpleFlow.java:140) at org.springframework.batch.core.job.flow.FlowJob.doExecute(FlowJob.java:132) at org.springframework.batch.core.job.AbstractJob.execute(AbstractJob.java:307) at org.springframework.batch.core.launch.support.TaskExecutorJobLauncher$1.run(TaskExecutorJobLauncher.java:155) at org.springframework.core.task.SyncTaskExecutor.execute(SyncTaskExecutor.java:50) at org.springframework.batch.core.launch.support.TaskExecutorJobLauncher.run(TaskExecutorJobLauncher.java:146) at org.springframework.batch.integration.partition.VanillaIntegrationTests.testLaunchJob(VanillaIntegrationTests.java:58) ``` 2. No need to create a new `HashSet` if payload is `Set` since no modifications applied to it. Signed-off-by: Yanming Zhou <zhouyanming@gmail.com> (cherry picked from commit 0d3832f)
1 parent 82d2b7c commit c36df65

File tree

1 file changed

+5
-2
lines changed

1 file changed

+5
-2
lines changed

spring-batch-integration/src/main/java/org/springframework/batch/integration/partition/MessageChannelPartitionHandler.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package org.springframework.batch.integration.partition;
1717

18+
import java.util.Collection;
1819
import java.util.HashSet;
1920
import java.util.List;
2021
import java.util.Set;
@@ -83,6 +84,7 @@
8384
* @author Will Schipp
8485
* @author Michael Minella
8586
* @author Mahmoud Ben Hassine
87+
* @author Yanming Zhou
8688
*
8789
*/
8890
@MessageEndpoint
@@ -286,7 +288,7 @@ private Set<StepExecution> pollReplies(final StepExecution managerStepExecution,
286288

287289
@SuppressWarnings("unchecked")
288290
private Set<StepExecution> receiveReplies(PollableChannel currentReplyChannel) {
289-
Message<Set<StepExecution>> message = (Message<Set<StepExecution>>) messagingGateway
291+
Message<Collection<StepExecution>> message = (Message<Collection<StepExecution>>) messagingGateway
290292
.receive(currentReplyChannel);
291293

292294
if (message == null) {
@@ -296,7 +298,8 @@ else if (logger.isDebugEnabled()) {
296298
logger.debug("Received replies: " + message);
297299
}
298300

299-
return new HashSet<>(message.getPayload());
301+
Collection<StepExecution> payload = message.getPayload();
302+
return payload instanceof Set ? (Set<StepExecution>) payload : new HashSet<>(message.getPayload());
300303
}
301304

302305
private Message<StepExecutionRequest> createMessage(int sequenceNumber, int sequenceSize,

0 commit comments

Comments
 (0)