@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); }
@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"); }
blockingQueueConsumer.setRetryDeclarationInterval(10); blockingQueueConsumer.setFailedDeclarationRetryInterval(10); blockingQueueConsumer.start();
protected BlockingQueueConsumer createBlockingQueueConsumer() { BlockingQueueConsumer consumer; String[] queues = getQueueNames(); // There's no point prefetching less than the tx size, otherwise the consumer will stall because the broker // didn't get an ack for delivered messages int actualPrefetchCount = getPrefetchCount() > this.txSize ? getPrefetchCount() : this.txSize; consumer = new BlockingQueueConsumer(getConnectionFactory(), getMessagePropertiesConverter(), this.cancellationLock, getAcknowledgeMode(), isChannelTransacted(), actualPrefetchCount, isDefaultRequeueRejected(), getConsumerArguments(), isNoLocal(), isExclusive(), queues); if (this.declarationRetries != null) { consumer.setDeclarationRetries(this.declarationRetries); } if (getFailedDeclarationRetryInterval() > 0) { consumer.setFailedDeclarationRetryInterval(getFailedDeclarationRetryInterval()); } if (this.retryDeclarationInterval != null) { consumer.setRetryDeclarationInterval(this.retryDeclarationInterval); } if (getConsumerTagStrategy() != null) { consumer.setTagStrategy(getConsumerTagStrategy()); // NOSONAR never null here } consumer.setBackOffExecution(getRecoveryBackOff().start()); consumer.setShutdownTimeout(getShutdownTimeout()); consumer.setApplicationEventPublisher(getApplicationEventPublisher()); return consumer; }
protected BlockingQueueConsumer createBlockingQueueConsumer() { BlockingQueueConsumer consumer; String[] queues = getQueueNames(); // There's no point prefetching less than the tx size, otherwise the consumer will stall because the broker // didn't get an ack for delivered messages int actualPrefetchCount = getPrefetchCount() > this.txSize ? getPrefetchCount() : this.txSize; consumer = new BlockingQueueConsumer(getConnectionFactory(), getMessagePropertiesConverter(), this.cancellationLock, getAcknowledgeMode(), isChannelTransacted(), actualPrefetchCount, isDefaultRequeueRejected(), getConsumerArguments(), isNoLocal(), isExclusive(), queues); if (this.declarationRetries != null) { consumer.setDeclarationRetries(this.declarationRetries); } if (getFailedDeclarationRetryInterval() > 0) { consumer.setFailedDeclarationRetryInterval(getFailedDeclarationRetryInterval()); } if (this.retryDeclarationInterval != null) { consumer.setRetryDeclarationInterval(this.retryDeclarationInterval); } if (getConsumerTagStrategy() != null) { consumer.setTagStrategy(getConsumerTagStrategy()); // NOSONAR never null here } consumer.setBackOffExecution(getRecoveryBackOff().start()); consumer.setShutdownTimeout(getShutdownTimeout()); consumer.setApplicationEventPublisher(getApplicationEventPublisher()); return consumer; }