List of usage examples for org.springframework.amqp.rabbit.listener SimpleMessageListenerContainer stop
@Override public void stop()
From source file:org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainerTests.java
@Test public void testDefaultConsumerCount() throws Exception { final SingleConnectionFactory singleConnectionFactory = new SingleConnectionFactory("localhost"); SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(singleConnectionFactory); container.setMessageListener(new MessageListenerAdapter(this)); container.setQueueNames("foo"); container.setAutoStartup(false);/*from w w w . j av a 2 s .c o m*/ container.afterPropertiesSet(); assertEquals(1, ReflectionTestUtils.getField(container, "concurrentConsumers")); container.stop(); singleConnectionFactory.destroy(); }
From source file:org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainerTests.java
@Test public void testLazyConsumerCount() throws Exception { final SingleConnectionFactory singleConnectionFactory = new SingleConnectionFactory("localhost"); SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(singleConnectionFactory) { @Override/* ww w .j av a2 s . com*/ protected void doStart() throws Exception { // do nothing } }; container.start(); assertEquals(1, ReflectionTestUtils.getField(container, "concurrentConsumers")); container.stop(); singleConnectionFactory.destroy(); }
From source file:org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainerTests.java
@SuppressWarnings("unchecked") @Test/*w w w. ja v a2s. co m*/ public void testTxSizeAcks() 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); final AtomicReference<Consumer> consumer = new AtomicReference<Consumer>(); doAnswer(invocation -> { consumer.set((Consumer) invocation.getArguments()[6]); consumer.get().handleConsumeOk("1"); return "1"; }).when(channel).basicConsume(anyString(), anyBoolean(), anyString(), anyBoolean(), anyBoolean(), anyMap(), any(Consumer.class)); final CountDownLatch latch = new CountDownLatch(2); doAnswer(invocation -> { latch.countDown(); return null; }).when(channel).basicAck(anyLong(), anyBoolean()); final List<Message> messages = new ArrayList<Message>(); final SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(connectionFactory); container.setQueueNames("foo"); container.setTxSize(2); container.setMessageListener((MessageListener) message -> messages.add(message)); container.start(); BasicProperties props = new BasicProperties(); byte[] payload = "baz".getBytes(); Envelope envelope = new Envelope(1L, false, "foo", "bar"); consumer.get().handleDelivery("1", envelope, props, payload); envelope = new Envelope(2L, false, "foo", "bar"); consumer.get().handleDelivery("1", envelope, props, payload); envelope = new Envelope(3L, false, "foo", "bar"); consumer.get().handleDelivery("1", envelope, props, payload); envelope = new Envelope(4L, false, "foo", "bar"); consumer.get().handleDelivery("1", envelope, props, payload); assertTrue(latch.await(5, TimeUnit.SECONDS)); assertEquals(4, messages.size()); Executors.newSingleThreadExecutor().execute(() -> container.stop()); consumer.get().handleCancelOk("1"); verify(channel, times(2)).basicAck(anyLong(), anyBoolean()); verify(channel).basicAck(2, true); verify(channel).basicAck(4, true); container.stop(); }
From source file:org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainerTests.java
@SuppressWarnings("unchecked") @Test//from w ww . j ava 2 s . c om public void testTxSizeAcksWIthShortSet() 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); final AtomicReference<Consumer> consumer = new AtomicReference<Consumer>(); final String consumerTag = "1"; doAnswer(invocation -> { consumer.set((Consumer) invocation.getArguments()[6]); consumer.get().handleConsumeOk(consumerTag); return consumerTag; }).when(channel).basicConsume(anyString(), anyBoolean(), anyString(), anyBoolean(), anyBoolean(), anyMap(), any(Consumer.class)); final CountDownLatch latch = new CountDownLatch(2); doAnswer(invocation -> { latch.countDown(); return null; }).when(channel).basicAck(anyLong(), anyBoolean()); final List<Message> messages = new ArrayList<Message>(); final SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(connectionFactory); container.setQueueNames("foobar"); container.setTxSize(2); container.setMessageListener((MessageListener) message -> messages.add(message)); container.start(); BasicProperties props = new BasicProperties(); byte[] payload = "baz".getBytes(); Envelope envelope = new Envelope(1L, false, "foo", "bar"); consumer.get().handleDelivery(consumerTag, envelope, props, payload); envelope = new Envelope(2L, false, "foo", "bar"); consumer.get().handleDelivery(consumerTag, envelope, props, payload); envelope = new Envelope(3L, false, "foo", "bar"); consumer.get().handleDelivery(consumerTag, envelope, props, payload); assertTrue(latch.await(5, TimeUnit.SECONDS)); assertEquals(3, messages.size()); assertEquals(consumerTag, messages.get(0).getMessageProperties().getConsumerTag()); assertEquals("foobar", messages.get(0).getMessageProperties().getConsumerQueue()); Executors.newSingleThreadExecutor().execute(() -> container.stop()); consumer.get().handleCancelOk(consumerTag); verify(channel, times(2)).basicAck(anyLong(), anyBoolean()); verify(channel).basicAck(2, true); // second set was short verify(channel).basicAck(3, true); container.stop(); }
From source file:org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainerTests.java
@SuppressWarnings("unchecked") @Test//from w ww. j ava2s. c o m public void testConsumerArgs() 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); final AtomicReference<Consumer> consumer = new AtomicReference<Consumer>(); final AtomicReference<Map<?, ?>> args = new AtomicReference<Map<?, ?>>(); doAnswer(invocation -> { consumer.set((Consumer) invocation.getArguments()[6]); consumer.get().handleConsumeOk("foo"); args.set((Map<?, ?>) invocation.getArguments()[5]); return "foo"; }).when(channel).basicConsume(anyString(), anyBoolean(), anyString(), anyBoolean(), anyBoolean(), any(Map.class), any(Consumer.class)); final SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(connectionFactory); container.setQueueNames("foo"); container.setMessageListener((MessageListener) message -> { }); container.setConsumerArguments(Collections.<String, Object>singletonMap("x-priority", Integer.valueOf(10))); container.afterPropertiesSet(); container.start(); verify(channel).basicConsume(anyString(), anyBoolean(), anyString(), anyBoolean(), anyBoolean(), any(Map.class), any(Consumer.class)); assertTrue(args.get() != null); assertEquals(10, args.get().get("x-priority")); consumer.get().handleCancelOk("foo"); container.stop(); }
From source file:org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainerTests.java
@Test public void testChangeQueues() throws Exception { ConnectionFactory connectionFactory = mock(ConnectionFactory.class); Connection connection = mock(Connection.class); Channel channel1 = mock(Channel.class); Channel channel2 = mock(Channel.class); when(channel1.isOpen()).thenReturn(true); when(channel2.isOpen()).thenReturn(true); when(connectionFactory.createConnection()).thenReturn(connection); when(connection.createChannel(false)).thenReturn(channel1, channel2); List<Consumer> consumers = new ArrayList<Consumer>(); AtomicInteger consumerTag = new AtomicInteger(); CountDownLatch latch1 = new CountDownLatch(1); CountDownLatch latch2 = new CountDownLatch(2); setupMockConsume(channel1, consumers, consumerTag, latch1); setUpMockCancel(channel1, consumers); setupMockConsume(channel2, consumers, consumerTag, latch2); setUpMockCancel(channel2, consumers); final SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(connectionFactory); container.setQueueNames("foo"); container.setReceiveTimeout(1);//from w w w .j a v a 2 s. co m container.setMessageListener((MessageListener) message -> { }); container.afterPropertiesSet(); container.start(); assertTrue(latch1.await(10, TimeUnit.SECONDS)); container.addQueueNames("bar"); assertTrue(latch2.await(10, TimeUnit.SECONDS)); container.stop(); verify(channel1).basicCancel("0"); verify(channel2, atLeastOnce()).basicCancel("1"); verify(channel2, atLeastOnce()).basicCancel("2"); }
From source file:org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainerTests.java
@Test public void testChangeQueuesSimple() throws Exception { ConnectionFactory connectionFactory = mock(ConnectionFactory.class); final SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(connectionFactory); container.setQueueNames("foo"); List<?> queues = TestUtils.getPropertyValue(container, "queueNames", List.class); assertEquals(1, queues.size());/*from w ww. j ava 2 s .c o m*/ container.addQueues(new AnonymousQueue(), new AnonymousQueue()); assertEquals(3, queues.size()); container.removeQueues(new Queue("foo")); assertEquals(2, queues.size()); container.stop(); }
From source file:org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainerTests.java
@SuppressWarnings("unchecked") @Test// w w w .ja va 2 s .co m public void testAddQueuesAndStartInCycle() throws Exception { ConnectionFactory connectionFactory = mock(ConnectionFactory.class); Connection connection = mock(Connection.class); Channel channel1 = mock(Channel.class); when(channel1.isOpen()).thenReturn(true); when(connectionFactory.createConnection()).thenReturn(connection); when(connection.createChannel(false)).thenReturn(channel1); final AtomicInteger count = new AtomicInteger(); doAnswer(invocation -> { Consumer cons = (Consumer) invocation.getArguments()[6]; String consumerTag = "consFoo" + count.incrementAndGet(); cons.handleConsumeOk(consumerTag); return consumerTag; }).when(channel1).basicConsume(anyString(), anyBoolean(), anyString(), anyBoolean(), anyBoolean(), anyMap(), any(Consumer.class)); final SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(connectionFactory); container.setMessageListener((MessageListener) message -> { }); container.afterPropertiesSet(); for (int i = 0; i < 10; i++) { container.addQueueNames("foo" + i); if (!container.isRunning()) { container.start(); } } container.stop(); }
From source file:org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainerTests.java
@SuppressWarnings("unchecked") @Test/*from w w w . ja v a2 s . co m*/ 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.amqp.rabbit.listener.SimpleMessageListenerContainerTests.java
@SuppressWarnings("unchecked") @Test/*w w w .j a va2 s . c om*/ public void testConsumerCancel() 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); final AtomicReference<Consumer> consumer = new AtomicReference<Consumer>(); doAnswer(invocation -> { consumer.set((Consumer) invocation.getArguments()[6]); consumer.get().handleConsumeOk("foo"); return "foo"; }).when(channel).basicConsume(anyString(), anyBoolean(), anyString(), anyBoolean(), anyBoolean(), anyMap(), any(Consumer.class)); final SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(connectionFactory); container.setQueueNames("foo"); container.setMessageListener((MessageListener) message -> { }); container.setReceiveTimeout(1); container.afterPropertiesSet(); container.start(); verify(channel).basicConsume(anyString(), anyBoolean(), anyString(), anyBoolean(), anyBoolean(), anyMap(), any(Consumer.class)); Log logger = spy(TestUtils.getPropertyValue(container, "logger", Log.class)); doReturn(false).when(logger).isDebugEnabled(); final CountDownLatch latch = new CountDownLatch(1); doAnswer(invocation -> { if (((String) invocation.getArguments()[0]).startsWith("Consumer raised exception")) { latch.countDown(); } return invocation.callRealMethod(); }).when(logger).warn(any()); new DirectFieldAccessor(container).setPropertyValue("logger", logger); consumer.get().handleCancel("foo"); assertTrue(latch.await(10, TimeUnit.SECONDS)); container.stop(); }