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

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

Introduction

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

Prototype

@Override
    @Nullable
    public Message receive(final String queueName, final long timeoutMillis) 

Source Link

Usage

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

@Test
public void testErrorHandler() throws Exception {
    brokerRunning.deleteQueues(Q1);/*from  w w  w  .ja  v a2 s. c  om*/
    Queue q1 = new Queue(Q1, true, false, false, new ArgumentBuilder().put("x-dead-letter-exchange", "")
            .put("x-dead-letter-routing-key", DLQ1).get());
    CachingConnectionFactory cf = new CachingConnectionFactory("localhost");
    RabbitAdmin admin = new RabbitAdmin(cf);
    admin.declareQueue(q1);
    DirectMessageListenerContainer container = new DirectMessageListenerContainer(cf);
    container.setQueueNames(Q1);
    container.setConsumersPerQueue(2);
    final AtomicReference<Channel> channel = new AtomicReference<>();
    container.setMessageListener((ChannelAwareMessageListener) (m, c) -> {
        channel.set(c);
        throw new MessageConversionException(
                "intended - should be wrapped in an AmqpRejectAndDontRequeueException");
    });
    container.setBeanName("errorHandler");
    container.setConsumerTagStrategy(new Tag());
    container.afterPropertiesSet();
    container.start();
    RabbitTemplate template = new RabbitTemplate(cf);
    template.convertAndSend(Q1, "foo");
    assertNotNull(template.receive(DLQ1, 10000));
    container.stop();
    assertTrue(consumersOnQueue(Q1, 0));
    assertTrue(consumersOnQueue(Q2, 0));
    assertTrue(activeConsumerCount(container, 0));
    assertEquals(0, TestUtils.getPropertyValue(container, "consumersByQueue", MultiValueMap.class).size());
    assertFalse(channel.get().isOpen());
    cf.destroy();
}

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

@Test
public void testErrorHandler() throws Exception {
    brokerRunning.getAdmin().deleteQueue(Q1);
    Queue q1 = new Queue(Q1, true, false, false, new ArgumentBuilder().put("x-dead-letter-exchange", "")
            .put("x-dead-letter-routing-key", DLQ1).get());
    brokerRunning.getAdmin().declareQueue(q1);
    CachingConnectionFactory cf = new CachingConnectionFactory("localhost");
    DirectMessageListenerContainer container = new DirectMessageListenerContainer(cf);
    container.setQueueNames(Q1);//from   w  ww .j  av a 2s  .  c om
    container.setConsumersPerQueue(2);
    final AtomicReference<Channel> channel = new AtomicReference<>();
    container.setMessageListener((ChannelAwareMessageListener) (m, c) -> {
        channel.set(c);
        throw new MessageConversionException(
                "intended - should be wrapped in an AmqpRejectAndDontRequeueException");
    });
    container.setBeanName("errorHandler");
    container.setConsumerTagStrategy(new Tag());
    container.afterPropertiesSet();
    container.start();
    RabbitTemplate template = new RabbitTemplate(cf);
    template.convertAndSend(Q1, "foo");
    assertNotNull(template.receive(DLQ1, 10000));
    container.stop();
    assertTrue(consumersOnQueue(Q1, 0));
    assertTrue(consumersOnQueue(Q2, 0));
    assertTrue(activeConsumerCount(container, 0));
    assertEquals(0, TestUtils.getPropertyValue(container, "consumersByQueue", MultiValueMap.class).size());
    assertFalse(channel.get().isOpen());
    cf.destroy();
}