List of usage examples for org.springframework.amqp.rabbit.connection AbstractConnectionFactory createConnection
Connection createConnection() throws AmqpException;
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//ww w . jav a 2s. 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 ww w . j a 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()); }
From source file:org.springframework.amqp.rabbit.connection.AbstractConnectionFactoryTests.java
@Test public void testCloseInvalidConnection() 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); when(mockConnectionFactory.newConnection(any(ExecutorService.class), anyString())) .thenReturn(mockConnection1, mockConnection2); // simulate a dead connection when(mockConnection1.isOpen()).thenReturn(false); AbstractConnectionFactory connectionFactory = createConnectionFactory(mockConnectionFactory); Connection connection = connectionFactory.createConnection(); // the dead connection should be discarded connection.createChannel(false);//from w w w . ja va2 s. c om verify(mockConnectionFactory, times(2)).newConnection(any(ExecutorService.class), anyString()); verify(mockConnection2, times(1)).createChannel(); connectionFactory.destroy(); verify(mockConnection2, times(1)).close(anyInt()); }
From source file:org.springframework.amqp.rabbit.connection.CachingConnectionFactoryTests.java
@Test public void testWithChannelListener() throws Exception { com.rabbitmq.client.ConnectionFactory mockConnectionFactory = mock( com.rabbitmq.client.ConnectionFactory.class); com.rabbitmq.client.Connection mockConnection = mock(com.rabbitmq.client.Connection.class); Channel mockChannel = mock(Channel.class); when(mockConnectionFactory.newConnection(any(ExecutorService.class), anyString())) .thenReturn(mockConnection); when(mockConnection.isOpen()).thenReturn(true); when(mockChannel.isOpen()).thenReturn(true); when(mockConnection.createChannel()).thenReturn(mockChannel); final AtomicInteger called = new AtomicInteger(0); AbstractConnectionFactory connectionFactory = createConnectionFactory(mockConnectionFactory); connectionFactory.setChannelListeners(Arrays.asList(new ChannelListener() { @Override/*from w ww.ja v a2 s . c o m*/ public void onCreate(Channel channel, boolean transactional) { called.incrementAndGet(); } })); ((CachingConnectionFactory) connectionFactory).setChannelCacheSize(1); Connection con = connectionFactory.createConnection(); Channel channel = con.createChannel(false); assertEquals(1, called.get()); channel.close(); con.close(); verify(mockConnection, never()).close(); connectionFactory.createConnection(); con.createChannel(false); assertEquals(1, called.get()); connectionFactory.destroy(); verify(mockConnection, atLeastOnce()).close(anyInt()); verify(mockConnectionFactory).newConnection(any(ExecutorService.class), anyString()); }
From source file:org.springframework.amqp.rabbit.connection.CachingConnectionFactoryTests.java
@Test public void testWithConnectionListener() throws Exception { com.rabbitmq.client.ConnectionFactory mockConnectionFactory = mock( com.rabbitmq.client.ConnectionFactory.class); com.rabbitmq.client.Connection mockConnection1 = mock(com.rabbitmq.client.Connection.class); when(mockConnection1.toString()).thenReturn("conn1"); com.rabbitmq.client.Connection mockConnection2 = mock(com.rabbitmq.client.Connection.class); when(mockConnection2.toString()).thenReturn("conn2"); Channel mockChannel = mock(Channel.class); when(mockConnectionFactory.newConnection(any(ExecutorService.class), anyString())) .thenReturn(mockConnection1, mockConnection2); when(mockConnection1.isOpen()).thenReturn(true); when(mockChannel.isOpen()).thenReturn(true); when(mockConnection1.createChannel()).thenReturn(mockChannel); when(mockConnection2.createChannel()).thenReturn(mockChannel); final AtomicReference<Connection> created = new AtomicReference<Connection>(); final AtomicReference<Connection> closed = new AtomicReference<Connection>(); final AtomicInteger timesClosed = new AtomicInteger(0); AbstractConnectionFactory connectionFactory = createConnectionFactory(mockConnectionFactory); connectionFactory.addConnectionListener(new ConnectionListener() { @Override//from ww w. j a va 2 s . com public void onCreate(Connection connection) { created.set(connection); } @Override public void onClose(Connection connection) { closed.set(connection); timesClosed.getAndAdd(1); } }); ((CachingConnectionFactory) connectionFactory).setChannelCacheSize(1); Connection con = connectionFactory.createConnection(); Channel channel = con.createChannel(false); assertSame(con, created.get()); channel.close(); con.close(); verify(mockConnection1, never()).close(); Connection same = connectionFactory.createConnection(); channel = con.createChannel(false); assertSame(con, same); channel.close(); com.rabbitmq.client.Connection conDelegate = targetDelegate(con); when(mockConnection1.isOpen()).thenReturn(false); when(mockChannel.isOpen()).thenReturn(false); // force a connection refresh channel.basicCancel("foo"); channel.close(); assertEquals(1, timesClosed.get()); Connection notSame = connectionFactory.createConnection(); assertNotSame(conDelegate, targetDelegate(notSame)); assertSame(con, closed.get()); assertSame(notSame, created.get()); connectionFactory.destroy(); verify(mockConnection2, atLeastOnce()).close(anyInt()); assertSame(notSame, closed.get()); assertEquals(2, timesClosed.get()); verify(mockConnectionFactory, times(2)).newConnection(any(ExecutorService.class), anyString()); }