List of usage examples for org.springframework.amqp.rabbit.connection Connection isOpen
boolean isOpen();
From source file:org.springframework.amqp.rabbit.connection.CachingConnectionFactoryTests.java
@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);//from w w w. j a v a 2s . co m }
From source file:org.springframework.amqp.rabbit.connection.CachingConnectionFactoryTests.java
@Test @Ignore // Test to verify log message is suppressed after patch to CCF public void testReturnsNormalCloseDeferredClose() 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); doThrow(new ShutdownSignalException(true, false, new com.rabbitmq.client.AMQP.Connection.Close.Builder().replyCode(200).replyText("OK").build(), null)).when(mockChannel).close(); CachingConnectionFactory ccf = new CachingConnectionFactory(mockConnectionFactory); ccf.setPublisherReturns(true);/* w w w .j a v a 2s.c o m*/ ccf.setExecutor(Executors.newSingleThreadExecutor()); Connection conn = ccf.createConnection(); Channel channel = conn.createChannel(false); RabbitUtils.setPhysicalCloseRequired(channel, true); channel.close(); Thread.sleep(6000); }
From source file:org.springframework.amqp.rabbit.connection.LocalizedQueueConnectionFactoryTests.java
@SuppressWarnings("unchecked") private ConnectionFactory mockCF(final String address, final CountDownLatch latch) throws Exception { ConnectionFactory connectionFactory = mock(ConnectionFactory.class); Connection connection = mock(Connection.class); Channel channel = mock(Channel.class); when(connectionFactory.createConnection()).thenReturn(connection); when(connection.createChannel(false)).thenReturn(channel); when(connection.isOpen()).thenReturn(true, false); when(channel.isOpen()).thenReturn(true, false); doAnswer(invocation -> {// w w w . ja va 2s . co m String tag = UUID.randomUUID().toString(); consumers.put(address, (Consumer) invocation.getArguments()[6]); consumerTags.put(address, tag); if (latch != null) { latch.countDown(); } return tag; }).when(channel).basicConsume(anyString(), anyBoolean(), anyString(), anyBoolean(), anyBoolean(), anyMap(), any(Consumer.class)); when(connectionFactory.getHost()).thenReturn(address); this.channels.put(address, channel); return connectionFactory; }
From source file:org.springframework.amqp.rabbit.connection.RabbitResourceHolder.java
public Connection getConnection() { if (this.connections.isEmpty()) { return null; }/*w ww. j a va2s .c o m*/ for (Connection conn : this.connections) { if (conn.isOpen()) { return conn; } } return null; }
From source file:org.springframework.amqp.rabbit.listener.DirectMessageListenerContainerIntegrationTests.java
@Test public void testRecoverBrokerLoss() throws Exception { ConnectionFactory mockCF = mock(ConnectionFactory.class); Connection connection = mock(Connection.class); Channel channel = mock(Channel.class); given(connection.isOpen()).willReturn(true); given(mockCF.createConnection()).willReturn(connection); given(connection.createChannel(false)).willReturn(channel); given(channel.isOpen()).willReturn(true); final CountDownLatch latch1 = new CountDownLatch(1); final CountDownLatch latch2 = new CountDownLatch(2); ArgumentCaptor<Consumer> consumerCaptor = ArgumentCaptor.forClass(Consumer.class); final String tag = "tag"; willAnswer(i -> {/*from w ww . ja va 2s. c o m*/ latch1.countDown(); latch2.countDown(); return tag; }).given(channel).basicConsume(eq("foo"), anyBoolean(), anyString(), anyBoolean(), anyBoolean(), anyMap(), consumerCaptor.capture()); DirectMessageListenerContainer container = new DirectMessageListenerContainer(mockCF); container.setQueueNames("foo"); container.setBeanName("brokerLost"); container.setConsumerTagStrategy(q -> "tag"); container.setShutdownTimeout(1); container.setMonitorInterval(200); container.setFailedDeclarationRetryInterval(200); container.afterPropertiesSet(); container.start(); assertTrue(latch1.await(10, TimeUnit.SECONDS)); given(channel.isOpen()).willReturn(false); assertTrue(latch2.await(10, TimeUnit.SECONDS)); container.setShutdownTimeout(1); container.stop(); }
From source file:org.springframework.amqp.rabbit.listener.DirectMessageListenerContainerIntegrationTests.java
@Test public void testCancelConsumerBeforeConsumeOk() throws Exception { ConnectionFactory mockCF = mock(ConnectionFactory.class); Connection connection = mock(Connection.class); Channel channel = mock(Channel.class); given(connection.isOpen()).willReturn(true); given(mockCF.createConnection()).willReturn(connection); given(connection.createChannel(false)).willReturn(channel); given(channel.isOpen()).willReturn(true); final CountDownLatch latch1 = new CountDownLatch(1); final CountDownLatch latch2 = new CountDownLatch(1); ArgumentCaptor<Consumer> consumerCaptor = ArgumentCaptor.forClass(Consumer.class); final String tag = "tag"; willAnswer(i -> {//from ww w .j ava 2 s.com latch1.countDown(); return tag; }).given(channel).basicConsume(eq("foo"), anyBoolean(), anyString(), anyBoolean(), anyBoolean(), anyMap(), consumerCaptor.capture()); DirectMessageListenerContainer container = new DirectMessageListenerContainer(mockCF); container.setQueueNames("foo"); container.setBeanName("backOff"); container.setConsumerTagStrategy(q -> "tag"); container.setShutdownTimeout(1); container.afterPropertiesSet(); container.start(); assertTrue(latch1.await(10, TimeUnit.SECONDS)); Consumer consumer = consumerCaptor.getValue(); Executors.newSingleThreadExecutor().execute(() -> { container.stop(); latch2.countDown(); }); assertTrue(latch2.await(10, TimeUnit.SECONDS)); verify(channel).basicCancel(tag); // canceled properly even without consumeOk consumer.handleCancelOk(tag); }
From source file:org.springframework.amqp.rabbit.listener.DirectMessageListenerContainerTests.java
@SuppressWarnings("unchecked") @Test/*from ww w . ja va 2 s .c o m*/ public void testRecoverBrokerLoss() throws Exception { ConnectionFactory mockCF = mock(ConnectionFactory.class); Connection connection = mock(Connection.class); Channel channel = mock(Channel.class); given(connection.isOpen()).willReturn(true); given(mockCF.createConnection()).willReturn(connection); given(connection.createChannel(false)).willReturn(channel); given(channel.isOpen()).willReturn(true); final CountDownLatch latch1 = new CountDownLatch(1); final CountDownLatch latch2 = new CountDownLatch(2); ArgumentCaptor<Consumer> consumerCaptor = ArgumentCaptor.forClass(Consumer.class); final String tag = "tag"; willAnswer(i -> { latch1.countDown(); latch2.countDown(); return tag; }).given(channel).basicConsume(eq("foo"), anyBoolean(), anyString(), anyBoolean(), anyBoolean(), anyMap(), consumerCaptor.capture()); DirectMessageListenerContainer container = new DirectMessageListenerContainer(mockCF); container.setQueueNames("foo"); container.setBeanName("brokerLost"); container.setConsumerTagStrategy(q -> "tag"); container.setShutdownTimeout(1); container.setMonitorInterval(200); container.setFailedDeclarationRetryInterval(200); container.afterPropertiesSet(); container.start(); assertTrue(latch1.await(10, TimeUnit.SECONDS)); given(channel.isOpen()).willReturn(false); assertTrue(latch2.await(10, TimeUnit.SECONDS)); container.setShutdownTimeout(1); container.stop(); }
From source file:org.springframework.amqp.rabbit.listener.DirectMessageListenerContainerTests.java
@SuppressWarnings("unchecked") @Test/* ww w . j a v a2 s . c om*/ public void testCancelConsumerBeforeConsumeOk() throws Exception { ConnectionFactory mockCF = mock(ConnectionFactory.class); Connection connection = mock(Connection.class); Channel channel = mock(Channel.class); given(connection.isOpen()).willReturn(true); given(mockCF.createConnection()).willReturn(connection); given(connection.createChannel(false)).willReturn(channel); given(channel.isOpen()).willReturn(true); final CountDownLatch latch1 = new CountDownLatch(1); final CountDownLatch latch2 = new CountDownLatch(1); ArgumentCaptor<Consumer> consumerCaptor = ArgumentCaptor.forClass(Consumer.class); final String tag = "tag"; willAnswer(i -> { latch1.countDown(); return tag; }).given(channel).basicConsume(eq("foo"), anyBoolean(), anyString(), anyBoolean(), anyBoolean(), anyMap(), consumerCaptor.capture()); DirectMessageListenerContainer container = new DirectMessageListenerContainer(mockCF); container.setQueueNames("foo"); container.setBeanName("backOff"); container.setConsumerTagStrategy(q -> "tag"); container.setShutdownTimeout(1); container.afterPropertiesSet(); container.start(); assertTrue(latch1.await(10, TimeUnit.SECONDS)); Consumer consumer = consumerCaptor.getValue(); Executors.newSingleThreadExecutor().execute(() -> { container.stop(); latch2.countDown(); }); assertTrue(latch2.await(10, TimeUnit.SECONDS)); verify(channel).basicCancel(tag); // canceled properly even without consumeOk consumer.handleCancelOk(tag); }
From source file:org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainerTests.java
@SuppressWarnings("unchecked") @Test/*from ww w. ja va 2 s. com*/ public void testWithConnectionPerListenerThread() 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); when(mockConnectionFactory.newConnection(any(ExecutorService.class), anyString())) .thenReturn(mockConnection1).thenReturn(mockConnection2).thenReturn(null); when(mockConnection1.createChannel()).thenReturn(mockChannel1).thenReturn(null); when(mockConnection2.createChannel()).thenReturn(mockChannel2).thenReturn(null); when(mockChannel1.isOpen()).thenReturn(true); when(mockConnection1.isOpen()).thenReturn(true); when(mockChannel2.isOpen()).thenReturn(true); when(mockConnection2.isOpen()).thenReturn(true); CachingConnectionFactory ccf = new CachingConnectionFactory(mockConnectionFactory); ccf.setCacheMode(CacheMode.CONNECTION); SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(ccf); container.setConcurrentConsumers(2); container.setQueueNames("foo"); container.afterPropertiesSet(); CountDownLatch latch1 = new CountDownLatch(2); CountDownLatch latch2 = new CountDownLatch(2); doAnswer(messageToConsumer(mockChannel1, container, false, latch1)).when(mockChannel1).basicConsume( anyString(), anyBoolean(), anyString(), anyBoolean(), anyBoolean(), anyMap(), any(Consumer.class)); doAnswer(messageToConsumer(mockChannel2, container, false, latch1)).when(mockChannel2).basicConsume( anyString(), anyBoolean(), anyString(), anyBoolean(), anyBoolean(), anyMap(), any(Consumer.class)); doAnswer(messageToConsumer(mockChannel1, container, true, latch2)).when(mockChannel1) .basicCancel(anyString()); doAnswer(messageToConsumer(mockChannel2, container, true, latch2)).when(mockChannel2) .basicCancel(anyString()); container.start(); assertTrue(latch1.await(10, TimeUnit.SECONDS)); Set<?> consumers = TestUtils.getPropertyValue(container, "consumers", Map.class).keySet(); container.stop(); assertTrue(latch2.await(10, TimeUnit.SECONDS)); waitForConsumersToStop(consumers); Set<?> allocatedConnections = TestUtils.getPropertyValue(ccf, "allocatedConnections", Set.class); assertEquals(2, allocatedConnections.size()); assertEquals("1", ccf.getCacheProperties().get("openConnections")); }
From source file:org.springframework.cloud.stream.binder.rabbit.LocalizedQueueConnectionFactoryTests.java
@SuppressWarnings("unchecked") private ConnectionFactory mockCF(final String address) throws Exception { ConnectionFactory connectionFactory = mock(ConnectionFactory.class); Connection connection = mock(Connection.class); Channel channel = mock(Channel.class); when(connectionFactory.createConnection()).thenReturn(connection); when(connection.createChannel(false)).thenReturn(channel); when(connection.isOpen()).thenReturn(true); when(channel.isOpen()).thenReturn(true); doAnswer(new Answer<String>() { @Override/*w ww.java2 s. c o m*/ public String answer(InvocationOnMock invocation) throws Throwable { String tag = UUID.randomUUID().toString(); consumers.put(address, (Consumer) invocation.getArguments()[6]); consumerTags.put(address, tag); latch.countDown(); return tag; } }).when(channel).basicConsume(anyString(), anyBoolean(), anyString(), anyBoolean(), anyBoolean(), anyMap(), any(Consumer.class)); when(connectionFactory.getHost()).thenReturn(address); this.cfs.put(address, connectionFactory); this.connections.put(address, connection); this.channels.put(address, channel); return connectionFactory; }