AMQP-150: add extra call to listener
This commit is contained in:
@@ -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 {
|
||||
|
||||
@@ -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 {
|
||||
|
||||
|
||||
Reference in New Issue
Block a user