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

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

Introduction

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

Prototype

private CachingConnectionFactory(com.rabbitmq.client.ConnectionFactory rabbitConnectionFactory,
        boolean isPublisherFactory) 

Source Link

Document

Create a new CachingConnectionFactory for the given target ConnectionFactory.

Usage

From source file:com.xoom.rabbit.test.Main.java

public static void main(String[] args) throws InterruptedException {
    if (args.length != 9) {
        System.out.println(/*from   w ww .  ja  v a2s  .com*/
                "usage: java -jar target/rabbit-tester-0.1-SNAPSHOT-standalone.jar [consumer_threads] [number_of_messages] [amqp_host] [amqp_port] [produce] [consume] [message size in bytes] [username] [password]");
        return;
    }
    final long startTime = System.currentTimeMillis();
    int consumerThreads = Integer.parseInt(args[0]);
    final int messages = Integer.parseInt(args[1]);
    String host = args[2];
    int port = Integer.parseInt(args[3]);
    boolean produce = Boolean.parseBoolean(args[4]);
    boolean consume = Boolean.parseBoolean(args[5]);
    final int messageSize = Integer.parseInt(args[6]);
    String username = args[7];
    String password = args[8];

    if (produce) {
        System.out.println("Sending " + messages + " messages to " + host + ":" + port);
    }
    if (consume) {
        System.out.println("Consuming " + messages + " messages from " + host + ":" + port);
    }
    if (!produce && !consume) {
        System.out.println("Not producing or consuming any messages.");
    }

    CachingConnectionFactory connectionFactory = new CachingConnectionFactory(host, port);
    connectionFactory.setUsername(username);
    connectionFactory.setPassword(password);
    connectionFactory.setChannelCacheSize(consumerThreads + 1);

    RabbitAdmin amqpAdmin = new RabbitAdmin(connectionFactory);

    DirectExchange exchange = new DirectExchange(EXCHANGE_NAME, true, false);
    Queue queue = new Queue(QUEUE_NAME);
    amqpAdmin.declareExchange(exchange);
    amqpAdmin.declareQueue(queue);
    amqpAdmin.declareBinding(BindingBuilder.bind(queue).to(exchange).with(ROUTING_KEY));

    final AmqpTemplate amqpTemplate = new RabbitTemplate(connectionFactory);

    final CountDownLatch producerLatch = new CountDownLatch(messages);
    final CountDownLatch consumerLatch = new CountDownLatch(messages);

    SimpleMessageListenerContainer listenerContainer = null;

    if (consume) {
        listenerContainer = new SimpleMessageListenerContainer();
        listenerContainer.setConnectionFactory(connectionFactory);
        listenerContainer.setQueueNames(QUEUE_NAME);
        listenerContainer.setConcurrentConsumers(consumerThreads);
        listenerContainer.setMessageListener(new MessageListener() {
            @Override
            public void onMessage(Message message) {
                if (consumerLatch.getCount() == 1) {
                    System.out.println("Finished consuming " + messages + " messages in "
                            + (System.currentTimeMillis() - startTime) + "ms");
                }
                consumerLatch.countDown();
            }
        });
        listenerContainer.start();
    }

    if (produce) {
        while (producerLatch.getCount() > 0) {
            try {
                byte[] message = new byte[messageSize];
                RND.nextBytes(message);
                amqpTemplate.send(EXCHANGE_NAME, ROUTING_KEY, new Message(message, new MessageProperties()));
                producerLatch.countDown();
            } catch (Exception e) {
                System.out.println("Failed to send message " + (messages - producerLatch.getCount())
                        + " will retry forever.");
            }
        }
    }

    if (consume) {
        consumerLatch.await();
        listenerContainer.shutdown();
    }

    connectionFactory.destroy();
}

From source file:org.resthub.rpc.AMQPHessianProxyTest.java

@BeforeClass
protected void setUp() throws Exception {
    connectionFactory = new CachingConnectionFactory("localhost", 5672);
    connectionFactory.setUsername("guest");
    connectionFactory.setPassword("guest");
}

From source file:org.resthub.rpc.hessian.AMQPProxyTest.java

@BeforeClass(groups = "hessian-serialization")
protected void setUp() throws Exception {
    connectionFactory = new CachingConnectionFactory("localhost", 5672);
    connectionFactory.setUsername("guest");
    connectionFactory.setPassword("guest");

}

From source file:org.resthub.rpc.java.AMQPProxyTest.java

@BeforeClass(groups = "java-serialization")
protected void setUp() throws Exception {
    connectionFactory = new CachingConnectionFactory("localhost", 5672);
    connectionFactory.setUsername("guest");
    connectionFactory.setPassword("guest");

    serializationHandler.setObjectOutputStreamFactory(new DefaultObjectOutputStreamFactory());
}

From source file:com.gopivotal.cloudfoundry.test.ApplicationConfiguration.java

@Bean
ConnectionFactory rabbitConnectionFactory() {
    return new CachingConnectionFactory(null, 0);
}

From source file:io.acme.solution.api.conf.RabbitConfigurer.java

@Bean
public ConnectionFactory getConnectionFactory() {

    log.info("Creating rabbit connection for command bus, publishing, on host: {" + this.hostname + "}");

    final CachingConnectionFactory connectionFactory = new CachingConnectionFactory(this.hostname, this.port);
    connectionFactory.setUsername(this.username);
    connectionFactory.setPassword(this.password);

    return connectionFactory;
}

From source file:io.acme.solution.infrastructure.conf.EventBusConfigurer.java

@Bean
@Primary/* www .ja va2s . co m*/
public ConnectionFactory eventBusConnectionFactory() {

    log.info("Creating event bus connection on hostname: {" + this.hostname + "}");

    final CachingConnectionFactory connectionFactory = new CachingConnectionFactory(this.hostname, this.port);
    connectionFactory.setUsername(this.username);
    connectionFactory.setPassword(this.password);

    return connectionFactory;
}

From source file:io.acme.solution.query.conf.EventBusConfigurer.java

@Bean
public ConnectionFactory eventBusConnectionFactory() {

    log.info("Creating query event bus connection on hostname: {" + this.hostname + "}");

    final CachingConnectionFactory connectionFactory = new CachingConnectionFactory(this.hostname, this.port);
    connectionFactory.setUsername(this.username);
    connectionFactory.setPassword(this.password);

    return connectionFactory;
}

From source file:io.acme.solution.application.conf.CommandBusConfigurer.java

@Bean
public ConnectionFactory commandBusConnectionFactory() {

    log.info("Creating command bus connection on hostname: {" + this.hostname + "}");

    final CachingConnectionFactory connectionFactory = new CachingConnectionFactory(this.hostname, this.port);
    connectionFactory.setUsername(this.username);
    connectionFactory.setPassword(this.password);

    return connectionFactory;
}

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

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

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

    final AtomicBoolean networkGlitch = new AtomicBoolean();

    class MockChannel extends PublisherCallbackChannelImpl {

        MockChannel(Channel delegate) {
            super(delegate);
        }//from   www.  java 2  s .  c  o  m

        @Override
        public DeclareOk queueDeclarePassive(String queue) throws IOException {
            if (networkGlitch.compareAndSet(false, true)) {
                getConnection().close();
                throw new IOException("Intentional connection reset");
            }
            return super.queueDeclarePassive(queue);
        }

    }

    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();
}