Skip to content

Commit

Permalink
GH-2736: TCP Async GW - Fix async check
Browse files Browse the repository at this point in the history
Checking for NIO cannot be done in `onInit()` because the factory is
not yet started.

**cherry-pick to 5.3.x**
  • Loading branch information
garyrussell authored and artembilan committed Jun 2, 2020
1 parent ef939a0 commit 6e78569
Showing 1 changed file with 8 additions and 16 deletions.
Expand Up @@ -39,7 +39,6 @@
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;
Expand Down Expand Up @@ -127,21 +126,6 @@ protected void doInit() {
}
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);
}
}
}

/**
Expand All @@ -167,6 +151,7 @@ protected Object handleRequestMessage(Message<?> requestMessage) {
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();
Expand Down Expand Up @@ -203,6 +188,13 @@ protected Object handleRequestMessage(Message<?> requestMessage) {
}
}

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");
Expand Down

0 comments on commit 6e78569

Please sign in to comment.