From 032c8fa55d723839bd595475e13abd092f212968 Mon Sep 17 00:00:00 2001 From: Gary Russell Date: Sun, 14 Jan 2018 14:59:13 -0500 Subject: [PATCH] INT-4378: TCP Fix CF Name in Intercepted Events JIRA: https://jira.spring.io/browse/INT-4378 Events (e.g. `TcpConnectionOpenEvent` from intercepted connections contain an 'unknown' connection factory name. Delegate to the underlying connection's factory name. --- .../TcpConnectionInterceptorSupport.java | 7 ++++- .../tcp/connection/TcpConnectionSupport.java | 12 ++++++--- ...terceptedSharedConnectionTests-context.xml | 2 ++ .../tcp/InterceptedSharedConnectionTests.java | 26 ++++++++++++++++++- 4 files changed, 41 insertions(+), 6 deletions(-) diff --git a/spring-integration-ip/src/main/java/org/springframework/integration/ip/tcp/connection/TcpConnectionInterceptorSupport.java b/spring-integration-ip/src/main/java/org/springframework/integration/ip/tcp/connection/TcpConnectionInterceptorSupport.java index 7ebede2ef1..9a94c4aed0 100644 --- a/spring-integration-ip/src/main/java/org/springframework/integration/ip/tcp/connection/TcpConnectionInterceptorSupport.java +++ b/spring-integration-ip/src/main/java/org/springframework/integration/ip/tcp/connection/TcpConnectionInterceptorSupport.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2016 the original author or authors. + * Copyright 2002-2018 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -106,6 +106,11 @@ public abstract class TcpConnectionInterceptorSupport extends TcpConnectionSuppo return this.theConnection.getSocketInfo(); } + @Override + public String getConnectionFactoryName() { + return this.theConnection.getConnectionFactoryName(); + } + @Override public void run() { this.theConnection.run(); diff --git a/spring-integration-ip/src/main/java/org/springframework/integration/ip/tcp/connection/TcpConnectionSupport.java b/spring-integration-ip/src/main/java/org/springframework/integration/ip/tcp/connection/TcpConnectionSupport.java index fee3f2c6f0..decc184564 100644 --- a/spring-integration-ip/src/main/java/org/springframework/integration/ip/tcp/connection/TcpConnectionSupport.java +++ b/spring-integration-ip/src/main/java/org/springframework/integration/ip/tcp/connection/TcpConnectionSupport.java @@ -1,5 +1,5 @@ /* - * Copyright 2001-2016 the original author or authors. + * Copyright 2001-2018 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -328,6 +328,10 @@ public abstract class TcpConnectionSupport implements TcpConnection { return this.socketInfo; } + public String getConnectionFactoryName() { + return this.connectionFactoryName; + } + protected boolean isNoReadErrorOnClose() { return this.noReadErrorOnClose; } @@ -347,19 +351,19 @@ public abstract class TcpConnectionSupport implements TcpConnection { protected void publishConnectionOpenEvent() { TcpConnectionEvent event = new TcpConnectionOpenEvent(this, - this.connectionFactoryName); + getConnectionFactoryName()); doPublish(event); } protected void publishConnectionCloseEvent() { TcpConnectionEvent event = new TcpConnectionCloseEvent(this, - this.connectionFactoryName); + getConnectionFactoryName()); doPublish(event); } protected void publishConnectionExceptionEvent(Throwable t) { TcpConnectionEvent event = new TcpConnectionExceptionEvent(this, - this.connectionFactoryName, t); + getConnectionFactoryName(), t); doPublish(event); } diff --git a/spring-integration-ip/src/test/java/org/springframework/integration/ip/tcp/InterceptedSharedConnectionTests-context.xml b/spring-integration-ip/src/test/java/org/springframework/integration/ip/tcp/InterceptedSharedConnectionTests-context.xml index 190a0702ac..0e5d9ade13 100644 --- a/spring-integration-ip/src/test/java/org/springframework/integration/ip/tcp/InterceptedSharedConnectionTests-context.xml +++ b/spring-integration-ip/src/test/java/org/springframework/integration/ip/tcp/InterceptedSharedConnectionTests-context.xml @@ -71,4 +71,6 @@ + + diff --git a/spring-integration-ip/src/test/java/org/springframework/integration/ip/tcp/InterceptedSharedConnectionTests.java b/spring-integration-ip/src/test/java/org/springframework/integration/ip/tcp/InterceptedSharedConnectionTests.java index 3f25a5b7c9..1d2ed7a56d 100644 --- a/spring-integration-ip/src/test/java/org/springframework/integration/ip/tcp/InterceptedSharedConnectionTests.java +++ b/spring-integration-ip/src/test/java/org/springframework/integration/ip/tcp/InterceptedSharedConnectionTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2017 the original author or authors. + * Copyright 2002-2018 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,8 +16,11 @@ package org.springframework.integration.ip.tcp; +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.CoreMatchers.notNullValue; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertThat; import org.apache.log4j.Level; import org.apache.log4j.LogManager; @@ -27,11 +30,14 @@ import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.ApplicationListener; import org.springframework.context.support.AbstractApplicationContext; import org.springframework.integration.channel.QueueChannel; import org.springframework.integration.config.ConsumerEndpointFactoryBean; import org.springframework.integration.ip.tcp.connection.AbstractClientConnectionFactory; import org.springframework.integration.ip.tcp.connection.AbstractServerConnectionFactory; +import org.springframework.integration.ip.tcp.connection.HelloWorldInterceptor; +import org.springframework.integration.ip.tcp.connection.TcpConnectionOpenEvent; import org.springframework.integration.ip.util.TestingUtilities; import org.springframework.integration.support.MessageBuilder; import org.springframework.messaging.Message; @@ -58,6 +64,9 @@ public class InterceptedSharedConnectionTests { @Autowired AbstractClientConnectionFactory client; + @Autowired + Listener listener; + private static Level existingLogLevel; // temporary hooks to investigate CI failures @@ -95,6 +104,21 @@ public class InterceptedSharedConnectionTests { assertNotNull(message); assertEquals("Test", message.getPayload()); } + assertThat(this.listener.openEvent, notNullValue()); + assertThat(this.listener.openEvent.getConnectionFactoryName(), equalTo("client")); + } + + public static class Listener implements ApplicationListener { + + private volatile TcpConnectionOpenEvent openEvent; + + @Override + public void onApplicationEvent(TcpConnectionOpenEvent event) { + if (event.getSource() instanceof HelloWorldInterceptor) { + this.openEvent = event; + } + } + } }