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.
This commit is contained in:
Gary Russell
2018-01-14 14:59:13 -05:00
committed by Artem Bilan
parent c4c4e51627
commit 032c8fa55d
4 changed files with 41 additions and 6 deletions

View File

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

View File

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

View File

@@ -71,4 +71,6 @@
<int:channel id="loop" />
<bean class="org.springframework.integration.ip.tcp.InterceptedSharedConnectionTests$Listener" />
</beans>

View File

@@ -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<TcpConnectionOpenEvent> {
private volatile TcpConnectionOpenEvent openEvent;
@Override
public void onApplicationEvent(TcpConnectionOpenEvent event) {
if (event.getSource() instanceof HelloWorldInterceptor) {
this.openEvent = event;
}
}
}
}