Example usage for org.springframework.amqp.core MessageDeliveryMode PERSISTENT

List of usage examples for org.springframework.amqp.core MessageDeliveryMode PERSISTENT

Introduction

In this page you can find the example usage for org.springframework.amqp.core MessageDeliveryMode PERSISTENT.

Prototype

MessageDeliveryMode PERSISTENT

To view the source code for org.springframework.amqp.core MessageDeliveryMode PERSISTENT.

Click Source Link

Document

Persistent.

Usage

From source file:com.bfair.pricing.gateway.RabbitPriceDataGateway.java

private MessageProperties getMessageProperties() {
    if (properties == null) {
        properties = new MessageProperties();
        properties.setDeliveryMode(MessageDeliveryMode.PERSISTENT);
        properties.setContentType(MessageProperties.CONTENT_TYPE_JSON);
    }//www  .  j  a v a  2 s . c o  m
    return properties;
}

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

@Test
public void testProducerProperties() throws Exception {
    RabbitTestBinder binder = getBinder();
    Binding<MessageChannel> producerBinding = binder.bindProducer("props.0",
            createBindableChannel("input", new BindingProperties()), createProducerProperties());
    Lifecycle endpoint = extractEndpoint(producerBinding);
    MessageDeliveryMode mode = TestUtils.getPropertyValue(endpoint, "defaultDeliveryMode",
            MessageDeliveryMode.class);
    assertThat(mode).isEqualTo(MessageDeliveryMode.PERSISTENT);
    List<?> requestHeaders = TestUtils.getPropertyValue(endpoint, "headerMapper.requestHeaderMatcher.matchers",
            List.class);
    assertThat(requestHeaders).hasSize(2);
    producerBinding.unbind();/*from   www.j ava2 s  . co  m*/
    assertThat(endpoint.isRunning()).isFalse();
    assertThat(TestUtils.getPropertyValue(endpoint, "amqpTemplate.transactional", Boolean.class)).isFalse();

    ExtendedProducerProperties<RabbitProducerProperties> producerProperties = createProducerProperties();
    producerProperties.getExtension().setPrefix("foo.");
    producerProperties.getExtension().setDeliveryMode(MessageDeliveryMode.NON_PERSISTENT);
    producerProperties.getExtension().setHeaderPatterns(new String[] { "foo" });
    producerProperties.setPartitionKeyExpression(spelExpressionParser.parseExpression("'foo'"));
    producerProperties.setPartitionKeyExtractorClass(TestPartitionKeyExtractorClass.class);
    producerProperties.setPartitionSelectorExpression(spelExpressionParser.parseExpression("0"));
    producerProperties.setPartitionSelectorClass(TestPartitionSelectorClass.class);
    producerProperties.setPartitionCount(1);
    producerProperties.getExtension().setTransacted(true);
    producerProperties.getExtension().setDelayExpression("42");
    producerProperties.setRequiredGroups("prodPropsRequired");

    BindingProperties producerBindingProperties = createProducerBindingProperties(producerProperties);
    DirectChannel channel = createBindableChannel("output", producerBindingProperties);
    producerBinding = binder.bindProducer("props.0", channel, producerProperties);
    endpoint = extractEndpoint(producerBinding);
    assertThat(getEndpointRouting(endpoint))
            .isEqualTo("'props.0-' + headers['" + BinderHeaders.PARTITION_HEADER + "']");
    assertThat(
            TestUtils.getPropertyValue(endpoint, "delayExpression", SpelExpression.class).getExpressionString())
                    .isEqualTo("42");
    mode = TestUtils.getPropertyValue(endpoint, "defaultDeliveryMode", MessageDeliveryMode.class);
    assertThat(mode).isEqualTo(MessageDeliveryMode.NON_PERSISTENT);
    assertThat(TestUtils.getPropertyValue(endpoint, "amqpTemplate.transactional", Boolean.class)).isTrue();
    verifyFooRequestProducer(endpoint);
    channel.send(new GenericMessage<>("foo"));
    org.springframework.amqp.core.Message received = new RabbitTemplate(this.rabbitAvailableRule.getResource())
            .receive("foo.props.0.prodPropsRequired-0", 10_000);
    assertThat(received).isNotNull();
    assertThat(received.getMessageProperties().getReceivedDelay()).isEqualTo(42);

    producerBinding.unbind();
    assertThat(endpoint.isRunning()).isFalse();
}

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

@Test
public void testAutoBindDLQPartitionedProducerFirst() throws Exception {
    RabbitTestBinder binder = getBinder();
    ExtendedProducerProperties<RabbitProducerProperties> properties = createProducerProperties();

    properties.getExtension().setPrefix("bindertest.");
    properties.getExtension().setAutoBindDlq(true);
    properties.setRequiredGroups("dlqPartGrp");
    properties.setPartitionKeyExtractorClass(PartitionTestSupport.class);
    properties.setPartitionSelectorClass(PartitionTestSupport.class);
    properties.setPartitionCount(2);//from w ww. j  av  a 2 s.c  o m
    DirectChannel output = createBindableChannel("output", createProducerBindingProperties(properties));
    output.setBeanName("test.output");
    Binding<MessageChannel> outputBinding = binder.bindProducer("partDLQ.1", output, properties);

    ExtendedConsumerProperties<RabbitConsumerProperties> consumerProperties = createConsumerProperties();
    consumerProperties.getExtension().setPrefix("bindertest.");
    consumerProperties.getExtension().setAutoBindDlq(true);
    consumerProperties.setMaxAttempts(1); // disable retry
    consumerProperties.setPartitioned(true);
    consumerProperties.setInstanceIndex(0);
    DirectChannel input0 = createBindableChannel("input", createConsumerBindingProperties(consumerProperties));
    input0.setBeanName("test.input0DLQ");
    Binding<MessageChannel> input0Binding = binder.bindConsumer("partDLQ.1", "dlqPartGrp", input0,
            consumerProperties);
    Binding<MessageChannel> defaultConsumerBinding1 = binder.bindConsumer("partDLQ.1", "defaultConsumer",
            new QueueChannel(), consumerProperties);
    consumerProperties.setInstanceIndex(1);
    DirectChannel input1 = createBindableChannel("input1", createConsumerBindingProperties(consumerProperties));
    input1.setBeanName("test.input1DLQ");
    Binding<MessageChannel> input1Binding = binder.bindConsumer("partDLQ.1", "dlqPartGrp", input1,
            consumerProperties);
    Binding<MessageChannel> defaultConsumerBinding2 = binder.bindConsumer("partDLQ.1", "defaultConsumer",
            new QueueChannel(), consumerProperties);

    final CountDownLatch latch0 = new CountDownLatch(1);
    input0.subscribe(new MessageHandler() {

        @Override
        public void handleMessage(Message<?> message) throws MessagingException {
            if (latch0.getCount() <= 0) {
                throw new RuntimeException("dlq");
            }
            latch0.countDown();
        }

    });

    final CountDownLatch latch1 = new CountDownLatch(1);
    input1.subscribe(new MessageHandler() {

        @Override
        public void handleMessage(Message<?> message) throws MessagingException {
            if (latch1.getCount() <= 0) {
                throw new RuntimeException("dlq");
            }
            latch1.countDown();
        }

    });

    output.send(new GenericMessage<Integer>(1));
    assertThat(latch1.await(10, TimeUnit.SECONDS)).isTrue();

    output.send(new GenericMessage<Integer>(0));
    assertThat(latch0.await(10, TimeUnit.SECONDS)).isTrue();

    output.send(new GenericMessage<Integer>(1));

    RabbitTemplate template = new RabbitTemplate(this.rabbitAvailableRule.getResource());
    template.setReceiveTimeout(10000);

    String streamDLQName = "bindertest.partDLQ.1.dlqPartGrp.dlq";

    org.springframework.amqp.core.Message received = template.receive(streamDLQName);
    assertThat(received).isNotNull();
    assertThat(received.getMessageProperties().getReceivedRoutingKey())
            .isEqualTo("bindertest.partDLQ.1.dlqPartGrp-1");
    assertThat(received.getMessageProperties().getHeaders()).doesNotContainKey(BinderHeaders.PARTITION_HEADER);
    assertThat(received.getMessageProperties().getReceivedDeliveryMode())
            .isEqualTo(MessageDeliveryMode.PERSISTENT);

    output.send(new GenericMessage<Integer>(0));
    received = template.receive(streamDLQName);
    assertThat(received).isNotNull();
    assertThat(received.getMessageProperties().getReceivedRoutingKey())
            .isEqualTo("bindertest.partDLQ.1.dlqPartGrp-0");
    assertThat(received.getMessageProperties().getHeaders()).doesNotContainKey(BinderHeaders.PARTITION_HEADER);

    input0Binding.unbind();
    input1Binding.unbind();
    defaultConsumerBinding1.unbind();
    defaultConsumerBinding2.unbind();
    outputBinding.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  .  j a v a2  s. c  o m*/

    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.integration.amqp.config.AmqpOutboundChannelAdapterParserTests.java

@SuppressWarnings("rawtypes")
@Test/*from   w  w  w .j  a  v  a2 s  . c o  m*/
public void amqpOutboundChannelAdapterWithinChain() {
    Object eventDrivenConsumer = context.getBean("chainWithRabbitOutbound");

    List chainHandlers = TestUtils.getPropertyValue(eventDrivenConsumer, "handler.handlers", List.class);

    AmqpOutboundEndpoint endpoint = (AmqpOutboundEndpoint) chainHandlers.get(0);
    assertNull(TestUtils.getPropertyValue(endpoint, "defaultDeliveryMode"));

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

    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("hello", new String(amqpMessage.getBody()));
        assertEquals(MessageDeliveryMode.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("amqpOutboundChannelAdapterWithinChain",
            MessageChannel.class);
    Message<?> message = MessageBuilder.withPayload("hello").build();
    requestChannel.send(message);
    Mockito.verify(amqpTemplate, Mockito.times(1)).send(Mockito.any(String.class), isNull(),
            Mockito.any(org.springframework.amqp.core.Message.class), isNull());
}

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

@Test
public void testProducerProperties() throws Exception {
    MessageBus bus = getMessageBus();/* w  ww .  j  av  a2 s.  c om*/
    bus.bindProducer("props.0", new DirectChannel(), null);
    @SuppressWarnings("unchecked")
    List<Binding> bindings = TestUtils.getPropertyValue(bus, "messageBus.bindings", List.class);
    assertEquals(1, bindings.size());
    AbstractEndpoint endpoint = bindings.get(0).getEndpoint();
    assertEquals("xdbus.props.0", TestUtils.getPropertyValue(endpoint, "handler.delegate.routingKey"));
    MessageDeliveryMode mode = TestUtils.getPropertyValue(endpoint, "handler.delegate.defaultDeliveryMode",
            MessageDeliveryMode.class);
    assertEquals(MessageDeliveryMode.PERSISTENT, mode);
    List<?> requestHeaders = TestUtils.getPropertyValue(endpoint,
            "handler.delegate.headerMapper.requestHeaderMatcher.strategies", List.class);
    assertEquals(2, requestHeaders.size());
    bus.unbindProducers("props.0");
    assertEquals(0, bindings.size());

    Properties properties = new Properties();
    properties.put("prefix", "foo.");
    properties.put("deliveryMode", "NON_PERSISTENT");
    properties.put("requestHeaderPatterns", "foo");
    properties.put("partitionKeyExpression", "'foo'");
    properties.put("partitionKeyExtractorClass", "foo");
    properties.put("partitionSelectorExpression", "0");
    properties.put("partitionSelectorClass", "foo");
    properties.put(BusProperties.NEXT_MODULE_COUNT, "1");

    bus.bindProducer("props.0", new DirectChannel(), properties);
    assertEquals(1, bindings.size());
    endpoint = bindings.get(0).getEndpoint();
    assertEquals("'foo.props.0-' + headers['partition']",
            TestUtils.getPropertyValue(endpoint, "handler.delegate.routingKeyExpression", SpelExpression.class)
                    .getExpressionString());
    mode = TestUtils.getPropertyValue(endpoint, "handler.delegate.defaultDeliveryMode",
            MessageDeliveryMode.class);
    assertEquals(MessageDeliveryMode.NON_PERSISTENT, mode);
    verifyFooRequestProducer(endpoint);

    try {
        bus.bindPubSubProducer("dummy", new DirectChannel(), properties);
        fail("Expected exception");
    } catch (IllegalArgumentException e) {
        assertThat(e.getMessage(),
                allOf(containsString("RabbitMessageBus does not support producer properties: "),
                        containsString("partitionSelectorExpression"),
                        containsString("partitionKeyExtractorClass"), containsString("partitionKeyExpression"),
                        containsString("partitionSelectorClass")));
        assertThat(e.getMessage(), containsString("for dummy."));
    }
    try {
        bus.bindProducer("queue:dummy", new DirectChannel(), properties);
        fail("Expected exception");
    } catch (IllegalArgumentException e) {
        assertThat(e.getMessage(),
                allOf(containsString("RabbitMessageBus does not support producer properties: "),
                        containsString("partitionSelectorExpression"),
                        containsString("partitionKeyExtractorClass"), containsString("partitionKeyExpression"),
                        containsString("partitionSelectorClass")));
        assertThat(e.getMessage(), containsString("for queue:dummy."));
    }

    bus.unbindProducers("props.0");
    assertEquals(0, bindings.size());
}

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

@Test
public void testProducerProperties() throws Exception {
    MessageBus bus = getMessageBus();/*from w  w w .  j  av  a 2  s.c o m*/
    bus.bindProducer("props.0", new DirectChannel(), null);
    @SuppressWarnings("unchecked")
    List<Binding> bindings = TestUtils.getPropertyValue(bus, "messageBus.bindings", List.class);
    assertEquals(1, bindings.size());
    AbstractEndpoint endpoint = bindings.get(0).getEndpoint();
    assertEquals("xdbus.props.0", TestUtils.getPropertyValue(endpoint, "handler.delegate.routingKey"));
    MessageDeliveryMode mode = TestUtils.getPropertyValue(endpoint, "handler.delegate.defaultDeliveryMode",
            MessageDeliveryMode.class);
    assertEquals(MessageDeliveryMode.PERSISTENT, mode);
    List<?> requestHeaders = TestUtils.getPropertyValue(endpoint,
            "handler.delegate.headerMapper.requestHeaderMatcher.strategies", List.class);
    assertEquals(2, requestHeaders.size());
    bus.unbindProducers("props.0");
    assertEquals(0, bindings.size());

    Properties properties = new Properties();
    properties.put("prefix", "foo.");
    properties.put("deliveryMode", "NON_PERSISTENT");
    properties.put("requestHeaderPatterns", "foo");
    properties.put("partitionKeyExpression", "'foo'");
    properties.put("partitionKeyExtractorClass", "foo");
    properties.put("partitionSelectorExpression", "0");
    properties.put("partitionSelectorClass", "foo");
    properties.put("partitionCount", "1");

    bus.bindProducer("props.0", new DirectChannel(), properties);
    assertEquals(1, bindings.size());
    endpoint = bindings.get(0).getEndpoint();
    assertEquals("'foo.props.0-' + headers['partition']",
            TestUtils.getPropertyValue(endpoint, "handler.delegate.routingKeyExpression", SpelExpression.class)
                    .getExpressionString());
    mode = TestUtils.getPropertyValue(endpoint, "handler.delegate.defaultDeliveryMode",
            MessageDeliveryMode.class);
    assertEquals(MessageDeliveryMode.NON_PERSISTENT, mode);
    verifyFooRequestProducer(endpoint);

    try {
        bus.bindPubSubProducer("dummy", new DirectChannel(), properties);
        fail("Expected exception");
    } catch (IllegalArgumentException e) {
        assertThat(e.getMessage(),
                allOf(containsString("RabbitMessageBus does not support producer properties: "),
                        containsString("partitionCount"), containsString("partitionSelectorExpression"),
                        containsString("partitionKeyExtractorClass"), containsString("partitionKeyExpression"),
                        containsString("partitionSelectorClass")));
        assertThat(e.getMessage(), containsString("for dummy."));
    }
    try {
        bus.bindProducer("queue:dummy", new DirectChannel(), properties);
        fail("Expected exception");
    } catch (IllegalArgumentException e) {
        assertThat(e.getMessage(),
                allOf(containsString("RabbitMessageBus does not support producer properties: "),
                        containsString("partitionCount"), containsString("partitionSelectorExpression"),
                        containsString("partitionKeyExtractorClass"), containsString("partitionKeyExpression"),
                        containsString("partitionSelectorClass")));
        assertThat(e.getMessage(), containsString("for queue:dummy."));
    }

    bus.unbindProducers("props.0");
    assertEquals(0, bindings.size());
}