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

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

Introduction

In this page you can find the example usage for org.springframework.amqp.rabbit.core BatchingRabbitTemplate 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: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 w w w  . ja  va2  s . c o 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);//w w w .  jav  a  2  s.com
    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);/*from  w  ww. j av a  2s .c  om*/
    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);//  w ww  .java 2 s . com
    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. ja  va 2  s .co  m
    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);/*  ww  w .  ja v a2s . com*/
    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   w  w w .  ja  v a 2s  .  co  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 ww.  j a  v  a2s .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);//w w w.j  a v  a 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);/*from w w  w .  j  a  v a 2  s . c  om*/
    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();
    }
}