From b4fcf9ec878db8fd10111f0c8b4e116f266a88bf Mon Sep 17 00:00:00 2001 From: Artem Bilan Date: Thu, 12 Dec 2019 14:47:40 -0500 Subject: [PATCH] GH-3120: Identify polling MG as outbound-CA Fixes https://github.com/spring-projects/spring-integration/issues/3120 The messaging gateway method without arguments works as a polling channel adapter. In this case a Messaging Gateway component is present in the end of flow. Therefore we treat it as an `outbound_channel_adapter` --- .../gateway/GatewayProxyFactoryBean.java | 18 +++++++++++++++--- .../gateway/GatewayProxyFactoryBeanTests.java | 15 ++++++++++++++- 2 files changed, 29 insertions(+), 4 deletions(-) diff --git a/spring-integration-core/src/main/java/org/springframework/integration/gateway/GatewayProxyFactoryBean.java b/spring-integration-core/src/main/java/org/springframework/integration/gateway/GatewayProxyFactoryBean.java index 95f8c4234c..02ef08585d 100644 --- a/spring-integration-core/src/main/java/org/springframework/integration/gateway/GatewayProxyFactoryBean.java +++ b/spring-integration-core/src/main/java/org/springframework/integration/gateway/GatewayProxyFactoryBean.java @@ -761,6 +761,10 @@ public class GatewayProxyFactoryBean extends AbstractEndpoint MethodInvocationGateway gateway = new MethodInvocationGateway(messageMapper); gateway.setupReturnType(this.serviceInterface, method); + if (method.getParameterTypes().length == 0 && !findPayloadExpression(method)) { + gateway.setPollable(); + } + JavaUtils.INSTANCE .acceptIfNotNull(payloadExpression, messageMapper::setPayloadExpression) .acceptIfNotNull(getTaskScheduler(), gateway::setTaskScheduler); @@ -941,15 +945,18 @@ public class GatewayProxyFactoryBean extends AbstractEndpoint private boolean isVoidReturn; + private boolean pollable; + MethodInvocationGateway(GatewayMethodInboundMessageMapper messageMapper) { setRequestMapper(messageMapper); } @Override public IntegrationPatternType getIntegrationPatternType() { - return this.isVoidReturn - ? IntegrationPatternType.inbound_channel_adapter - : IntegrationPatternType.inbound_gateway; + return this.pollable ? IntegrationPatternType.outbound_channel_adapter + : this.isVoidReturn + ? IntegrationPatternType.inbound_channel_adapter + : IntegrationPatternType.inbound_gateway; } @Nullable @@ -992,6 +999,11 @@ public class GatewayProxyFactoryBean extends AbstractEndpoint } return Void.class.isAssignableFrom(returnTypeToCheck); } + + private void setPollable() { + this.pollable = true; + } + } private final class Invoker implements Supplier { diff --git a/spring-integration-core/src/test/java/org/springframework/integration/gateway/GatewayProxyFactoryBeanTests.java b/spring-integration-core/src/test/java/org/springframework/integration/gateway/GatewayProxyFactoryBeanTests.java index e21ce82121..3911646023 100644 --- a/spring-integration-core/src/test/java/org/springframework/integration/gateway/GatewayProxyFactoryBeanTests.java +++ b/spring-integration-core/src/test/java/org/springframework/integration/gateway/GatewayProxyFactoryBeanTests.java @@ -31,7 +31,7 @@ import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.TimeUnit; -import org.junit.Test; +import org.junit.jupiter.api.Test; import org.mockito.Mockito; import org.springframework.beans.factory.BeanFactory; @@ -43,6 +43,7 @@ import org.springframework.core.convert.converter.Converter; import org.springframework.core.convert.support.DefaultConversionService; import org.springframework.core.convert.support.GenericConversionService; import org.springframework.expression.common.LiteralExpression; +import org.springframework.integration.IntegrationPatternType; import org.springframework.integration.annotation.Gateway; import org.springframework.integration.annotation.GatewayHeader; import org.springframework.integration.channel.DirectChannel; @@ -56,6 +57,7 @@ import org.springframework.messaging.MessageHeaders; import org.springframework.messaging.PollableChannel; import org.springframework.messaging.handler.annotation.Header; import org.springframework.messaging.support.GenericMessage; +import org.springframework.util.ClassUtils; import org.springframework.util.ReflectionUtils; /** @@ -156,6 +158,11 @@ public class GatewayProxyFactoryBeanTests { Message message = service.getMessage(); assertThat(message).isNotNull(); assertThat(message.getPayload()).isEqualTo("foo"); + + MessagingGatewaySupport messagingGatewaySupport = + proxyFactory.getGateways().get(ClassUtils.getMethod(TestService.class, "getMessage")); + assertThat(messagingGatewaySupport.getIntegrationPatternType()) + .isEqualTo(IntegrationPatternType.outbound_channel_adapter); } @Test @@ -259,6 +266,12 @@ public class GatewayProxyFactoryBeanTests { TestService service = (TestService) proxyFactory.getObject(); String result = service.requestReplyWithPayloadAnnotation(); assertThat(result).isEqualTo("requestReplyWithPayloadAnnotation0bar"); + + MessagingGatewaySupport messagingGatewaySupport = + proxyFactory.getGateways() + .get(ClassUtils.getMethod(TestService.class, "requestReplyWithPayloadAnnotation")); + assertThat(messagingGatewaySupport.getIntegrationPatternType()) + .isEqualTo(IntegrationPatternType.inbound_gateway); } @Test