Example usage for org.springframework.amqp.rabbit.listener DirectMessageListenerContainer setIdleEventInterval

List of usage examples for org.springframework.amqp.rabbit.listener DirectMessageListenerContainer setIdleEventInterval

Introduction

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

Prototype

public void setIdleEventInterval(long idleEventInterval) 

Source Link

Document

How often to emit ListenerContainerIdleEvent s in milliseconds.

Usage

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

@Test
public void testEvents() throws Exception {
    CachingConnectionFactory cf = new CachingConnectionFactory("localhost");
    DirectMessageListenerContainer container = new DirectMessageListenerContainer(cf);
    container.setQueueNames(EQ1, EQ2);//  ww w .j  a  v a 2  s .c  om
    final List<Long> times = new ArrayList<>();
    final CountDownLatch latch1 = new CountDownLatch(2);
    final AtomicReference<ApplicationEvent> failEvent = new AtomicReference<>();
    final CountDownLatch latch2 = new CountDownLatch(2);
    container.setApplicationEventPublisher(event -> {
        if (event instanceof ListenerContainerIdleEvent) {
            times.add(System.currentTimeMillis());
            latch1.countDown();
        } else {
            failEvent.set((ApplicationEvent) event);
            latch2.countDown();
        }
    });
    container.setMessageListener(m -> {
    });
    container.setIdleEventInterval(50L);
    container.setBeanName("events");
    container.setConsumerTagStrategy(new Tag());
    container.afterPropertiesSet();
    container.start();
    assertTrue(latch1.await(10, TimeUnit.SECONDS));
    assertThat(times.get(1) - times.get(0), greaterThanOrEqualTo(50L));
    brokerRunning.deleteQueues(EQ1, EQ2);
    assertTrue(latch2.await(10, TimeUnit.SECONDS));
    assertNotNull(failEvent.get());
    assertThat(failEvent.get(), instanceOf(ListenerContainerConsumerTerminatedEvent.class));
    container.stop();
    cf.destroy();
}

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

@Test
public void testNonManagedContainerStopsWhenConnectionFactoryDestroyed() throws Exception {
    CachingConnectionFactory cf = new CachingConnectionFactory("localhost");
    ApplicationContext context = mock(ApplicationContext.class);
    cf.setApplicationContext(context);//  w  ww  .  j  av a  2s . c o  m
    DirectMessageListenerContainer container = new DirectMessageListenerContainer(cf);
    final CountDownLatch latch = new CountDownLatch(1);
    container.setMessageListener(m -> {
        latch.countDown();
    });
    container.setQueueNames(Q1);
    container.setBeanName("stopAfterDestroy");
    container.setIdleEventInterval(500);
    container.setFailedDeclarationRetryInterval(500);
    container.afterPropertiesSet();
    container.start();
    new RabbitTemplate(cf).convertAndSend(Q1, "foo");
    assertTrue(latch.await(10, TimeUnit.SECONDS));
    cf.onApplicationEvent(new ContextClosedEvent(context));
    cf.stop();
    cf.destroy();
    int n = 0;
    while (n++ < 100 && container.isRunning()) {
        Thread.sleep(100);
    }
    assertFalse(container.isRunning());
}

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

@Test
public void testEvents() throws Exception {
    CachingConnectionFactory cf = new CachingConnectionFactory("localhost");
    DirectMessageListenerContainer container = new DirectMessageListenerContainer(cf);
    container.setQueueNames(EQ1, EQ2);//from   www .ja v a 2 s.  c o  m
    final List<Long> times = new ArrayList<>();
    final CountDownLatch latch1 = new CountDownLatch(2);
    final AtomicReference<ApplicationEvent> failEvent = new AtomicReference<>();
    final CountDownLatch latch2 = new CountDownLatch(2);
    container.setApplicationEventPublisher(new ApplicationEventPublisher() {

        @Override
        public void publishEvent(Object event) {
        }

        @Override
        public void publishEvent(ApplicationEvent event) {
            if (event instanceof ListenerContainerIdleEvent) {
                times.add(System.currentTimeMillis());
                latch1.countDown();
            } else {
                failEvent.set(event);
                latch2.countDown();
            }
        }

    });
    container.setMessageListener(m -> {
    });
    container.setIdleEventInterval(50L);
    container.setBeanName("events");
    container.setConsumerTagStrategy(new Tag());
    container.afterPropertiesSet();
    container.start();
    assertTrue(latch1.await(10, TimeUnit.SECONDS));
    assertThat(times.get(1) - times.get(0), greaterThanOrEqualTo(50L));
    brokerRunning.getAdmin().deleteQueue(EQ1);
    brokerRunning.getAdmin().deleteQueue(EQ2);
    assertTrue(latch2.await(10, TimeUnit.SECONDS));
    assertNotNull(failEvent.get());
    assertThat(failEvent.get(), instanceOf(ListenerContainerConsumerTerminatedEvent.class));
    container.stop();
    cf.destroy();
}