Example usage for org.springframework.amqp.support AmqpHeaders DELIVERY_MODE

List of usage examples for org.springframework.amqp.support AmqpHeaders DELIVERY_MODE

Introduction

In this page you can find the example usage for org.springframework.amqp.support AmqpHeaders DELIVERY_MODE.

Prototype

String DELIVERY_MODE

To view the source code for org.springframework.amqp.support AmqpHeaders DELIVERY_MODE.

Click Source Link

Usage

From source file:org.springframework.cloud.stream.binder.rabbit.RabbitBinderTests.java

@SuppressWarnings("unchecked")
@Test/*from  w  ww.j  a v a  2  s .  c  om*/
public void testBatchingAndCompression() throws Exception {
    RabbitTestBinder binder = getBinder();
    ExtendedProducerProperties<RabbitProducerProperties> producerProperties = createProducerProperties();
    producerProperties.getExtension().setDeliveryMode(MessageDeliveryMode.NON_PERSISTENT);
    producerProperties.getExtension().setBatchingEnabled(true);
    producerProperties.getExtension().setBatchSize(2);
    producerProperties.getExtension().setBatchBufferLimit(100000);
    producerProperties.getExtension().setBatchTimeout(30000);
    producerProperties.getExtension().setCompress(true);
    producerProperties.setRequiredGroups("default");

    DirectChannel output = createBindableChannel("input", createProducerBindingProperties(producerProperties));
    output.setBeanName("batchingProducer");
    Binding<MessageChannel> producerBinding = binder.bindProducer("batching.0", output, producerProperties);

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

    assertThat(TestUtils.getPropertyValue(binder, "binder.compressingPostProcessor.level"))
            .isEqualTo(Deflater.BEST_SPEED);

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

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

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

    QueueChannel input = new QueueChannel();
    input.setBeanName("batchingConsumer");
    Binding<MessageChannel> consumerBinding = binder.bindConsumer("batching.0", "test", input,
            createConsumerProperties());

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

    Message<byte[]> in = (Message<byte[]>) input.receive(10000);
    assertThat(in).isNotNull();
    assertThat(new String(in.getPayload())).isEqualTo("foo");
    in = (Message<byte[]>) input.receive(10000);
    assertThat(in).isNotNull();
    assertThat(new String(in.getPayload())).isEqualTo("bar");
    assertThat(in.getHeaders().get(AmqpHeaders.DELIVERY_MODE)).isNull();

    producerBinding.unbind();
    consumerBinding.unbind();
}

From source file:org.springframework.integration.amqp.config.AmqpOutboundChannelAdapterParserTests.java

@Test
public void withHeaderMapperCustomHeaders() {
    Object eventDrivenConsumer = context.getBean("withHeaderMapperCustomHeaders");

    AmqpOutboundEndpoint endpoint = TestUtils.getPropertyValue(eventDrivenConsumer, "handler",
            AmqpOutboundEndpoint.class);
    assertNotNull(TestUtils.getPropertyValue(endpoint, "defaultDeliveryMode"));
    assertFalse(TestUtils.getPropertyValue(endpoint, "lazyConnect", Boolean.class));
    assertEquals("42", TestUtils
            .getPropertyValue(endpoint, "delayExpression", org.springframework.expression.Expression.class)
            .getExpressionString());/*from w ww .ja v a  2s  . c om*/

    Field amqpTemplateField = ReflectionUtils.findField(AmqpOutboundEndpoint.class, "amqpTemplate");
    amqpTemplateField.setAccessible(true);
    RabbitTemplate amqpTemplate = TestUtils.getPropertyValue(endpoint, "amqpTemplate", RabbitTemplate.class);
    amqpTemplate = Mockito.spy(amqpTemplate);
    final AtomicBoolean shouldBePersistent = new AtomicBoolean();

    Mockito.doAnswer(invocation -> {
        Object[] args = invocation.getArguments();
        org.springframework.amqp.core.Message amqpMessage = (org.springframework.amqp.core.Message) args[2];
        MessageProperties properties = amqpMessage.getMessageProperties();
        assertEquals("foo", properties.getHeaders().get("foo"));
        assertEquals("foobar", properties.getHeaders().get("foobar"));
        assertNull(properties.getHeaders().get("bar"));
        assertEquals(
                shouldBePersistent.get() ? MessageDeliveryMode.PERSISTENT : MessageDeliveryMode.NON_PERSISTENT,
                properties.getDeliveryMode());
        return null;
    }).when(amqpTemplate).send(Mockito.any(String.class), Mockito.any(String.class),
            Mockito.any(org.springframework.amqp.core.Message.class), Mockito.any(CorrelationData.class));
    ReflectionUtils.setField(amqpTemplateField, endpoint, amqpTemplate);

    MessageChannel requestChannel = context.getBean("requestChannel", MessageChannel.class);
    Message<?> message = MessageBuilder.withPayload("hello").setHeader("foo", "foo").setHeader("bar", "bar")
            .setHeader("foobar", "foobar").build();
    requestChannel.send(message);
    Mockito.verify(amqpTemplate, Mockito.times(1)).send(anyString(), isNull(),
            Mockito.any(org.springframework.amqp.core.Message.class), isNull());

    shouldBePersistent.set(true);
    message = MessageBuilder.withPayload("hello").setHeader("foo", "foo").setHeader("bar", "bar")
            .setHeader("foobar", "foobar").setHeader(AmqpHeaders.DELIVERY_MODE, MessageDeliveryMode.PERSISTENT)
            .build();
    requestChannel.send(message);
}

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

@SuppressWarnings("unchecked")
@Test/*w w  w . j a v a  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");
}