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"));
}
}
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));
}
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);
}
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);
}
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;
});
}
}
Aggregations