From b49a54dd849eeba845844e9aa59a94b036fcae7c Mon Sep 17 00:00:00 2001 From: Dave Syer Date: Wed, 6 Apr 2011 13:20:21 +0100 Subject: [PATCH] AMQP-150: add extra call to listener --- .../connection/SingleConnectionFactory.java | 8 ++++ .../SingleConnectionFactoryTests.java | 38 +++++++++++++++++++ 2 files changed, 46 insertions(+) diff --git a/spring-rabbit/src/main/java/org/springframework/amqp/rabbit/connection/SingleConnectionFactory.java b/spring-rabbit/src/main/java/org/springframework/amqp/rabbit/connection/SingleConnectionFactory.java index 7ca2b30f..4e94662a 100644 --- a/spring-rabbit/src/main/java/org/springframework/amqp/rabbit/connection/SingleConnectionFactory.java +++ b/spring-rabbit/src/main/java/org/springframework/amqp/rabbit/connection/SingleConnectionFactory.java @@ -130,10 +130,18 @@ public class SingleConnectionFactory implements ConnectionFactory, DisposableBea public void setConnectionListeners(List listeners) { this.listener.setDelegates(listeners); + // If the connection is already alive we assume that the new listeners want to be notified + if (this.connection != null) { + this.listener.onCreate(this.connection); + } } public void addConnectionListener(ConnectionListener listener) { this.listener.addDelegate(listener); + // If the connection is already alive we assume that the new listener wants to be notified + if (this.connection != null) { + listener.onCreate(this.connection); + } } public final Connection createConnection() throws AmqpException { diff --git a/spring-rabbit/src/test/java/org/springframework/amqp/rabbit/connection/SingleConnectionFactoryTests.java b/spring-rabbit/src/test/java/org/springframework/amqp/rabbit/connection/SingleConnectionFactoryTests.java index f100a6b4..00dcfbd9 100644 --- a/spring-rabbit/src/test/java/org/springframework/amqp/rabbit/connection/SingleConnectionFactoryTests.java +++ b/spring-rabbit/src/test/java/org/springframework/amqp/rabbit/connection/SingleConnectionFactoryTests.java @@ -56,6 +56,44 @@ public class SingleConnectionFactoryTests { } + @Test + public void testWithListenerRegisteredAfterOpen() throws IOException { + + com.rabbitmq.client.ConnectionFactory mockConnectionFactory = mock(com.rabbitmq.client.ConnectionFactory.class); + com.rabbitmq.client.Connection mockConnection = mock(com.rabbitmq.client.Connection.class); + + when(mockConnectionFactory.newConnection()).thenReturn(mockConnection); + + final AtomicInteger called = new AtomicInteger(0); + SingleConnectionFactory connectionFactory = new SingleConnectionFactory(mockConnectionFactory); + Connection con = connectionFactory.createConnection(); + assertEquals(0, called.get()); + + connectionFactory.setConnectionListeners(Arrays.asList(new ConnectionListener() { + public void onCreate(Connection connection) { + called.incrementAndGet(); + } + public void onClose(Connection connection) { + called.decrementAndGet(); + } + })); + assertEquals(1, called.get()); + + con.close(); + assertEquals(1, called.get()); + verify(mockConnection, never()).close(); + + connectionFactory.createConnection(); + assertEquals(1, called.get()); + + connectionFactory.destroy(); + assertEquals(0, called.get()); + verify(mockConnection, atLeastOnce()).close(); + + verify(mockConnectionFactory, times(1)).newConnection(); + + } + @Test public void testCloseInvalidConnection() throws Exception {