Example usage for org.springframework.amqp.rabbit.connection CachingConnectionFactory onApplicationEvent

List of usage examples for org.springframework.amqp.rabbit.connection CachingConnectionFactory onApplicationEvent

Introduction

In this page you can find the example usage for org.springframework.amqp.rabbit.connection CachingConnectionFactory onApplicationEvent.

Prototype

@Override
    public void onApplicationEvent(ContextClosedEvent event) 

Source Link

Usage

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

@Test
public void testNonManagedContainerDoesntStartWhenConnectionFactoryDestroyed() throws Exception {
    CachingConnectionFactory cf = new CachingConnectionFactory("localhost");
    ApplicationContext context = mock(ApplicationContext.class);
    cf.setApplicationContext(context);/* w  ww.j  a va 2  s. com*/

    cf.addConnectionListener(connection -> {
        cf.onApplicationEvent(new ContextClosedEvent(context));
        cf.stop();
        cf.destroy();
    });

    DirectMessageListenerContainer container = new DirectMessageListenerContainer(cf);
    container.setMessageListener(m -> {
    });
    container.setQueueNames(Q1);
    container.setBeanName("stopAfterDestroyBeforeStart");
    container.afterPropertiesSet();
    container.start();

    int n = 0;
    while (n++ < 100 && container.isRunning()) {
        Thread.sleep(100);
    }
    assertFalse(container.isRunning());
}

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);//from   w w w .j  a  va2  s  .co 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());
}