List of usage examples for org.springframework.amqp.rabbit.connection AbstractConnectionFactory setConnectionListeners
public void setConnectionListeners(List<? extends ConnectionListener> listeners)
From source file:org.springframework.amqp.rabbit.connection.AbstractConnectionFactoryTests.java
@Test public void testWithListener() throws Exception { 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(any(ExecutorService.class), anyString())) .thenReturn(mockConnection); final AtomicInteger called = new AtomicInteger(0); AbstractConnectionFactory connectionFactory = createConnectionFactory(mockConnectionFactory); connectionFactory.setConnectionListeners(Collections.singletonList(new ConnectionListener() { @Override/* w ww . j a v a 2 s . c o m*/ public void onCreate(Connection connection) { called.incrementAndGet(); } @Override public void onClose(Connection connection) { called.decrementAndGet(); } })); Log logger = spy(TestUtils.getPropertyValue(connectionFactory, "logger", Log.class)); doReturn(true).when(logger).isInfoEnabled(); new DirectFieldAccessor(connectionFactory).setPropertyValue("logger", logger); Connection con = connectionFactory.createConnection(); assertEquals(1, called.get()); ArgumentCaptor<String> captor = ArgumentCaptor.forClass(String.class); verify(logger).info(captor.capture()); assertThat(captor.getValue(), containsString("Created new connection: SimpleConnection")); con.close(); assertEquals(1, called.get()); verify(mockConnection, never()).close(anyInt()); connectionFactory.createConnection(); assertEquals(1, called.get()); connectionFactory.destroy(); assertEquals(0, called.get()); verify(mockConnection, atLeastOnce()).close(anyInt()); verify(mockConnectionFactory, times(1)).newConnection(any(ExecutorService.class), anyString()); }
From source file:org.springframework.amqp.rabbit.connection.AbstractConnectionFactoryTests.java
@Test public void testWithListenerRegisteredAfterOpen() throws Exception { 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(any(ExecutorService.class), anyString())) .thenReturn(mockConnection); final AtomicInteger called = new AtomicInteger(0); AbstractConnectionFactory connectionFactory = createConnectionFactory(mockConnectionFactory); Connection con = connectionFactory.createConnection(); assertEquals(0, called.get());/*from w ww. ja v a 2s . c o m*/ connectionFactory.setConnectionListeners(Collections.singletonList(new ConnectionListener() { @Override public void onCreate(Connection connection) { called.incrementAndGet(); } @Override public void onClose(Connection connection) { called.decrementAndGet(); } })); assertEquals(1, called.get()); con.close(); assertEquals(1, called.get()); verify(mockConnection, never()).close(anyInt()); connectionFactory.createConnection(); assertEquals(1, called.get()); connectionFactory.destroy(); assertEquals(0, called.get()); verify(mockConnection, atLeastOnce()).close(anyInt()); verify(mockConnectionFactory, times(1)).newConnection(any(ExecutorService.class), anyString()); }