Example usage for org.springframework.amqp.rabbit.core RabbitTemplate RabbitTemplate

List of usage examples for org.springframework.amqp.rabbit.core RabbitTemplate RabbitTemplate

Introduction

In this page you can find the example usage for org.springframework.amqp.rabbit.core RabbitTemplate RabbitTemplate.

Prototype

public RabbitTemplate(ConnectionFactory connectionFactory) 

Source Link

Document

Create a rabbit template with default strategies and settings.

Usage

From source file:amqp.spring.camel.component.SpringAMQPConsumerTest.java

@Override
protected CamelContext createCamelContext() throws Exception {
    CachingConnectionFactory factory = new CachingConnectionFactory();
    RabbitTemplate amqpTemplate = new RabbitTemplate(factory);
    //The JSON converter stresses marshalling more than the default converter
    amqpTemplate.setMessageConverter(new JsonMessageConverter());
    SpringAMQPComponent amqpComponent = new SpringAMQPComponent(factory);
    amqpComponent.setAmqpTemplate(amqpTemplate);

    CamelContext camelContext = super.createCamelContext();
    camelContext.addComponent("spring-amqp", amqpComponent);
    return camelContext;
}

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

public void afterPropertiesSet() {
    if (this.connectionFactory == null) {
        throw new IllegalArgumentException("Property 'connectionFactory' is required");
    }//from  w w w  .  j  a v a  2  s . c om
    // initialize template
    this.template = new RabbitTemplate(this.connectionFactory);
    if (this.readTimeout > 0) {
        this.template.setReplyTimeout(readTimeout);
    }
    this.initializeQueues();

    // Add connection listener to recreate queue and reinitialize template when connection fall
    connectionFactory.addConnectionListener(new ConnectionListener() {

        public void onCreate(Connection connection) {
            initializeQueues();
        }

        public void onClose(Connection connection) {
        }

    });
}

From source file:org.opentestsystem.delivery.logging.RabbitConfiguration.java

@Bean
public RabbitTemplate rabbitTemplate(final ConnectionFactory connectionFactory) {
    return new RabbitTemplate(connectionFactory);
}

From source file:org.springframework.amqp.rabbit.AsyncRabbitTemplate.java

/**
 * Construct an instance using the provided arguments. If 'replyAddress' is null,
 * replies will be routed to the default exchange using the reply queue name as the
 * routing key. Otherwise it should have the form exchange/routingKey and must
 * cause messages to be routed to the reply queue.
 * @param connectionFactory the connection factory.
 * @param exchange the default exchange to which requests will be sent.
 * @param routingKey the default routing key.
 * @param replyQueue the name of the reply queue to listen for replies.
 * @param replyAddress the reply address (exchange/routingKey).
 *///from w w w . j  a v a 2  s  .c  o m
public AsyncRabbitTemplate(ConnectionFactory connectionFactory, String exchange, String routingKey,
        String replyQueue, String replyAddress) {
    Assert.notNull(connectionFactory, "'connectionFactory' cannot be null");
    Assert.notNull(routingKey, "'routingKey' cannot be null");
    Assert.notNull(replyQueue, "'replyQueue' cannot be null");
    this.template = new RabbitTemplate(connectionFactory);
    this.template.setExchange(exchange == null ? "" : exchange);
    this.template.setRoutingKey(routingKey);
    this.container = new SimpleMessageListenerContainer(connectionFactory);
    this.container.setQueueNames(replyQueue);
    this.container.setMessageListener(this);
    this.container.afterPropertiesSet();
    this.directReplyToContainer = null;
    if (replyAddress == null) {
        this.replyAddress = replyQueue;
    } else {
        this.replyAddress = replyAddress;
    }

}

From source file:org.springframework.amqp.rabbit.AsyncRabbitTemplate.java

/**
 * Construct an instance using the provided arguments. "Direct replyTo" is used for
 * replies./*from   w ww  .  j av  a2 s. co m*/
 * @param connectionFactory the connection factory.
 * @param exchange the default exchange to which requests will be sent.
 * @param routingKey the default routing key.
 * @since 2.0
 */
public AsyncRabbitTemplate(ConnectionFactory connectionFactory, String exchange, String routingKey) {
    Assert.notNull(connectionFactory, "'connectionFactory' cannot be null");
    Assert.notNull(routingKey, "'routingKey' cannot be null");
    this.template = new RabbitTemplate(connectionFactory);
    this.template.setExchange(exchange == null ? "" : exchange);
    this.template.setRoutingKey(routingKey);
    this.container = null;
    this.replyAddress = null;
    this.directReplyToContainer = new DirectReplyToMessageListenerContainer(
            this.template.getConnectionFactory());
    this.directReplyToContainer.setChannelAwareMessageListener(this);
}

From source file:org.springframework.amqp.rabbit.core.AsyncRabbitTemplate.java

/**
 * Construct an instance using the provided arguments. If 'replyAddress' is null,
 * replies will be routed to the default exchange using the reply queue name as the
 * routing key. Otherwise it should have the form exchange/routingKey and must
 * cause messages to be routed to the reply queue.
 * @param connectionFactory the connection factory.
 * @param exchange the default exchange to which requests will be sent.
 * @param routingKey the default routing key.
 * @param replyQueue the name of the reply queue to listen for replies.
 * @param replyAddress the reply address (exchange/routingKey).
 *//*  w  w  w.ja  v  a 2 s .  c  om*/
public AsyncRabbitTemplate(ConnectionFactory connectionFactory, String exchange, String routingKey,
        String replyQueue, String replyAddress) {
    Assert.notNull(connectionFactory, "'connectionFactory' cannot be null");
    Assert.notNull(routingKey, "'routingKey' cannot be null");
    Assert.notNull(replyQueue, "'replyQueue' cannot be null");
    this.template = new RabbitTemplate(connectionFactory);
    this.template.setExchange(exchange == null ? "" : exchange);
    this.template.setRoutingKey(routingKey);
    this.container = new SimpleMessageListenerContainer(connectionFactory);
    this.container.setQueueNames(replyQueue);
    this.container.setMessageListener(this);
    this.container.afterPropertiesSet();
    if (replyAddress == null) {
        this.replyAddress = replyQueue;
    } else {
        this.replyAddress = replyAddress;
    }

}

From source file:org.springframework.amqp.rabbit.core.RabbitAdmin.java

public RabbitAdmin(ConnectionFactory connectionFactory) {
    this.connectionFactory = connectionFactory;
    Assert.notNull(connectionFactory, "ConnectionFactory must not be null");
    this.rabbitTemplate = new RabbitTemplate(connectionFactory);
}

From source file:org.springframework.amqp.rabbit.core.RabbitAdminTests.java

@Test
public void testProperties() throws Exception {
    SingleConnectionFactory connectionFactory = new SingleConnectionFactory();
    connectionFactory.setHost("localhost");
    RabbitAdmin rabbitAdmin = new RabbitAdmin(connectionFactory);
    String queueName = "test.properties." + System.currentTimeMillis();
    try {//w w w.  j  ava2 s  .  c o  m
        rabbitAdmin.declareQueue(new Queue(queueName));
        new RabbitTemplate(connectionFactory).convertAndSend(queueName, "foo");
        int n = 0;
        while (n++ < 100 && messageCount(rabbitAdmin, queueName) == 0) {
            Thread.sleep(100);
        }
        assertTrue("Message count = 0", n < 100);
        Channel channel = connectionFactory.createConnection().createChannel(false);
        DefaultConsumer consumer = new DefaultConsumer(channel);
        channel.basicConsume(queueName, true, consumer);
        n = 0;
        while (n++ < 100 && messageCount(rabbitAdmin, queueName) > 0) {
            Thread.sleep(100);
        }
        assertTrue("Message count > 0", n < 100);
        Properties props = rabbitAdmin.getQueueProperties(queueName);
        assertNotNull(props.get(RabbitAdmin.QUEUE_CONSUMER_COUNT));
        assertEquals(1, props.get(RabbitAdmin.QUEUE_CONSUMER_COUNT));
        channel.close();
    } finally {
        rabbitAdmin.deleteQueue(queueName);
        connectionFactory.destroy();
    }
}

From source file:org.springframework.amqp.rabbit.core.RabbitGatewaySupport.java

/**
 * Create a RabbitTemplate for the given ConnectionFactory.
 * Only invoked if populating the gateway with a ConnectionFactory reference.
 *
 * @param connectionFactory the Rabbit ConnectionFactory to create a RabbitTemplate for
 * @return the new RabbitTemplate instance
 * @see #setConnectionFactory/*from w  ww .  j a v  a 2s . c  om*/
 */
protected RabbitTemplate createRabbitTemplate(ConnectionFactory connectionFactory) {
    return new RabbitTemplate(connectionFactory);
}

From source file:org.springframework.amqp.rabbit.core.RabbitTemplateIntegrationTests.java

@Before
public void create() {
    CachingConnectionFactory connectionFactory = new CachingConnectionFactory();
    connectionFactory.setPort(BrokerTestUtils.getPort());
    template = new RabbitTemplate(connectionFactory);
}