From d8fb02306f6fbb6a273d69d4eb0a57e21d1adef2 Mon Sep 17 00:00:00 2001 From: Artem Bilan Date: Tue, 10 Dec 2024 16:34:29 -0500 Subject: [PATCH] GH-9709: Fix `IntegrationFlow` for input channel resolution Fixes: #9709 Issue link: https://github.com/spring-projects/spring-integration/issues/9709 The Java DSL loses an `inputChannel` when existing channel is referenced by its name. Then the target IntegrationFlow does not contain a bean reference for this channel and when we call its `getInputChannel()` we got something from middle of the flow. However, that `inputChannel` is really expected to be an input for the flow. * Fix `IntegrationFlowBeanPostProcessor` to get a bean by `MessageChannelReference` and populate it into `targetIntegrationComponents` as we do for newly created `DirectChannel` if there is no bean for provided `MessageChannelReference` # Conflicts: # spring-integration-core/src/main/java/org/springframework/integration/dsl/context/IntegrationFlowBeanPostProcessor.java --- .../context/IntegrationFlowBeanPostProcessor.java | 11 ++++++++--- .../composition/IntegrationFlowCompositionTests.java | 12 +++++++++--- 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/spring-integration-core/src/main/java/org/springframework/integration/dsl/context/IntegrationFlowBeanPostProcessor.java b/spring-integration-core/src/main/java/org/springframework/integration/dsl/context/IntegrationFlowBeanPostProcessor.java index b746627c83..3386fe7392 100644 --- a/spring-integration-core/src/main/java/org/springframework/integration/dsl/context/IntegrationFlowBeanPostProcessor.java +++ b/spring-integration-core/src/main/java/org/springframework/integration/dsl/context/IntegrationFlowBeanPostProcessor.java @@ -74,6 +74,7 @@ import org.springframework.integration.dsl.support.MessageChannelReference; import org.springframework.integration.gateway.AnnotationGatewayProxyFactoryBean; import org.springframework.integration.support.context.NamedComponent; import org.springframework.lang.Nullable; +import org.springframework.messaging.MessageChannel; import org.springframework.messaging.MessageHandler; import org.springframework.util.Assert; import org.springframework.util.CollectionUtils; @@ -181,11 +182,15 @@ public class IntegrationFlowBeanPostProcessor } else if (component instanceof MessageChannelReference messageChannelReference) { String channelBeanName = messageChannelReference.name(); + MessageChannel channelByName; if (!this.beanFactory.containsBean(channelBeanName)) { - DirectChannel directChannel = new DirectChannel(); - registerComponent(directChannel, channelBeanName, flowBeanName); - targetIntegrationComponents.put(directChannel, channelBeanName); + channelByName = new DirectChannel(); + registerComponent(channelByName, channelBeanName, flowBeanName); } + else { + channelByName = this.beanFactory.getBean(channelBeanName, MessageChannel.class); + } + targetIntegrationComponents.put(channelByName, channelBeanName); } else if (component instanceof SourcePollingChannelAdapterSpec spec) { Map componentsToRegister = spec.getComponentsToRegister(); diff --git a/spring-integration-core/src/test/java/org/springframework/integration/dsl/composition/IntegrationFlowCompositionTests.java b/spring-integration-core/src/test/java/org/springframework/integration/dsl/composition/IntegrationFlowCompositionTests.java index d3413cacf1..ee84fef3ae 100644 --- a/spring-integration-core/src/test/java/org/springframework/integration/dsl/composition/IntegrationFlowCompositionTests.java +++ b/spring-integration-core/src/test/java/org/springframework/integration/dsl/composition/IntegrationFlowCompositionTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2021-2023 the original author or authors. + * Copyright 2021-2024 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -202,11 +202,17 @@ public class IntegrationFlowCompositionTests { .to(lastFlow); } + @Bean + DirectChannel lastFlowInput() { + return new DirectChannel(); + } + @Bean IntegrationFlow lastFlow() { - return f -> f + return IntegrationFlow.from("lastFlowInput") .transform(p -> p + ", and last flow") - .channel(c -> c.queue("lastFlowResult")); + .channel(c -> c.queue("lastFlowResult")) + .get(); } }