Search in sources :

Example 1 with MessageConverter

use of org.springframework.amqp.support.converter.MessageConverter in project spring-integration by spring-projects.

the class MappingUtilsTests method testMapping.

@Test
public void testMapping() {
    Message<?> requestMessage = MessageBuilder.withPayload("foo").setHeader(AmqpHeaders.CONTENT_TYPE, "my/ct").build();
    MessageConverter converter = new SimpleMessageConverter();
    AmqpHeaderMapper headerMapper = DefaultAmqpHeaderMapper.outboundMapper();
    MessageDeliveryMode defaultDeliveryMode = MessageDeliveryMode.NON_PERSISTENT;
    boolean headersMappedLast = false;
    org.springframework.amqp.core.Message mapped = MappingUtils.mapMessage(requestMessage, converter, headerMapper, defaultDeliveryMode, headersMappedLast);
    assertThat(mapped.getMessageProperties().getContentType(), equalTo("text/plain"));
    headersMappedLast = true;
    mapped = MappingUtils.mapMessage(requestMessage, converter, headerMapper, defaultDeliveryMode, headersMappedLast);
    assertThat(mapped.getMessageProperties().getContentType(), equalTo("my/ct"));
    ContentTypeDelegatingMessageConverter ctdConverter = new ContentTypeDelegatingMessageConverter();
    ctdConverter.addDelegate("my/ct", converter);
    mapped = MappingUtils.mapMessage(requestMessage, ctdConverter, headerMapper, defaultDeliveryMode, headersMappedLast);
    assertThat(mapped.getMessageProperties().getContentType(), equalTo("my/ct"));
    headersMappedLast = false;
    mapped = MappingUtils.mapMessage(requestMessage, ctdConverter, headerMapper, defaultDeliveryMode, headersMappedLast);
    assertThat(mapped.getMessageProperties().getContentType(), equalTo("text/plain"));
    headersMappedLast = true;
    requestMessage = MessageBuilder.withPayload("foo").setHeader(AmqpHeaders.CONTENT_TYPE, 42).build();
    try {
        mapped = MappingUtils.mapMessage(requestMessage, ctdConverter, headerMapper, defaultDeliveryMode, headersMappedLast);
        fail("Expected IllegalArgumentException");
    } catch (IllegalArgumentException e) {
        assertThat(e.getMessage(), equalTo("contentType header must be a MimeType or String, found: java.lang.Integer"));
    }
}
Also used : SimpleMessageConverter(org.springframework.amqp.support.converter.SimpleMessageConverter) ContentTypeDelegatingMessageConverter(org.springframework.amqp.support.converter.ContentTypeDelegatingMessageConverter) MessageConverter(org.springframework.amqp.support.converter.MessageConverter) SimpleMessageConverter(org.springframework.amqp.support.converter.SimpleMessageConverter) ContentTypeDelegatingMessageConverter(org.springframework.amqp.support.converter.ContentTypeDelegatingMessageConverter) MessageDeliveryMode(org.springframework.amqp.core.MessageDeliveryMode) Test(org.junit.Test)

Example 2 with MessageConverter

use of org.springframework.amqp.support.converter.MessageConverter in project spring-integration by spring-projects.

the class ChannelTests method messageConversionTests.

@Test
public void messageConversionTests() throws Exception {
    RabbitTemplate amqpTemplate = new RabbitTemplate(this.connectionFactory);
    MessageConverter messageConverter = mock(MessageConverter.class);
    amqpTemplate.setMessageConverter(messageConverter);
    PointToPointSubscribableAmqpChannel channel = new PointToPointSubscribableAmqpChannel("testConvertFail", new SimpleMessageListenerContainer(this.connectionFactory), amqpTemplate);
    channel.afterPropertiesSet();
    MessageListener listener = TestUtils.getPropertyValue(channel, "container.messageListener", MessageListener.class);
    willThrow(new MessageConversionException("foo", new IllegalStateException("bar"))).given(messageConverter).fromMessage(any(org.springframework.amqp.core.Message.class));
    this.exception.expect(MessageConversionException.class);
    this.exception.expectCause(instanceOf(IllegalStateException.class));
    listener.onMessage(mock(org.springframework.amqp.core.Message.class));
}
Also used : RabbitTemplate(org.springframework.amqp.rabbit.core.RabbitTemplate) MessageConversionException(org.springframework.amqp.support.converter.MessageConversionException) Message(org.springframework.messaging.Message) GenericMessage(org.springframework.messaging.support.GenericMessage) SimpleMessageListenerContainer(org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer) MessageListener(org.springframework.amqp.core.MessageListener) MessageConverter(org.springframework.amqp.support.converter.MessageConverter) Test(org.junit.Test)

Example 3 with MessageConverter

use of org.springframework.amqp.support.converter.MessageConverter in project spring-integration by spring-projects.

the class AmqpOutboundEndpoint method sendAndReceive.

private AbstractIntegrationMessageBuilder<?> sendAndReceive(String exchangeName, String routingKey, Message<?> requestMessage, CorrelationData correlationData) {
    Assert.isInstanceOf(RabbitTemplate.class, this.amqpTemplate, "RabbitTemplate implementation is required for publisher confirms");
    MessageConverter converter = ((RabbitTemplate) this.amqpTemplate).getMessageConverter();
    org.springframework.amqp.core.Message amqpMessage = MappingUtils.mapMessage(requestMessage, converter, getHeaderMapper(), getDefaultDeliveryMode(), isHeadersMappedLast());
    addDelayProperty(requestMessage, amqpMessage);
    org.springframework.amqp.core.Message amqpReplyMessage = ((RabbitTemplate) this.amqpTemplate).sendAndReceive(exchangeName, routingKey, amqpMessage, correlationData);
    if (amqpReplyMessage == null) {
        return null;
    }
    return buildReply(converter, amqpReplyMessage);
}
Also used : RabbitTemplate(org.springframework.amqp.rabbit.core.RabbitTemplate) MessageConverter(org.springframework.amqp.support.converter.MessageConverter)

Example 4 with MessageConverter

use of org.springframework.amqp.support.converter.MessageConverter in project spring-integration by spring-projects.

the class AmqpOutboundEndpoint method returnedMessage.

@Override
public void returnedMessage(org.springframework.amqp.core.Message message, int replyCode, String replyText, String exchange, String routingKey) {
    // safe to cast; we asserted we have a RabbitTemplate in doInit()
    MessageConverter converter = ((RabbitTemplate) this.amqpTemplate).getMessageConverter();
    Message<?> returned = buildReturnedMessage(message, replyCode, replyText, exchange, routingKey, converter);
    getReturnChannel().send(returned);
}
Also used : RabbitTemplate(org.springframework.amqp.rabbit.core.RabbitTemplate) MessageConverter(org.springframework.amqp.support.converter.MessageConverter)

Example 5 with MessageConverter

use of org.springframework.amqp.support.converter.MessageConverter in project spring-integration by spring-projects.

the class AmqpOutboundEndpoint method send.

private void send(String exchangeName, String routingKey, final Message<?> requestMessage, CorrelationData correlationData) {
    if (this.amqpTemplate instanceof RabbitTemplate) {
        MessageConverter converter = ((RabbitTemplate) this.amqpTemplate).getMessageConverter();
        org.springframework.amqp.core.Message amqpMessage = MappingUtils.mapMessage(requestMessage, converter, getHeaderMapper(), getDefaultDeliveryMode(), isHeadersMappedLast());
        addDelayProperty(requestMessage, amqpMessage);
        ((RabbitTemplate) this.amqpTemplate).send(exchangeName, routingKey, amqpMessage, correlationData);
    } else {
        this.amqpTemplate.convertAndSend(exchangeName, routingKey, requestMessage.getPayload(), message -> {
            getHeaderMapper().fromHeadersToRequest(requestMessage.getHeaders(), message.getMessageProperties());
            return message;
        });
    }
}
Also used : RabbitTemplate(org.springframework.amqp.rabbit.core.RabbitTemplate) MessageConverter(org.springframework.amqp.support.converter.MessageConverter)

Aggregations

MessageConverter (org.springframework.amqp.support.converter.MessageConverter)10 Test (org.junit.Test)6 RabbitTemplate (org.springframework.amqp.rabbit.core.RabbitTemplate)5 SimpleMessageConverter (org.springframework.amqp.support.converter.SimpleMessageConverter)5 MessageProperties (org.springframework.amqp.core.MessageProperties)3 SimpleMessageListenerContainer (org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer)3 Jackson2JsonMessageConverter (org.springframework.amqp.support.converter.Jackson2JsonMessageConverter)3 MessageConversionException (org.springframework.amqp.support.converter.MessageConversionException)3 Message (org.springframework.messaging.Message)3 GenericMessage (org.springframework.messaging.support.GenericMessage)3 Channel (com.rabbitmq.client.Channel)2 MessageListener (org.springframework.amqp.core.MessageListener)2 Connection (org.springframework.amqp.rabbit.connection.Connection)2 ConnectionFactory (org.springframework.amqp.rabbit.connection.ConnectionFactory)2 ChannelAwareMessageListener (org.springframework.amqp.rabbit.listener.api.ChannelAwareMessageListener)2 DirectChannel (org.springframework.integration.channel.DirectChannel)2 QueueChannel (org.springframework.integration.channel.QueueChannel)2 PollableChannel (org.springframework.messaging.PollableChannel)2 HashMap (java.util.HashMap)1 Message (org.springframework.amqp.core.Message)1