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

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

Introduction

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

Prototype

public final void setConnectionFactory(ConnectionFactory connectionFactory) 

Source Link

Document

Set the ConnectionFactory to use for obtaining RabbitMQ Connection Connections .

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 w  w  . j av a  2 s .  c o m*/
                "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:com.anton.dev.tqrbs2.QueueConsumeProcess.java

private void consume() {
    SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();
    container.setConnectionFactory(connectionFactory);
    container.setQueueNames("test.queue.1", "test.queue.2", "test.queue.3", "test.queue.4", "test.queue.5");
    container.setMessageListener(new MessageListenerAdapter(this));
    container.start();//  www .  j a v a  2s .c om
}

From source file:vn.topmedia.monitor.rabbit.client.RabbitClientConfiguration.java

@Bean
public SimpleMessageListenerContainer listenerContainer() {
    SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();
    container.setConnectionFactory(connectionFactory());
    container.setQueues(marketDataQueue());
    container.setMessageListener(messageListenerAdapter());
    return container;
}

From source file:com.vcredit.lrh.backend.RabbitConfiguration.java

@Bean
public SimpleMessageListenerContainer scheduleTaskContainer(ConnectionFactory connectionFactory) {
    if (!StringUtils.isEmpty(rabbitMQProperties.getScheduleTaskQueue())) {
        MessageListenerAdapter listener1 = new MessageListenerAdapter(scheduleTaskMessageReceiver,
                "receiveMessage");
        SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();
        container.setConnectionFactory(connectionFactory);
        container.setQueueNames(rabbitMQProperties.getScheduleTaskQueue());
        container.setMessageListener(listener1);
        return container;
    } else {//from   ww  w.j  ava  2 s . c om
        return null;
    }
}

From source file:be.rufer.playground.amqpclient.config.RabbitConfig.java

@Bean
public SimpleMessageListenerContainer listenerContainer() {
    SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();
    container.setConnectionFactory(connectionFactory());
    container.setQueueNames(this.queueName);
    container.setMessageListener(createMessageListenerAdapter());
    return container;
}

From source file:com.vcredit.lrh.backend.RabbitConfiguration.java

@Bean
public SimpleMessageListenerContainer appLogContainer(ConnectionFactory connectionFactory) {

    if (!StringUtils.isEmpty(rabbitMQProperties.getLogQueue())) {
        MessageListenerAdapter listener = new MessageListenerAdapter(aPPLogMessageReceiver, "receiveMessage");
        SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();
        container.setConnectionFactory(connectionFactory);
        container.setQueueNames(rabbitMQProperties.getLogQueue());
        container.setMessageListener(listener);
        return container;
    } else {/*from w w  w.j  a v  a2  s  .c  o m*/
        return null;
    }

}

From source file:lab.example.service.MessageListener.java

@Bean
SimpleMessageListenerContainer listenerContainer(ConnectionFactory connectionFactory,
        MessageListenerAdapter listenerAdapter) {
    SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();
    container.setConnectionFactory(connectionFactory);
    container.setQueueNames(personEventsQueueName);
    container.setMessageListener(listenerAdapter);

    return container;
}

From source file:de.msg.message.amqp.AmqpConfiguration.java

@Bean
public SimpleMessageListenerContainer container(ConnectionFactory connectionFactory,
        MessageListenerAdapter listenerAdapter, MessageConverter jsonMessageConverter) {
    SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();
    container.setConnectionFactory(connectionFactory);
    container.setQueueNames(QUEUE_NAME);
    container.setMessageConverter(jsonMessageConverter);
    container.setMessageListener(listenerAdapter);
    return container;
}

From source file:jp.co.ctc_g.rack.resource_manager.config.RackResourceManagerConsumerContextConfig.java

private SimpleMessageListenerContainer makeContainer() {

    SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();
    container.setConnectionFactory(factory());
    container.setErrorHandler(errorHandler());
    container.setAdviceChain(advice());/*w w  w  . j  a v a2 s  . co m*/
    return container;
}

From source file:com.sample.amqp.RabbitConfiguration.java

@Bean
public SimpleMessageListenerContainer listenerContainer() {
    SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();
    container.setConcurrentConsumers(5);
    container.setConnectionFactory(connectionFactory());
    container.setQueues(responseQueue());
    container.setMessageListener(rabbitTemplate());
    return container;
}