Example usage for org.springframework.amqp.rabbit.connection SingleConnectionFactory SingleConnectionFactory

List of usage examples for org.springframework.amqp.rabbit.connection SingleConnectionFactory SingleConnectionFactory

Introduction

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

Prototype

public SingleConnectionFactory(String hostname, int port) 

Source Link

Document

Create a new SingleConnectionFactory given a host name.

Usage

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

@Test
public void testRestartConsumerOnBasicQosIoException() throws Exception {
    this.template.convertAndSend(queue.getName(), "foo");

    ConnectionFactory connectionFactory = new SingleConnectionFactory("localhost", BrokerTestUtils.getPort());

    final AtomicBoolean networkGlitch = new AtomicBoolean();

    class MockChannel extends PublisherCallbackChannelImpl {

        MockChannel(Channel delegate) {
            super(delegate);
        }/*from  ww w. ja  va 2 s  .  c o m*/

        @Override
        public void basicQos(int prefetchCount) throws IOException {
            if (networkGlitch.compareAndSet(false, true)) {
                throw new IOException("Intentional connection reset");
            }
            super.basicQos(prefetchCount);
        }

    }

    Connection connection = spy(connectionFactory.createConnection());
    when(connection.createChannel(anyBoolean()))
            .then(invocation -> new MockChannel((Channel) invocation.callRealMethod()));

    DirectFieldAccessor dfa = new DirectFieldAccessor(connectionFactory);
    dfa.setPropertyValue("connection", connection);

    CountDownLatch latch = new CountDownLatch(1);
    SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(connectionFactory);
    container.setMessageListener(new MessageListenerAdapter(new PojoListener(latch)));
    container.setQueueNames(queue.getName());
    container.setRecoveryInterval(500);
    container.afterPropertiesSet();
    container.start();

    assertTrue(latch.await(10, TimeUnit.SECONDS));
    assertTrue(networkGlitch.get());

    container.stop();
    ((DisposableBean) connectionFactory).destroy();
}