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:org.springframework.xd.dirt.integration.bus.rabbit.RabbitMessageBusTests.java

@Test
public void testDurablePubSubWithAutoBindDLQ() throws Exception {
    RabbitAdmin admin = new RabbitAdmin(this.rabbitAvailableRule.getResource());

    MessageBus bus = getMessageBus();/*from w ww.ja  va2s  .  c om*/
    Properties properties = new Properties();
    properties.put("prefix", "xdbustest.");
    properties.put("autoBindDLQ", "true");
    properties.put("durableSubscription", "true");
    properties.put("maxAttempts", "1"); // disable retry
    properties.put("requeue", "false");
    DirectChannel moduleInputChannel = new DirectChannel();
    moduleInputChannel.setBeanName("durableTest");
    moduleInputChannel.subscribe(new MessageHandler() {

        @Override
        public void handleMessage(Message<?> message) throws MessagingException {
            throw new RuntimeException("foo");
        }

    });
    bus.bindPubSubConsumer("teststream.tap:stream:durabletest.0", moduleInputChannel, properties);

    RabbitTemplate template = new RabbitTemplate(this.rabbitAvailableRule.getResource());
    template.convertAndSend("xdbustest.topic.tap:stream:durabletest.0", "", "foo");

    int n = 0;
    while (n++ < 100) {
        Object deadLetter = template.receiveAndConvert("xdbustest.teststream.tap:stream:durabletest.0.dlq");
        if (deadLetter != null) {
            assertEquals("foo", deadLetter);
            break;
        }
        Thread.sleep(100);
    }
    assertTrue(n < 100);

    bus.unbindConsumer("teststream.tap:stream:durabletest.0", moduleInputChannel);
    assertNotNull(admin.getQueueProperties("xdbustest.teststream.tap:stream:durabletest.0.dlq"));
    admin.deleteQueue("xdbustest.teststream.tap:stream:durabletest.0.dlq");
    admin.deleteQueue("xdbustest.teststream.tap:stream:durabletest.0");
    admin.deleteExchange("xdbustest.topic.tap:stream:durabletest.0");
    admin.deleteExchange("xdbustest.DLX");
}

From source file:org.springframework.xd.dirt.integration.bus.rabbit.RabbitMessageBusTests.java

@Test
public void testAutoBindDLQ() throws Exception {
    RabbitAdmin admin = new RabbitAdmin(this.rabbitAvailableRule.getResource());

    MessageBus bus = getMessageBus();/*from  w  ww  .  j  ava2  s . c o  m*/
    Properties properties = new Properties();
    properties.put("prefix", "xdbustest.");
    properties.put("autoBindDLQ", "true");
    properties.put("maxAttempts", "1"); // disable retry
    properties.put("requeue", "false");
    DirectChannel moduleInputChannel = new DirectChannel();
    moduleInputChannel.setBeanName("dlqTest");
    moduleInputChannel.subscribe(new MessageHandler() {

        @Override
        public void handleMessage(Message<?> message) throws MessagingException {
            throw new RuntimeException("foo");
        }

    });
    bus.bindConsumer("dlqtest", moduleInputChannel, properties);

    RabbitTemplate template = new RabbitTemplate(this.rabbitAvailableRule.getResource());
    template.convertAndSend("", "xdbustest.dlqtest", "foo");

    int n = 0;
    while (n++ < 100) {
        Object deadLetter = template.receiveAndConvert("xdbustest.dlqtest.dlq");
        if (deadLetter != null) {
            assertEquals("foo", deadLetter);
            break;
        }
        Thread.sleep(100);
    }
    assertTrue(n < 100);

    bus.unbindConsumer("dlqtest", moduleInputChannel);
    admin.deleteQueue("xdbustest.dlqtest.dlq");
    admin.deleteQueue("xdbustest.dlqtest");
    admin.deleteExchange("xdbustest.DLX");
}

From source file:org.springframework.xd.dirt.integration.bus.rabbit.RabbitMessageBusTests.java

@Test
public void testAutoBindDLQwithRepublish() throws Exception {
    // pre-declare the queue with dead-lettering, users can also use a policy
    RabbitAdmin admin = new RabbitAdmin(this.rabbitAvailableRule.getResource());
    Map<String, Object> args = new HashMap<String, Object>();
    args.put("x-dead-letter-exchange", "xdbustest.DLX");
    Queue queue = new Queue("xdbustest.dlqpubtest", true, false, false, args);
    admin.declareQueue(queue);/*from w w  w.  j  av  a2s  .  c om*/

    MessageBus bus = getMessageBus();
    Properties properties = new Properties();
    properties.put("prefix", "xdbustest.");
    properties.put("autoBindDLQ", "true");
    properties.put("republishToDLQ", "true");
    properties.put("maxAttempts", "1"); // disable retry
    properties.put("requeue", "false");
    DirectChannel moduleInputChannel = new DirectChannel();
    moduleInputChannel.setBeanName("dlqPubTest");
    moduleInputChannel.subscribe(new MessageHandler() {

        @Override
        public void handleMessage(Message<?> message) throws MessagingException {
            throw new RuntimeException("foo");
        }

    });
    bus.bindConsumer("dlqpubtest", moduleInputChannel, properties);

    RabbitTemplate template = new RabbitTemplate(this.rabbitAvailableRule.getResource());
    template.convertAndSend("", "xdbustest.dlqpubtest", "foo");

    int n = 0;
    while (n++ < 100) {
        org.springframework.amqp.core.Message deadLetter = template.receive("xdbustest.dlqpubtest.dlq");
        if (deadLetter != null) {
            assertEquals("foo", new String(deadLetter.getBody()));
            assertNotNull(deadLetter.getMessageProperties().getHeaders().get("x-exception-stacktrace"));
            break;
        }
        Thread.sleep(100);
    }
    assertTrue(n < 100);

    bus.unbindConsumer("dlqpubtest", moduleInputChannel);
    admin.deleteQueue("xdbustest.dlqpubtest.dlq");
    admin.deleteQueue("xdbustest.dlqpubtest");
    admin.deleteExchange("xdbustest.DLX");
}

From source file:org.springframework.xd.dirt.integration.bus.rabbit.RabbitMessageBusTests.java

@SuppressWarnings("unchecked")
@Test/*from ww  w. j ava 2  s .  c  o m*/
public void testBatchingAndCompression() throws Exception {
    RabbitTemplate template = new RabbitTemplate(this.rabbitAvailableRule.getResource());
    MessageBus bus = getMessageBus();
    Properties properties = new Properties();
    properties.put("deliveryMode", "NON_PERSISTENT");
    properties.put("batchingEnabled", "true");
    properties.put("batchSize", "2");
    properties.put("batchBufferLimit", "100000");
    properties.put("batchTimeout", "30000");
    properties.put("compress", "true");

    DirectChannel output = new DirectChannel();
    output.setBeanName("batchingProducer");
    bus.bindProducer("batching.0", output, properties);

    while (template.receive("xdbus.batching.0") != null) {
    }

    Log logger = spy(TestUtils.getPropertyValue(bus, "messageBus.compressingPostProcessor.logger", Log.class));
    new DirectFieldAccessor(TestUtils.getPropertyValue(bus, "messageBus.compressingPostProcessor"))
            .setPropertyValue("logger", logger);
    when(logger.isTraceEnabled()).thenReturn(true);

    assertEquals(Deflater.BEST_SPEED,
            TestUtils.getPropertyValue(bus, "messageBus.compressingPostProcessor.level"));

    output.send(new GenericMessage<>("foo".getBytes()));
    output.send(new GenericMessage<>("bar".getBytes()));

    Object out = spyOn("batching.0").receive(false);
    assertThat(out, instanceOf(byte[].class));
    assertEquals("\u0000\u0000\u0000\u0003foo\u0000\u0000\u0000\u0003bar", new String((byte[]) out));

    ArgumentCaptor<Object> captor = ArgumentCaptor.forClass(Object.class);
    verify(logger).trace(captor.capture());
    assertThat(captor.getValue().toString(), containsString("Compressed 14 to "));

    QueueChannel input = new QueueChannel();
    input.setBeanName("batchingConsumer");
    bus.bindConsumer("batching.0", input, null);

    output.send(new GenericMessage<>("foo".getBytes()));
    output.send(new GenericMessage<>("bar".getBytes()));

    Message<byte[]> in = (Message<byte[]>) input.receive(10000);
    assertNotNull(in);
    assertEquals("foo", new String(in.getPayload()));
    in = (Message<byte[]>) input.receive(10000);
    assertNotNull(in);
    assertEquals("bar", new String(in.getPayload()));
    assertNull(in.getHeaders().get(AmqpHeaders.DELIVERY_MODE));

    bus.unbindProducers("batching.0");
    bus.unbindConsumers("batching.0");
}

From source file:org.springframework.xd.dirt.integration.bus.rabbit.RabbitMessageBusTests.java

@Override
public Spy spyOn(final String queue) {
    final RabbitTemplate template = new RabbitTemplate(this.rabbitAvailableRule.getResource());
    template.setAfterReceivePostProcessors(new DelegatingDecompressingPostProcessor());
    return new Spy() {

        @Override/*from w  w w  .j av  a 2  s. c  o m*/
        public Object receive(boolean expectNull) throws Exception {
            if (expectNull) {
                Thread.sleep(50);
                return template.receiveAndConvert("xdbus." + queue);
            }
            Object bar = null;
            int n = 0;
            while (n++ < 100 && bar == null) {
                bar = template.receiveAndConvert("xdbus." + queue);
                Thread.sleep(100);
            }
            assertTrue("Message did not arrive in RabbitMQ", n < 100);
            return bar;
        }

    };
}

From source file:org.springframework.xd.dirt.integration.rabbit.RabbitMessageBus.java

private RabbitTemplate determineRabbitTemplate(RabbitPropertiesAccessor properties) {
    RabbitTemplate rabbitTemplate = null;
    if (properties.isBatchingEnabled(this.defaultBatchingEnabled)) {
        BatchingStrategy batchingStrategy = new SimpleBatchingStrategy(
                properties.getBatchSize(this.defaultBatchSize),
                properties.geteBatchBufferLimit(this.defaultBatchBufferLimit),
                properties.getBatchTimeout(this.defaultBatchTimeout));
        rabbitTemplate = new BatchingRabbitTemplate(batchingStrategy, getApplicationContext()
                .getBean(IntegrationContextUtils.TASK_SCHEDULER_BEAN_NAME, TaskScheduler.class));
        rabbitTemplate.setConnectionFactory(this.connectionFactory);
    }//  ww  w.  ja  v a 2s .c  om
    if (properties.isCompress(this.defaultCompress)) {
        if (rabbitTemplate == null) {
            rabbitTemplate = new RabbitTemplate(this.connectionFactory);
        }
        rabbitTemplate.setBeforePublishPostProcessors(this.compressingPostProcessor);
        rabbitTemplate.afterPropertiesSet();
    }
    if (rabbitTemplate == null) {
        rabbitTemplate = this.rabbitTemplate;
    }
    return rabbitTemplate;
}

From source file:org.springframework.xd.dirt.integration.rabbit.RabbitMessageBusTests.java

@Test
public void testAutoBindDLQ() throws Exception {
    // pre-declare the queue with dead-lettering, users can also use a policy
    RabbitAdmin admin = new RabbitAdmin(this.rabbitAvailableRule.getResource());
    Map<String, Object> args = new HashMap<String, Object>();
    args.put("x-dead-letter-exchange", "xdbustest.DLX");
    Queue queue = new Queue("xdbustest.dlqtest", true, false, false, args);
    admin.declareQueue(queue);//  w ww  . j  a va2 s.co m

    MessageBus bus = getMessageBus();
    Properties properties = new Properties();
    properties.put("prefix", "xdbustest.");
    properties.put("autoBindDLQ", "true");
    properties.put("maxAttempts", "1"); // disable retry
    properties.put("requeue", "false");
    DirectChannel moduleInputChannel = new DirectChannel();
    moduleInputChannel.setBeanName("dlqTest");
    moduleInputChannel.subscribe(new MessageHandler() {

        @Override
        public void handleMessage(Message<?> message) throws MessagingException {
            throw new RuntimeException("foo");
        }

    });
    bus.bindConsumer("dlqtest", moduleInputChannel, properties);

    RabbitTemplate template = new RabbitTemplate(this.rabbitAvailableRule.getResource());
    template.convertAndSend("", "xdbustest.dlqtest", "foo");

    int n = 0;
    while (n++ < 100) {
        Object deadLetter = template.receiveAndConvert("xdbustest.dlqtest.dlq");
        if (deadLetter != null) {
            assertEquals("foo", deadLetter);
            break;
        }
        Thread.sleep(100);
    }
    assertTrue(n < 100);

    bus.unbindConsumer("dlqtest", moduleInputChannel);
    admin.deleteQueue("xdbustest.dlqtest.dlq");
    admin.deleteQueue("xdbustest.dlqtest");
    admin.deleteExchange("xdbustest.DLX");
}

From source file:org.springframework.xd.dirt.integration.rabbit.RabbitMessageBusTests.java

@Override
public Spy spyOn(final String queue) {
    final RabbitTemplate template = new RabbitTemplate(this.rabbitAvailableRule.getResource());
    template.setAfterReceivePostProcessor(new DelegatingDecompressingPostProcessor());
    return new Spy() {

        @Override// w  ww. j  a v  a2  s .  com
        public Object receive(boolean expectNull) throws Exception {
            if (expectNull) {
                Thread.sleep(50);
                return template.receiveAndConvert("xdbus." + queue);
            }
            Object bar = null;
            int n = 0;
            while (n++ < 100 && bar == null) {
                bar = template.receiveAndConvert("xdbus." + queue);
                Thread.sleep(100);
            }
            assertTrue("Message did not arrive in RabbitMQ", n < 100);
            return bar;
        }

    };
}