logger.warn("Closing channel for unresponsive consumer: " + consumer); consumer.stop(); });
logger.warn("Closing channel for unresponsive consumer: " + consumer); consumer.stop(); });
consumer.stop(); logger.error("Error starting new consumer", e); this.cancellationLock.release(consumer);
logger.debug("Cancelling " + this.consumer); try { this.consumer.stop(); SimpleMessageListenerContainer.this.cancellationLock.release(this.consumer); if (getApplicationEventPublisher() != null) {
consumer.stop(); logger.error("Error starting new consumer", e); this.cancellationLock.release(consumer);
logger.debug("Cancelling " + this.consumer); try { this.consumer.stop(); SimpleMessageListenerContainer.this.cancellationLock.release(this.consumer); if (getApplicationEventPublisher() != null) {
try { consumer.stop();
try { consumer.stop();
@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 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"); }
.get("test"); isOpen.set(false); blockingQueueConsumer.stop(); verify(channel).basicCancel("consumerTag");
@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(); }
@Test public void testSendAndReceiveWithFanout() throws Exception { RabbitAdmin admin = new RabbitAdmin(connectionFactory); FanoutExchange exchange = new FanoutExchange("fanout"); admin.declareExchange(exchange); template.setExchange(exchange.getName()); admin.declareBinding(BindingBuilder.bind(queue).to(exchange)); template.execute(channel -> { BlockingQueueConsumer consumer = createConsumer(template); String tag = consumer.getConsumerTags().iterator().next(); assertNotNull(tag); try { template.convertAndSend("message"); String result = getResult(consumer); assertEquals("message", result); } finally { consumer.stop(); } return null; }); admin.deleteExchange("fanout"); }
@Test // @Ignore("Not sure yet if we need to support a use case like this") public void testSendAndReceiveWithTopicConsumeInBackground() throws Exception { RabbitAdmin admin = new RabbitAdmin(connectionFactory); TopicExchange exchange = new TopicExchange("topic"); admin.declareExchange(exchange); template.setExchange(exchange.getName()); admin.declareBinding(BindingBuilder.bind(queue).to(exchange).with("*.end")); final CachingConnectionFactory cachingConnectionFactory = new CachingConnectionFactory(); cachingConnectionFactory.setHost("localhost"); final RabbitTemplate template = new RabbitTemplate(cachingConnectionFactory); template.setExchange(exchange.getName()); BlockingQueueConsumer consumer = template.execute(channel -> { BlockingQueueConsumer consumer1 = createConsumer(template); String tag = consumer1.getConsumerTags().iterator().next(); assertNotNull(tag); return consumer1; }); template.convertAndSend("foo", "message"); String result = getResult(consumer); assertEquals(null, result); template.convertAndSend("foo.end", "message"); result = getResult(consumer); assertEquals("message", result); consumer.stop(); admin.deleteExchange("topic"); cachingConnectionFactory.destroy(); }