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

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

Introduction

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

Prototype

public BatchingRabbitTemplate(BatchingStrategy batchingStrategy, TaskScheduler scheduler) 

Source Link

Document

Create an instance with the supplied parameters.

Usage

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

@Test
public void testSimpleBatch() throws Exception {
    BatchingStrategy batchingStrategy = new SimpleBatchingStrategy(2, Integer.MAX_VALUE, 30000);
    BatchingRabbitTemplate template = new BatchingRabbitTemplate(batchingStrategy, this.scheduler);
    template.setConnectionFactory(this.connectionFactory);
    MessageProperties props = new MessageProperties();
    Message message = new Message("foo".getBytes(), props);
    template.send("", ROUTE, message);
    message = new Message("bar".getBytes(), props);
    template.send("", ROUTE, message);
    message = receive(template);/*from  ww  w .  j  a va  2s  .  co  m*/
    assertEquals("\u0000\u0000\u0000\u0003foo\u0000\u0000\u0000\u0003bar", new String(message.getBody()));
}

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

@Test
public void testSimpleBatchTimeout() throws Exception {
    BatchingStrategy batchingStrategy = new SimpleBatchingStrategy(2, Integer.MAX_VALUE, 50);
    BatchingRabbitTemplate template = new BatchingRabbitTemplate(batchingStrategy, this.scheduler);
    template.setConnectionFactory(this.connectionFactory);
    MessageProperties props = new MessageProperties();
    Message message = new Message("foo".getBytes(), props);
    template.send("", ROUTE, message);
    message = receive(template);/*from  www.j  av a  2  s. co m*/
    assertEquals("foo", new String(message.getBody()));
}

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

@Test
public void testSimpleBatchTimeoutMultiple() throws Exception {
    BatchingStrategy batchingStrategy = new SimpleBatchingStrategy(2, Integer.MAX_VALUE, 50);
    BatchingRabbitTemplate template = new BatchingRabbitTemplate(batchingStrategy, this.scheduler);
    template.setConnectionFactory(this.connectionFactory);
    MessageProperties props = new MessageProperties();
    Message message = new Message("foo".getBytes(), props);
    template.send("", ROUTE, message);
    template.send("", ROUTE, message);
    message = receive(template);/*  w  w  w  .  j  av  a  2s.  c  o m*/
    assertEquals("\u0000\u0000\u0000\u0003foo\u0000\u0000\u0000\u0003foo", new String(message.getBody()));
}

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

@Test
public void testSimpleBatchBufferLimit() throws Exception {
    BatchingStrategy batchingStrategy = new SimpleBatchingStrategy(2, 8, 50);
    BatchingRabbitTemplate template = new BatchingRabbitTemplate(batchingStrategy, this.scheduler);
    template.setConnectionFactory(this.connectionFactory);
    MessageProperties props = new MessageProperties();
    Message message = new Message("foo".getBytes(), props);
    template.send("", ROUTE, message);
    message = new Message("bar".getBytes(), props);
    template.send("", ROUTE, message);
    message = receive(template);/*from  ww  w  .j  a v a2 s .c o  m*/
    assertEquals("foo", new String(message.getBody()));
    message = receive(template);
    assertEquals("bar", new String(message.getBody()));
}

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

@Test
public void testSimpleBatchBufferLimitMultiple() throws Exception {
    BatchingStrategy batchingStrategy = new SimpleBatchingStrategy(2, 15, 30000);
    BatchingRabbitTemplate template = new BatchingRabbitTemplate(batchingStrategy, this.scheduler);
    template.setConnectionFactory(this.connectionFactory);
    MessageProperties props = new MessageProperties();
    Message message = new Message("foo".getBytes(), props);
    template.send("", ROUTE, message);
    template.send("", ROUTE, message);
    message = new Message("bar".getBytes(), props);
    template.send("", ROUTE, message);
    template.send("", ROUTE, message);
    message = receive(template);//w  w  w.  j a  v a 2s .  c om
    assertEquals("\u0000\u0000\u0000\u0003foo\u0000\u0000\u0000\u0003foo", new String(message.getBody()));
    message = receive(template);
    assertEquals("\u0000\u0000\u0000\u0003bar\u0000\u0000\u0000\u0003bar", new String(message.getBody()));
}

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

@Test
public void testSimpleBatchBiggerThanBufferLimit() throws Exception {
    BatchingStrategy batchingStrategy = new SimpleBatchingStrategy(2, 2, 30000);
    BatchingRabbitTemplate template = new BatchingRabbitTemplate(batchingStrategy, this.scheduler);
    template.setConnectionFactory(this.connectionFactory);
    MessageProperties props = new MessageProperties();
    Message message = new Message("foo".getBytes(), props);
    template.send("", ROUTE, message);
    message = new Message("bar".getBytes(), props);
    template.send("", ROUTE, message);
    message = receive(template);//from   ww  w. jav  a 2 s  . co  m
    assertEquals("foo", new String(message.getBody()));
    message = receive(template);
    assertEquals("bar", new String(message.getBody()));
}

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

@Test
// existing buffered; new message bigger than bufferLimit; released immediately
public void testSimpleBatchBiggerThanBufferLimitMultiple() throws Exception {
    BatchingStrategy batchingStrategy = new SimpleBatchingStrategy(2, 6, 30000);
    BatchingRabbitTemplate template = new BatchingRabbitTemplate(batchingStrategy, this.scheduler);
    template.setConnectionFactory(this.connectionFactory);
    MessageProperties props = new MessageProperties();
    Message message = new Message("f".getBytes(), props);
    template.send("", ROUTE, message);
    message = new Message("bar".getBytes(), props);
    template.send("", ROUTE, message);
    message = receive(template);//from  www . j  a  va2s  .  c  o  m
    assertEquals("f", new String(message.getBody()));
    message = receive(template);
    assertEquals("bar", new String(message.getBody()));
}

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

@Test
public void testSimpleBatchTwoEqualBufferLimit() throws Exception {
    BatchingStrategy batchingStrategy = new SimpleBatchingStrategy(10, 14, 30000);
    BatchingRabbitTemplate template = new BatchingRabbitTemplate(batchingStrategy, this.scheduler);
    template.setConnectionFactory(this.connectionFactory);
    MessageProperties props = new MessageProperties();
    Message message = new Message("foo".getBytes(), props);
    template.send("", ROUTE, message);
    message = new Message("bar".getBytes(), props);
    template.send("", ROUTE, message);
    message = receive(template);/*from   w w  w  .  j a  v  a  2s . co m*/
    assertEquals("\u0000\u0000\u0000\u0003foo\u0000\u0000\u0000\u0003bar", new String(message.getBody()));
}

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

@Test
public void testDebatchByContainer() throws Exception {
    final List<Message> received = new ArrayList<Message>();
    final CountDownLatch latch = new CountDownLatch(2);
    SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(this.connectionFactory);
    container.setQueueNames(ROUTE);/*from w  w  w  .j  a  va 2 s. c  o  m*/
    container.setMessageListener((MessageListener) message -> {
        received.add(message);
        latch.countDown();
    });
    container.setReceiveTimeout(100);
    container.afterPropertiesSet();
    container.start();
    try {
        BatchingStrategy batchingStrategy = new SimpleBatchingStrategy(2, Integer.MAX_VALUE, 30000);
        BatchingRabbitTemplate template = new BatchingRabbitTemplate(batchingStrategy, this.scheduler);
        template.setConnectionFactory(this.connectionFactory);
        MessageProperties props = new MessageProperties();
        Message message = new Message("foo".getBytes(), props);
        template.send("", ROUTE, message);
        message = new Message("bar".getBytes(), props);
        template.send("", ROUTE, message);
        assertTrue(latch.await(10, TimeUnit.SECONDS));
        assertEquals(2, received.size());
        assertEquals("foo", new String(received.get(0).getBody()));
        assertEquals(3, received.get(0).getMessageProperties().getContentLength());
        assertEquals("bar", new String(received.get(1).getBody()));
        assertEquals(3, received.get(0).getMessageProperties().getContentLength());
    } finally {
        container.stop();
    }
}

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

@Test
public void testDebatchByContainerPerformance() throws Exception {
    final List<Message> received = new ArrayList<Message>();
    int count = 100000;
    final CountDownLatch latch = new CountDownLatch(count);
    SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(this.connectionFactory);
    container.setQueueNames(ROUTE);/* ww  w .  j av  a  2  s  .c  o  m*/
    container.setMessageListener((MessageListener) message -> {
        received.add(message);
        latch.countDown();
    });
    container.setReceiveTimeout(100);
    container.setPrefetchCount(1000);
    container.setTxSize(1000);
    container.afterPropertiesSet();
    container.start();
    try {
        BatchingStrategy batchingStrategy = new SimpleBatchingStrategy(1000, Integer.MAX_VALUE, 30000);
        BatchingRabbitTemplate template = new BatchingRabbitTemplate(batchingStrategy, this.scheduler);
        //         RabbitTemplate template = new RabbitTemplate();
        template.setConnectionFactory(this.connectionFactory);
        MessageProperties props = new MessageProperties();
        props.setDeliveryMode(MessageDeliveryMode.NON_PERSISTENT);
        Message message = new Message(new byte[256], props);
        StopWatch watch = new StopWatch();
        watch.start();
        for (int i = 0; i < count; i++) {
            template.send("", ROUTE, message);
        }
        assertTrue(latch.await(60, TimeUnit.SECONDS));
        watch.stop();
        // System .out .println(watch.getTotalTimeMillis());
        assertEquals(count, received.size());
    } finally {
        container.stop();
    }
}