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**
This commit is contained in:
Gary Russell
2020-06-01 09:46:24 -04:00
committed by Artem Bilan
parent ef939a09a4
commit 6e78569c00

View File

@@ -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");