Example usage for org.springframework.amqp.rabbit.listener SimpleMessageListenerContainer getActiveConsumerCount

List of usage examples for org.springframework.amqp.rabbit.listener SimpleMessageListenerContainer getActiveConsumerCount

Introduction

In this page you can find the example usage for org.springframework.amqp.rabbit.listener SimpleMessageListenerContainer getActiveConsumerCount.

Prototype

@ManagedMetric(metricType = MetricType.GAUGE)
    public int getActiveConsumerCount() 

Source Link

Usage

From source file:org.springframework.amqp.rabbit.listener.MessageListenerContainerMultipleQueueIntegrationTests.java

private void doTest(int concurrentConsumers, ContainerConfigurer configurer) {
    int messageCount = 10;
    RabbitTemplate template = new RabbitTemplate();
    CachingConnectionFactory connectionFactory = new CachingConnectionFactory();
    connectionFactory.setChannelCacheSize(concurrentConsumers);
    connectionFactory.setPort(BrokerTestUtils.getPort());
    template.setConnectionFactory(connectionFactory);
    SimpleMessageConverter messageConverter = new SimpleMessageConverter();
    messageConverter.setCreateMessageIds(true);
    template.setMessageConverter(messageConverter);
    for (int i = 0; i < messageCount; i++) {
        template.convertAndSend(queue1.getName(), new Integer(i));
        template.convertAndSend(queue2.getName(), new Integer(i));
    }// w w  w  .  j  av a  2s .co m
    final SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(connectionFactory);
    final CountDownLatch latch = new CountDownLatch(messageCount * 2);
    PojoListener listener = new PojoListener(latch);
    container.setMessageListener(new MessageListenerAdapter(listener));
    container.setAcknowledgeMode(AcknowledgeMode.AUTO);
    container.setChannelTransacted(true);
    container.setConcurrentConsumers(concurrentConsumers);
    configurer.configure(container);
    container.afterPropertiesSet();
    container.start();
    try {
        int timeout = Math.min(1 + messageCount / concurrentConsumers, 30);
        boolean waited = latch.await(timeout, TimeUnit.SECONDS);
        logger.info("All messages recovered: " + waited);
        assertEquals(concurrentConsumers, container.getActiveConsumerCount());
        assertTrue("Timed out waiting for messages", waited);
    } catch (InterruptedException e) {
        Thread.currentThread().interrupt();
        throw new IllegalStateException("unexpected interruption");
    } finally {
        container.shutdown();
        assertEquals(0, container.getActiveConsumerCount());
    }
    assertNull(template.receiveAndConvert(queue1.getName()));
    assertNull(template.receiveAndConvert(queue2.getName()));
}

From source file:org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainerIntegration2Tests.java

@Test
public void testExclusive() throws Exception {
    Log logger = spy(TestUtils.getPropertyValue(this.template.getConnectionFactory(), "logger", Log.class));
    doReturn(true).when(logger).isInfoEnabled();
    new DirectFieldAccessor(this.template.getConnectionFactory()).setPropertyValue("logger", logger);
    CountDownLatch latch1 = new CountDownLatch(1000);
    SimpleMessageListenerContainer container1 = new SimpleMessageListenerContainer(
            template.getConnectionFactory());
    container1.setMessageListener(new MessageListenerAdapter(new PojoListener(latch1)));
    container1.setQueueNames(queue.getName());
    GenericApplicationContext context = new GenericApplicationContext();
    context.getBeanFactory().registerSingleton("foo", queue);
    context.refresh();//from www . j av a 2  s.c  om
    container1.setApplicationContext(context);
    container1.setExclusive(true);
    container1.afterPropertiesSet();
    container1.start();
    int n = 0;
    while (n++ < 100 && container1.getActiveConsumerCount() < 1) {
        Thread.sleep(100);
    }
    assertTrue(n < 100);
    CountDownLatch latch2 = new CountDownLatch(1000);
    SimpleMessageListenerContainer container2 = new SimpleMessageListenerContainer(
            template.getConnectionFactory());
    container2.setMessageListener(new MessageListenerAdapter(new PojoListener(latch2)));
    container2.setQueueNames(queue.getName());
    container2.setApplicationContext(context);
    container2.setRecoveryInterval(1000);
    container2.setExclusive(true); // not really necessary, but likely people will make all consumers exclusive.
    ApplicationEventPublisher publisher = mock(ApplicationEventPublisher.class);
    container2.setApplicationEventPublisher(publisher);
    container2.afterPropertiesSet();
    Log containerLogger = spy(TestUtils.getPropertyValue(container2, "logger", Log.class));
    doReturn(true).when(containerLogger).isWarnEnabled();
    new DirectFieldAccessor(container2).setPropertyValue("logger", containerLogger);
    container2.start();
    for (int i = 0; i < 1000; i++) {
        template.convertAndSend(queue.getName(), i + "foo");
    }
    assertTrue(latch1.await(10, TimeUnit.SECONDS));
    assertEquals(1000, latch2.getCount());
    container1.stop();
    // container 2 should recover and process the next batch of messages
    for (int i = 0; i < 1000; i++) {
        template.convertAndSend(queue.getName(), i + "foo");
    }
    assertTrue(latch2.await(10, TimeUnit.SECONDS));
    container2.stop();
    ArgumentCaptor<String> captor = ArgumentCaptor.forClass(String.class);
    verify(logger, atLeastOnce()).info(captor.capture());
    assertThat(captor.getAllValues(), contains(containsString("exclusive")));
    ArgumentCaptor<ListenerContainerConsumerFailedEvent> eventCaptor = ArgumentCaptor
            .forClass(ListenerContainerConsumerFailedEvent.class);
    verify(publisher).publishEvent(eventCaptor.capture());
    ListenerContainerConsumerFailedEvent event = eventCaptor.getValue();
    assertEquals("Consumer raised exception, attempting restart", event.getReason());
    assertFalse(event.isFatal());
    assertThat(event.getThrowable(), instanceOf(AmqpIOException.class));
    verify(containerLogger).warn(any());
}

From source file:org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainerLongTests.java

@Test
public void testAddQueuesAndStartInCycle() throws Exception {
    final SingleConnectionFactory connectionFactory = new SingleConnectionFactory("localhost");
    final SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(connectionFactory);
    container.setMessageListener((MessageListener) message -> {
    });/* w  w  w.  jav a2s.c o  m*/
    container.setConcurrentConsumers(2);
    container.afterPropertiesSet();

    RabbitAdmin admin = new RabbitAdmin(connectionFactory);
    for (int i = 0; i < 20; i++) {
        AnonymousQueue anonymousQueue = new AnonymousQueue();
        admin.declareQueue(anonymousQueue);
        container.addQueueNames(anonymousQueue.getName());
        if (!container.isRunning()) {
            container.start();
        }
    }

    int n = 0;
    while (n++ < 100 && container.getActiveConsumerCount() != 2) {
        Thread.sleep(100);
    }
    assertEquals(2, container.getActiveConsumerCount());
    container.stop();
    connectionFactory.destroy();
}