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 testSimpleBatchGZipped() throws Exception {
    BatchingStrategy batchingStrategy = new SimpleBatchingStrategy(2, Integer.MAX_VALUE, 30000);
    BatchingRabbitTemplate template = new BatchingRabbitTemplate(batchingStrategy, this.scheduler);
    template.setConnectionFactory(this.connectionFactory);
    GZipPostProcessor gZipPostProcessor = new GZipPostProcessor();
    assertEquals(Deflater.BEST_SPEED, getStreamLevel(gZipPostProcessor));
    template.setBeforePublishPostProcessors(gZipPostProcessor);
    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.jav a2s . c o m
    assertEquals("gzip", message.getMessageProperties().getContentEncoding());
    GUnzipPostProcessor unzipper = new GUnzipPostProcessor();
    message = unzipper.postProcessMessage(message);
    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 testSimpleBatchGZippedConfiguredUnzipper() throws Exception {
    BatchingStrategy batchingStrategy = new SimpleBatchingStrategy(2, Integer.MAX_VALUE, 30000);
    BatchingRabbitTemplate template = new BatchingRabbitTemplate(batchingStrategy, this.scheduler);
    template.setConnectionFactory(this.connectionFactory);
    GZipPostProcessor gZipPostProcessor = new GZipPostProcessor();
    gZipPostProcessor.setLevel(Deflater.BEST_COMPRESSION);
    assertEquals(Deflater.BEST_COMPRESSION, getStreamLevel(gZipPostProcessor));
    template.setBeforePublishPostProcessors(gZipPostProcessor);
    template.setAfterReceivePostProcessors(new GUnzipPostProcessor());
    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 v  a2  s.c o m*/
    assertNull(message.getMessageProperties().getContentEncoding());
    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 testSimpleBatchGZippedWithEncoding() throws Exception {
    BatchingStrategy batchingStrategy = new SimpleBatchingStrategy(2, Integer.MAX_VALUE, 30000);
    BatchingRabbitTemplate template = new BatchingRabbitTemplate(batchingStrategy, this.scheduler);
    template.setConnectionFactory(this.connectionFactory);
    template.setBeforePublishPostProcessors(new GZipPostProcessor());
    MessageProperties props = new MessageProperties();
    props.setContentEncoding("foo");
    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 a2 s  .  c  om
    assertEquals("gzip:foo", message.getMessageProperties().getContentEncoding());
    GUnzipPostProcessor unzipper = new GUnzipPostProcessor();
    message = unzipper.postProcessMessage(message);
    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 testSimpleBatchGZippedWithEncodingInflated() throws Exception {
    BatchingStrategy batchingStrategy = new SimpleBatchingStrategy(2, Integer.MAX_VALUE, 30000);
    BatchingRabbitTemplate template = new BatchingRabbitTemplate(batchingStrategy, this.scheduler);
    template.setConnectionFactory(this.connectionFactory);
    template.setBeforePublishPostProcessors(new GZipPostProcessor());
    template.setAfterReceivePostProcessors(new DelegatingDecompressingPostProcessor());
    MessageProperties props = new MessageProperties();
    props.setContentEncoding("foo");
    Message message = new Message("foo".getBytes(), props);
    template.send("", ROUTE, message);
    message = new Message("bar".getBytes(), props);
    template.send("", ROUTE, message);
    Thread.sleep(100);//from  w  ww.j  ava2 s  . c  o m
    byte[] out = (byte[]) template.receiveAndConvert(ROUTE);
    assertNotNull(out);
    assertEquals("\u0000\u0000\u0000\u0003foo\u0000\u0000\u0000\u0003bar", new String(out));
}

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

@Test
public void testSimpleBatchZippedBestCompression() throws Exception {
    BatchingStrategy batchingStrategy = new SimpleBatchingStrategy(2, Integer.MAX_VALUE, 30000);
    BatchingRabbitTemplate template = new BatchingRabbitTemplate(batchingStrategy, this.scheduler);
    template.setConnectionFactory(this.connectionFactory);
    ZipPostProcessor zipPostProcessor = new ZipPostProcessor();
    zipPostProcessor.setLevel(Deflater.BEST_COMPRESSION);
    assertEquals(Deflater.BEST_COMPRESSION, getStreamLevel(zipPostProcessor));
    template.setBeforePublishPostProcessors(zipPostProcessor);
    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 .j  av a  2s .c  om
    assertEquals("zip", message.getMessageProperties().getContentEncoding());
    UnzipPostProcessor unzipper = new UnzipPostProcessor();
    message = unzipper.postProcessMessage(message);
    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 testSimpleBatchZippedWithEncoding() throws Exception {
    BatchingStrategy batchingStrategy = new SimpleBatchingStrategy(2, Integer.MAX_VALUE, 30000);
    BatchingRabbitTemplate template = new BatchingRabbitTemplate(batchingStrategy, this.scheduler);
    template.setConnectionFactory(this.connectionFactory);
    ZipPostProcessor zipPostProcessor = new ZipPostProcessor();
    assertEquals(Deflater.BEST_SPEED, getStreamLevel(zipPostProcessor));
    template.setBeforePublishPostProcessors(zipPostProcessor);
    MessageProperties props = new MessageProperties();
    props.setContentEncoding("foo");
    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  va 2  s. c  o m*/
    assertEquals("zip:foo", message.getMessageProperties().getContentEncoding());
    UnzipPostProcessor unzipper = new UnzipPostProcessor();
    message = unzipper.postProcessMessage(message);
    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 testCompressionWithContainer() 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  ww. j  av  a  2s.  com*/
    container.setMessageListener((MessageListener) message -> {
        received.add(message);
        latch.countDown();
    });
    container.setReceiveTimeout(100);
    container.setAfterReceivePostProcessors(new DelegatingDecompressingPostProcessor());
    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);
        template.setBeforePublishPostProcessors(new GZipPostProcessor());
        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.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. j  a v  a2s  .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;
}