diff --git a/spring-integration-ip/src/main/java/org/springframework/integration/ip/tcp/connection/TcpNetConnection.java b/spring-integration-ip/src/main/java/org/springframework/integration/ip/tcp/connection/TcpNetConnection.java index 980121a1b6..f61b4cec5a 100644 --- a/spring-integration-ip/src/main/java/org/springframework/integration/ip/tcp/connection/TcpNetConnection.java +++ b/spring-integration-ip/src/main/java/org/springframework/integration/ip/tcp/connection/TcpNetConnection.java @@ -154,7 +154,6 @@ public class TcpNetConnection extends TcpConnectionSupport implements Scheduling */ @Override public void run() { - TcpListener listener = getListener(); boolean okToRun = true; if (logger.isDebugEnabled()) { logger.debug(this.getConnectionId() + " Reading..."); @@ -176,6 +175,7 @@ public class TcpNetConnection extends TcpConnectionSupport implements Scheduling logger.debug("Message received " + message); } try { + TcpListener listener = getListener(); if (listener == null) { throw new NoListenerException("No listener"); } diff --git a/spring-integration-ip/src/test/java/org/springframework/integration/ip/tcp/connection/FailoverClientConnectionFactoryTests.java b/spring-integration-ip/src/test/java/org/springframework/integration/ip/tcp/connection/FailoverClientConnectionFactoryTests.java index 9ea8857192..12af25af0e 100644 --- a/spring-integration-ip/src/test/java/org/springframework/integration/ip/tcp/connection/FailoverClientConnectionFactoryTests.java +++ b/spring-integration-ip/src/test/java/org/springframework/integration/ip/tcp/connection/FailoverClientConnectionFactoryTests.java @@ -53,6 +53,7 @@ import org.springframework.context.ApplicationEvent; import org.springframework.context.ApplicationEventPublisher; import org.springframework.integration.channel.DirectChannel; import org.springframework.integration.channel.QueueChannel; +import org.springframework.integration.handler.BridgeHandler; import org.springframework.integration.ip.IpHeaders; import org.springframework.integration.ip.tcp.TcpInboundGateway; import org.springframework.integration.ip.tcp.TcpOutboundGateway; @@ -405,6 +406,61 @@ public class FailoverClientConnectionFactoryTests { server2.stop(); } + @SuppressWarnings("unchecked") + @Test + public void testFailoverCachedWithGateway() throws Exception { + final TcpNetServerConnectionFactory server = new TcpNetServerConnectionFactory(0); + server.setBeanName("server"); + server.afterPropertiesSet(); + DirectChannel inChannel = new DirectChannel(); + inChannel.setBeanName("inChannel"); + TcpInboundGateway inbound = new TcpInboundGateway(); + inbound.setConnectionFactory(server); + inbound.setRequestChannel(inChannel); + inbound.afterPropertiesSet(); + inChannel.subscribe(new BridgeHandler()); + inbound.start(); + TestingUtilities.waitListening(server, 10000L); + int port = server.getPort(); + AbstractClientConnectionFactory client = new TcpNetClientConnectionFactory("localhost", port); + client.setBeanName("client"); + + // Cache + CachingClientConnectionFactory cachingClient = new CachingClientConnectionFactory(client, 2); + cachingClient.setBeanName("cache"); + cachingClient.afterPropertiesSet(); + + // Failover + List clientFactories = new ArrayList(); + clientFactories.add(cachingClient); + FailoverClientConnectionFactory failoverClient = new FailoverClientConnectionFactory(clientFactories); + failoverClient.setSingleUse(true); + failoverClient.afterPropertiesSet(); + + TcpOutboundGateway outbound = new TcpOutboundGateway(); + outbound.setConnectionFactory(failoverClient); + QueueChannel replyChannel = new QueueChannel(); + replyChannel.setBeanName("replyChannel"); + outbound.setReplyChannel(replyChannel); + outbound.setBeanFactory(mock(BeanFactory.class)); + outbound.afterPropertiesSet(); + outbound.start(); + + outbound.handleMessage(new GenericMessage("foo")); + Message result = (Message) replyChannel.receive(10000); + assertNotNull(result); + assertEquals("foo", new String(result.getPayload())); + + // INT-4024 - second reply had bad connection id + outbound.handleMessage(new GenericMessage("foo")); + result = (Message) replyChannel.receive(10000); + assertNotNull(result); + assertEquals("foo", new String(result.getPayload())); + + inbound.stop(); + outbound.stop(); + } + @Test public void testFailoverCachedRealBadHost() throws Exception { TcpNetServerConnectionFactory server1 = new TcpNetServerConnectionFactory(0);