From 6e78569c0097b9bd6c69a50af2129b20daee29a2 Mon Sep 17 00:00:00 2001 From: Gary Russell Date: Mon, 1 Jun 2020 09:46:24 -0400 Subject: [PATCH] GH-2736: TCP Async GW - Fix async check Checking for NIO cannot be done in `onInit()` because the factory is not yet started. **cherry-pick to 5.3.x** --- .../ip/tcp/TcpOutboundGateway.java | 24 +++++++------------ 1 file changed, 8 insertions(+), 16 deletions(-) diff --git a/spring-integration-ip/src/main/java/org/springframework/integration/ip/tcp/TcpOutboundGateway.java b/spring-integration-ip/src/main/java/org/springframework/integration/ip/tcp/TcpOutboundGateway.java index 1963f0d591..3a4390c0e1 100644 --- a/spring-integration-ip/src/main/java/org/springframework/integration/ip/tcp/TcpOutboundGateway.java +++ b/spring-integration-ip/src/main/java/org/springframework/integration/ip/tcp/TcpOutboundGateway.java @@ -39,7 +39,6 @@ import org.springframework.integration.ip.tcp.connection.AbstractClientConnectio import org.springframework.integration.ip.tcp.connection.AbstractConnectionFactory; import org.springframework.integration.ip.tcp.connection.TcpConnection; import org.springframework.integration.ip.tcp.connection.TcpConnectionFailedCorrelationEvent; -import org.springframework.integration.ip.tcp.connection.TcpConnectionSupport; import org.springframework.integration.ip.tcp.connection.TcpListener; import org.springframework.integration.ip.tcp.connection.TcpNioConnectionSupport; import org.springframework.integration.ip.tcp.connection.TcpSender; @@ -127,21 +126,6 @@ public class TcpOutboundGateway extends AbstractReplyProducingMessageHandler } Assert.state(!this.closeStreamAfterSend || this.isSingleUse, "Single use connection needed with closeStreamAfterSend"); - if (isAsync()) { - try { - TcpConnectionSupport connection = this.connectionFactory.getConnection(); - if (connection instanceof TcpNioConnectionSupport) { - setAsync(false); - this.logger.warn("Async replies are not supported with NIO; see the reference manual"); - } - if (this.isSingleUse) { - connection.close(); - } - } - catch (Exception e) { - this.logger.error("Could not check if async is supported", e); - } - } } /** @@ -167,6 +151,7 @@ public class TcpOutboundGateway extends AbstractReplyProducingMessageHandler try { haveSemaphore = acquireSemaphoreIfNeeded(requestMessage); connection = this.connectionFactory.getConnection(); + checkAsync(connection, async); Long remoteTimeout = getRemoteTimeout(requestMessage); AsyncReply reply = new AsyncReply(remoteTimeout, connection, haveSemaphore, requestMessage, async); connectionId = connection.getConnectionId(); @@ -203,6 +188,13 @@ public class TcpOutboundGateway extends AbstractReplyProducingMessageHandler } } + private void checkAsync(TcpConnection connection, boolean async) { + if (async && connection instanceof TcpNioConnectionSupport) { + setAsync(false); + this.logger.warn("Async replies are not supported with NIO; see the reference manual"); + } + } + private boolean acquireSemaphoreIfNeeded(Message requestMessage) throws InterruptedException { if (!this.isSingleUse) { logger.debug("trying semaphore");