@Test public void testNoLocalConsumerConfiguration() 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(anyBoolean())).thenReturn(channel); when(channel.isOpen()).thenReturn(true); final String queue = "testQ"; final boolean noLocal = true; BlockingQueueConsumer blockingQueueConsumer = new BlockingQueueConsumer(connectionFactory, new DefaultMessagePropertiesConverter(), new ActiveObjectCounter<BlockingQueueConsumer>(), AcknowledgeMode.AUTO, true, 1, true, null, noLocal, false, queue); blockingQueueConsumer.start(); verify(channel) .basicConsume(eq(queue), eq(AcknowledgeMode.AUTO.isAutoAck()), eq(""), eq(noLocal), eq(false), anyMap(), any(Consumer.class)); blockingQueueConsumer.stop(); }
@Test public void testAutoDeclareFalse() throws Exception { RabbitTemplate template = context.getBean(RabbitTemplate.class); template.convertAndSend("testContainerWithAutoDeleteQueues", "anon2", "foo"); assertNotNull(queue.poll(10, TimeUnit.SECONDS)); SimpleMessageListenerContainer listenerContainer = context.getBean("container4", SimpleMessageListenerContainer.class); listenerContainer.stop(); RabbitAdmin admin = spy(TestUtils.getPropertyValue(listenerContainer, "amqpAdmin", RabbitAdmin.class)); new DirectFieldAccessor(listenerContainer).setPropertyValue("amqpAdmin", admin); listenerContainer = spy(listenerContainer); //Prevent a long 'passiveDeclare' process BlockingQueueConsumer consumer = mock(BlockingQueueConsumer.class); doThrow(RuntimeException.class).when(consumer).start(); // when(consumer.getBackOffExecution()).thenReturn(mock(BackOffExecution.class)); when(listenerContainer.createBlockingQueueConsumer()).thenReturn(consumer); listenerContainer.start(); listenerContainer.stop(); verify(admin, never()).initialize(); // should not be called since 'autoDeclare = false' }
private void initialize() throws Throwable { // NOSONAR try { redeclareElementsIfNecessary(); this.consumer.start(); this.start.countDown();
private void initialize() throws Throwable { // NOSONAR try { redeclareElementsIfNecessary(); this.consumer.start(); this.start.countDown();
@Test public void testAlwaysCancelAutoRecoverConsumer() throws IOException { ConnectionFactory connectionFactory = mock(ConnectionFactory.class); Connection connection = mock(Connection.class); ChannelProxy channel = mock(ChannelProxy.class); Channel rabbitChannel = mock(AutorecoveringChannel.class); when(channel.getTargetChannel()).thenReturn(rabbitChannel); when(connectionFactory.createConnection()).thenReturn(connection); when(connection.createChannel(anyBoolean())).thenReturn(channel); final AtomicBoolean isOpen = new AtomicBoolean(true); doReturn(isOpen.get()).when(channel).isOpen(); when(channel.queueDeclarePassive(anyString())) .then(invocation -> mock(AMQP.Queue.DeclareOk.class)); doAnswer(i -> { ((Consumer) i.getArgument(6)).handleConsumeOk("consumerTag"); return "consumerTag"; }).when(channel).basicConsume(anyString(), anyBoolean(), anyString(), anyBoolean(), anyBoolean(), anyMap(), any(Consumer.class)); BlockingQueueConsumer blockingQueueConsumer = new BlockingQueueConsumer(connectionFactory, new DefaultMessagePropertiesConverter(), new ActiveObjectCounter<>(), AcknowledgeMode.AUTO, true, 2, "test"); blockingQueueConsumer.setDeclarationRetries(1); blockingQueueConsumer.setRetryDeclarationInterval(10); blockingQueueConsumer.setFailedDeclarationRetryInterval(10); blockingQueueConsumer.start(); verify(channel).basicQos(2); isOpen.set(false); blockingQueueConsumer.stop(); verify(channel).basicCancel("consumerTag"); }
@Test public void testPrefetchIsSetOnFailedPassiveDeclaration() throws IOException { ConnectionFactory connectionFactory = mock(ConnectionFactory.class); Connection connection = mock(Connection.class); Channel channel = mock(Channel.class); when(connectionFactory.createConnection()).thenReturn(connection); when(connection.createChannel(anyBoolean())).thenReturn(channel); when(channel.isOpen()).thenReturn(true); when(channel.queueDeclarePassive(anyString())) .then(invocation -> { String arg = invocation.getArgument(0); if ("good".equals(arg)) { return any(AMQP.Queue.DeclareOk.class); } else { throw new IOException(); } }); when(channel.basicConsume(anyString(), anyBoolean(), anyString(), anyBoolean(), anyBoolean(), anyMap(), any(Consumer.class))).thenReturn("consumerTag"); BlockingQueueConsumer blockingQueueConsumer = new BlockingQueueConsumer(connectionFactory, new DefaultMessagePropertiesConverter(), new ActiveObjectCounter<BlockingQueueConsumer>(), AcknowledgeMode.AUTO, true, 20, "good", "bad"); blockingQueueConsumer.setDeclarationRetries(1); blockingQueueConsumer.setRetryDeclarationInterval(10); blockingQueueConsumer.setFailedDeclarationRetryInterval(10); blockingQueueConsumer.start(); verify(channel).basicQos(20); }
private BlockingQueueConsumer createConsumer(RabbitAccessor accessor) { BlockingQueueConsumer consumer = new BlockingQueueConsumer( accessor.getConnectionFactory(), new DefaultMessagePropertiesConverter(), new ActiveObjectCounter<BlockingQueueConsumer>(), AcknowledgeMode.AUTO, true, 1, queue.getName()); consumer.start(); // wait for consumeOk... int n = 0; while (n++ < 100) { if (consumer.getConsumerTags().size() == 0) { try { Thread.sleep(100); } catch (InterruptedException e) { Thread.currentThread().interrupt(); break; } } } return consumer; }
@Test public void testContainerNotRecoveredAfterExhaustingRecoveryBackOff() throws Exception { SimpleMessageListenerContainer container = spy(new SimpleMessageListenerContainer(mock(ConnectionFactory.class))); container.setQueueNames("foo"); container.setRecoveryBackOff(new FixedBackOff(100, 3)); container.setConcurrentConsumers(3); doAnswer(invocation -> { BlockingQueueConsumer consumer = spy((BlockingQueueConsumer) invocation.callRealMethod()); doThrow(RuntimeException.class).when(consumer).start(); return consumer; }).when(container).createBlockingQueueConsumer(); container.afterPropertiesSet(); container.start(); // Since backOff exhausting makes listenerContainer as invalid (calls stop()), // it is enough to check the listenerContainer activity int n = 0; while (container.isActive() && n++ < 100) { Thread.sleep(100); } assertThat(n, lessThanOrEqualTo(100)); }
blockingQueueConsumer.setRetryDeclarationInterval(10); blockingQueueConsumer.setFailedDeclarationRetryInterval(10); blockingQueueConsumer.start();
@Test public void testAvoidHangAMQP_508() { CachingConnectionFactory connectionFactory = new CachingConnectionFactory("localhost"); String longName = new String(new byte[300]).replace('\u0000', 'x'); BlockingQueueConsumer blockingQueueConsumer = new BlockingQueueConsumer(connectionFactory, new DefaultMessagePropertiesConverter(), new ActiveObjectCounter<BlockingQueueConsumer>(), AcknowledgeMode.AUTO, true, 1, longName, "foobar"); try { blockingQueueConsumer.start(); fail("expected exception"); } catch (FatalListenerStartupException e) { assertThat(e.getCause(), instanceOf(IllegalArgumentException.class)); } connectionFactory.destroy(); }
final CountDownLatch latch = new CountDownLatch(1); Executors.newSingleThreadExecutor().execute(() -> { blockingQueueConsumer.start(); while (true) { try {
@Test public void testTransactionalLowLevel() throws Exception { RabbitTemplate template = new RabbitTemplate(); CachingConnectionFactory connectionFactory = new CachingConnectionFactory(); connectionFactory.setHost("localhost"); connectionFactory.setPort(BrokerTestUtils.getPort()); template.setConnectionFactory(connectionFactory); BlockingQueueConsumer blockingQueueConsumer = new BlockingQueueConsumer(connectionFactory, new DefaultMessagePropertiesConverter(), new ActiveObjectCounter<BlockingQueueConsumer>(), AcknowledgeMode.AUTO, true, 1, queue1.getName(), queue2.getName()); final String consumerTagPrefix = UUID.randomUUID().toString(); blockingQueueConsumer.setTagStrategy(queue -> consumerTagPrefix + '#' + queue); CountDownLatch latch = new CountDownLatch(2); List<ConsumeOkEvent> events = new ArrayList<>(); blockingQueueConsumer.setApplicationEventPublisher(e -> { if (e instanceof ConsumeOkEvent) { events.add((ConsumeOkEvent) e); latch.countDown(); } }); blockingQueueConsumer.start(); assertTrue(latch.await(10, TimeUnit.SECONDS)); assertThat(events.get(0).getConsumerTag(), equalTo(consumerTagPrefix + "#" + queue1.getName())); assertThat(events.get(1).getConsumerTag(), equalTo(consumerTagPrefix + "#" + queue2.getName())); blockingQueueConsumer.stop(); assertNull(template.receiveAndConvert(queue1.getName())); connectionFactory.destroy(); }