List of usage examples for org.springframework.amqp.rabbit.connection CachingConnectionFactory addConnectionListener
@Override public void addConnectionListener(ConnectionListener listener)
From source file:org.springframework.amqp.rabbit.connection.CachingConnectionFactoryTests.java
private void testCheckoutsWithRefreshedConnectionGuts(CacheMode mode) throws Exception { com.rabbitmq.client.ConnectionFactory mockConnectionFactory = mock( com.rabbitmq.client.ConnectionFactory.class); com.rabbitmq.client.Connection mockConnection1 = mock(com.rabbitmq.client.Connection.class); com.rabbitmq.client.Connection mockConnection2 = mock(com.rabbitmq.client.Connection.class); Channel mockChannel1 = mock(Channel.class); Channel mockChannel2 = mock(Channel.class); Channel mockChannel3 = mock(Channel.class); Channel mockChannel4 = mock(Channel.class); when(mockConnectionFactory.newConnection(any(ExecutorService.class), anyString())) .thenReturn(mockConnection1, mockConnection2); when(mockConnection1.createChannel()).thenReturn(mockChannel1, mockChannel2); when(mockConnection1.isOpen()).thenReturn(true); when(mockConnection2.createChannel()).thenReturn(mockChannel3, mockChannel4); when(mockConnection2.isOpen()).thenReturn(true); when(mockChannel1.isOpen()).thenReturn(true); when(mockChannel2.isOpen()).thenReturn(true); when(mockChannel3.isOpen()).thenReturn(true); when(mockChannel4.isOpen()).thenReturn(true); CachingConnectionFactory ccf = new CachingConnectionFactory(mockConnectionFactory); ccf.setExecutor(mock(ExecutorService.class)); ccf.setChannelCacheSize(2);/*from w w w . j a v a 2s . c o m*/ ccf.setChannelCheckoutTimeout(10); ccf.setCacheMode(mode); ccf.addConnectionListener(connection -> { try { // simulate admin connection.createChannel(false).close(); } catch (Exception e) { fail(e.getMessage()); } }); Connection con = ccf.createConnection(); Channel channel1 = con.createChannel(false); assertEquals(1, ((Semaphore) TestUtils.getPropertyValue(ccf, "checkoutPermits", Map.class).values() .iterator().next()).availablePermits()); channel1.close(); con.close(); assertEquals(2, ((Semaphore) TestUtils.getPropertyValue(ccf, "checkoutPermits", Map.class).values() .iterator().next()).availablePermits()); when(mockConnection1.isOpen()).thenReturn(false); when(mockChannel1.isOpen()).thenReturn(false); when(mockChannel2.isOpen()).thenReturn(false); con.createChannel(false).close(); con = ccf.createConnection(); con.createChannel(false).close(); con.createChannel(false).close(); con.createChannel(false).close(); con.createChannel(false).close(); con.createChannel(false).close(); verify(mockConnection1, times(1)).createChannel(); verify(mockConnection2, times(2)).createChannel(); con.close(); verify(mockConnection2, never()).close(); assertEquals(2, ((Semaphore) TestUtils.getPropertyValue(ccf, "checkoutPermits", Map.class).values() .iterator().next()).availablePermits()); ccf.destroy(); assertEquals(2, ((Semaphore) TestUtils.getPropertyValue(ccf, "checkoutPermits", Map.class).values() .iterator().next()).availablePermits()); }
From source file:org.springframework.amqp.rabbit.listener.DirectMessageListenerContainerIntegrationTests.java
@Test public void testNonManagedContainerDoesntStartWhenConnectionFactoryDestroyed() throws Exception { CachingConnectionFactory cf = new CachingConnectionFactory("localhost"); ApplicationContext context = mock(ApplicationContext.class); cf.setApplicationContext(context);//from w w w .j av a 2 s.c o m cf.addConnectionListener(connection -> { cf.onApplicationEvent(new ContextClosedEvent(context)); cf.stop(); cf.destroy(); }); DirectMessageListenerContainer container = new DirectMessageListenerContainer(cf); container.setMessageListener(m -> { }); container.setQueueNames(Q1); container.setBeanName("stopAfterDestroyBeforeStart"); container.afterPropertiesSet(); container.start(); int n = 0; while (n++ < 100 && container.isRunning()) { Thread.sleep(100); } assertFalse(container.isRunning()); }