AMQP-150: add extra call to listener

This commit is contained in:
Dave Syer
2011-04-06 13:20:21 +01:00
parent 666c51a6b9
commit b49a54dd84
2 changed files with 46 additions and 0 deletions

View File

@@ -130,10 +130,18 @@ public class SingleConnectionFactory implements ConnectionFactory, DisposableBea
public void setConnectionListeners(List<? extends ConnectionListener> 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 {

View File

@@ -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 {