assertThat(received.getHeaders().get(AmqpHeaders.CONSUMER_QUEUE), equalTo("foo")); org.springframework.amqp.rabbit.connection.Connection conn = ccf.createConnection(); Channel notCached = conn.createChannel(false); // should not have been "closed" verify(connection, times(2)).createChannel();
@Override protected void obtainResource() throws Exception { resource = new CachingConnectionFactory("localhost"); resource.createConnection().close(); if (management) { Socket socket = SocketFactory.getDefault().createSocket("localhost", 15672); socket.close(); } }
@Override protected void obtainResource() throws Exception { resource = new CachingConnectionFactory("localhost"); resource.createConnection().close(); if (management) { Socket socket = SocketFactory.getDefault().createSocket("localhost", 15672); socket.close(); } }
@Bean public ConnectionFactory connectionFactory() throws IOException { CachingConnectionFactory connectionFactory = new CachingConnectionFactory("localhost"); connectionFactory.setUsername("guest"); connectionFactory.setPassword("guest"); Connection conn = connectionFactory.createConnection(); Channel channel = conn.createChannel(false); // declare exchnage AMQP.Exchange.DeclareOk resEx = channel.exchangeDeclare(AmqpTemp.SPRING_BOOT_EXCHANGE_test, ExchangeTypes.FANOUT, true, false, false, null); // declares queues AMQP.Queue.DeclareOk resQ = channel.queueDeclare(AmqpTemp.Q2, true, false, false, null); resQ = channel.queueDeclare(AmqpTemp.Q3, true, false, false, null); // declare binding AMQP.Queue.BindOk resB = channel.queueBind(AmqpTemp.Q2, AmqpTemp.SPRING_BOOT_EXCHANGE_test, ""); resB = channel.queueBind(AmqpTemp.Q3, AmqpTemp.SPRING_BOOT_EXCHANGE_test, ""); // channel.queueBind(queueName2, SPRING_BOOT_EXCHANGE, ""); return connectionFactory; }
@Test public void testConnectionCloseLog() { Log logger = spy(TestUtils.getPropertyValue(this.connectionFactory, "logger", Log.class)); new DirectFieldAccessor(this.connectionFactory).setPropertyValue("logger", logger); Connection conn = this.connectionFactory.createConnection(); conn.createChannel(false); this.connectionFactory.destroy(); verify(logger, never()).error(anyString()); }
@Test public void setUri() throws Exception { URI uri = new URI("amqp://localhost:1234/%2f"); ConnectionFactory mock = mock(com.rabbitmq.client.ConnectionFactory.class); CachingConnectionFactory ccf = new CachingConnectionFactory(mock); ccf.setExecutor(mock(ExecutorService.class)); ccf.setUri(uri); ccf.createConnection(); InOrder order = inOrder(mock); order.verify(mock).isAutomaticRecoveryEnabled(); order.verify(mock).setUri(uri); Log logger = TestUtils.getPropertyValue(ccf, "logger", Log.class); if (logger.isInfoEnabled()) { order.verify(mock).getHost(); order.verify(mock).getPort(); } order.verify(mock).newConnection(any(ExecutorService.class), anyString()); verifyNoMoreInteractions(mock); }
@Test public void setAddressesOneHost() throws Exception { ConnectionFactory mock = mock(com.rabbitmq.client.ConnectionFactory.class); CachingConnectionFactory ccf = new CachingConnectionFactory(mock); ccf.setAddresses("mq1"); ccf.createConnection(); verify(mock).isAutomaticRecoveryEnabled(); verify(mock) .newConnection(isNull(), aryEq(new Address[] { new Address("mq1") }), anyString()); verifyNoMoreInteractions(mock); }
@Test public void setAddressesTwoHosts() throws Exception { ConnectionFactory mock = mock(com.rabbitmq.client.ConnectionFactory.class); doReturn(true).when(mock).isAutomaticRecoveryEnabled(); CachingConnectionFactory ccf = new CachingConnectionFactory(mock); ccf.setAddresses("mq1,mq2"); ccf.createConnection(); verify(mock).isAutomaticRecoveryEnabled(); verify(mock).setAutomaticRecoveryEnabled(false); verify(mock).newConnection(isNull(), aryEq(new Address[] { new Address("mq1"), new Address("mq2") }), anyString()); verifyNoMoreInteractions(mock); }
@Test public void setAddressesEmpty() throws Exception { ConnectionFactory mock = mock(com.rabbitmq.client.ConnectionFactory.class); CachingConnectionFactory ccf = new CachingConnectionFactory(mock); ccf.setExecutor(mock(ExecutorService.class)); ccf.setHost("abc"); ccf.setAddresses(""); ccf.createConnection(); verify(mock).isAutomaticRecoveryEnabled(); verify(mock).setHost("abc"); Log logger = TestUtils.getPropertyValue(ccf, "logger", Log.class); if (logger.isInfoEnabled()) { verify(mock).getHost(); verify(mock).getPort(); } verify(mock).newConnection(any(ExecutorService.class), anyString()); verifyNoMoreInteractions(mock); }
@Test public void testChannelCloseIdempotency() 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(mockConnection.createChannel()).thenReturn(mockChannel); when(mockChannel.isOpen()).thenReturn(true).thenReturn(false); CachingConnectionFactory ccf = new CachingConnectionFactory(mockConnectionFactory); ccf.setExecutor(mock(ExecutorService.class)); Connection con = ccf.createConnection(); Channel channel = con.createChannel(false); // RabbitUtils.setPhysicalCloseRequired(true); channel.close(); // should be ignored, and placed into channel cache. channel.close(); // physically closed, so remove from the cache. channel.close(); // physically closed and removed from the cache before, so void "close". Channel channel2 = con.createChannel(false); assertNotSame(channel, channel2); }
@Test public void testDestroy() { Connection connection1 = this.connectionFactory.createConnection(); this.connectionFactory.destroy(); Connection connection2 = this.connectionFactory.createConnection(); assertSame(connection1, connection2); ApplicationContext context = mock(ApplicationContext.class); this.connectionFactory.setApplicationContext(context); this.connectionFactory.onApplicationEvent(new ContextClosedEvent(context)); this.connectionFactory.destroy(); try { connection2 = this.connectionFactory.createConnection(); fail("Expected exception"); } catch (AmqpApplicationContextClosedException e) { assertThat(e.getMessage(), containsString("is closed")); } }
@Test public void testConnectionName() { Connection connection = this.connectionFactory.createConnection(); com.rabbitmq.client.Connection rabbitConnection = TestUtils.getPropertyValue(connection, "target.delegate", com.rabbitmq.client.Connection.class); assertEquals(CF_INTEGRATION_CONNECTION_NAME, rabbitConnection.getClientProperties().get("connection_name")); this.connectionFactory.destroy(); }
@Test public void connectionName() { Connection conn = this.connectionFactory.createConnection(); conn.close(); assertThat(conn.getDelegate().getClientProvidedName(), equalTo("testConnectionName")); }
@Test public void testConnectionFactoryAvailableDuringStop() { AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(Config.class); MyLifecycle myLifecycle = context.getBean(MyLifecycle.class); CachingConnectionFactory cf = context.getBean(CachingConnectionFactory.class); context.close(); assertFalse(myLifecycle.isRunning()); assertTrue(TestUtils.getPropertyValue(cf, "stopped", Boolean.class)); try { cf.createConnection(); fail("Expected exception"); } catch (AmqpApplicationContextClosedException e) { assertThat(e.getMessage(), containsString("The ApplicationContext is closed")); } }
@Test(expected = AmqpResourceNotAvailableException.class) public void testChannelMax() { this.connectionFactory.getRabbitConnectionFactory().setRequestedChannelMax(1); Connection connection = this.connectionFactory.createConnection(); connection.createChannel(true); connection.createChannel(false); }
@Test public void testNoOpWhenNothingToDeclare() throws Exception { com.rabbitmq.client.ConnectionFactory cf = mock(com.rabbitmq.client.ConnectionFactory.class); com.rabbitmq.client.Connection connection = mock(com.rabbitmq.client.Connection.class); Channel channel = mock(Channel.class, "channel1"); given(channel.isOpen()).willReturn(true); willReturn(connection).given(cf).newConnection(any(ExecutorService.class), anyString()); given(connection.isOpen()).willReturn(true); given(connection.createChannel()).willReturn(channel); CachingConnectionFactory ccf = new CachingConnectionFactory(cf); ccf.setExecutor(mock(ExecutorService.class)); RabbitTemplate rabbitTemplate = new RabbitTemplate(ccf); RabbitAdmin admin = new RabbitAdmin(rabbitTemplate); ApplicationContext ac = mock(ApplicationContext.class); admin.setApplicationContext(ac); admin.afterPropertiesSet(); ccf.createConnection(); verify(connection, never()).createChannel(); }
@Test public void testListenerRecoversFromClosedConnection() throws Exception { ConnectionFactory connectionFactory1 = createConnectionFactory(); RabbitTemplate template = new RabbitTemplate(connectionFactory1); CountDownLatch latch = new CountDownLatch(messageCount); CachingConnectionFactory connectionFactory2 = createConnectionFactory(); // this test closes the underlying connection normally; it won't automatically recover. connectionFactory2.getRabbitConnectionFactory().setAutomaticRecoveryEnabled(false); container = createContainer(queue.getName(), new CloseConnectionListener((ConnectionProxy) connectionFactory2.createConnection(), latch), connectionFactory2); for (int i = 0; i < messageCount; i++) { template.convertAndSend(queue.getName(), i + "foo"); } int timeout = Math.min(4 + messageCount / (4 * concurrentConsumers), 30); logger.debug("Waiting for messages with timeout = " + timeout + " (s)"); boolean waited = latch.await(timeout, TimeUnit.SECONDS); assertTrue("Timed out waiting for message", waited); assertNull(template.receiveAndConvert(queue.getName())); this.container.stop(); ((DisposableBean) connectionFactory1).destroy(); ((DisposableBean) connectionFactory2).destroy(); }
@Test public void testQueueWithAutoDelete() throws Exception { final Queue queue = new Queue("test.queue", false, true, true); context.getBeanFactory().registerSingleton("foo", queue); rabbitAdmin.afterPropertiesSet(); // Queue created on spring startup rabbitAdmin.initialize(); assertTrue(queueExists(queue)); // Stop and broker deletes queue (only verifiable in native API) connectionFactory.destroy(); assertFalse(queueExists(queue)); // Start and queue re-created by the connection listener connectionFactory.createConnection(); assertTrue(queueExists(queue)); // Queue manually deleted assertTrue(rabbitAdmin.deleteQueue(queue.getName())); assertFalse(queueExists(queue)); }
@Test public void testQueueWithoutAutoDelete() throws Exception { final Queue queue = new Queue("test.queue", false, false, false); context.getBeanFactory().registerSingleton("foo", queue); rabbitAdmin.afterPropertiesSet(); // Queue created on Spring startup rabbitAdmin.initialize(); assertTrue(queueExists(queue)); // Stop and broker retains queue (only verifiable in native API) connectionFactory.destroy(); assertTrue(queueExists(queue)); // Start and queue still exists connectionFactory.createConnection(); assertTrue(queueExists(queue)); // Queue manually deleted assertTrue(rabbitAdmin.deleteQueue(queue.getName())); assertFalse(queueExists(queue)); connectionFactory.destroy(); }
@Test public void testQueueWithoutName() throws Exception { final Queue queue = new Queue("", true, false, true); String generatedName = rabbitAdmin.declareQueue(queue); assertEquals("", queue.getName()); Queue queueWithGeneratedName = new Queue(generatedName, true, false, true); assertTrue(queueExists(queueWithGeneratedName)); // Stop and broker retains queue (only verifiable in native API) connectionFactory.destroy(); assertTrue(queueExists(queueWithGeneratedName)); // Start and queue still exists connectionFactory.createConnection(); assertTrue(queueExists(queueWithGeneratedName)); // Queue manually deleted assertTrue(rabbitAdmin.deleteQueue(generatedName)); assertFalse(queueExists(queueWithGeneratedName)); connectionFactory.destroy(); }