use of org.springframework.amqp.support.converter.Jackson2JsonMessageConverter in project spring-integration by spring-projects.
the class JsonConverterCompatibilityTests method setUp.
@Before
public void setUp() {
this.rabbitTemplate = new RabbitTemplate(new CachingConnectionFactory("localhost"));
this.rabbitTemplate.setMessageConverter(new Jackson2JsonMessageConverter());
}
use of org.springframework.amqp.support.converter.Jackson2JsonMessageConverter in project fru-paqx-parent by dellemc-symphony.
the class RabbitConfig method messageConverter.
@Bean
public MessageConverter messageConverter() {
Jackson2JsonMessageConverter messageConverter = new Jackson2JsonMessageConverter();
messageConverter.setClassMapper(classMapper());
messageConverter.setCreateMessageIds(true);
final ObjectMapper objectMapper = new ObjectMapper();
// use ISO8601 format for dates
objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
messageConverter.setJsonObjectMapper(objectMapper);
// ignore properties we don't need or aren't expecting
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
messageConverter.setJsonObjectMapper(objectMapper);
return messageConverter;
}
use of org.springframework.amqp.support.converter.Jackson2JsonMessageConverter in project spring-integration by spring-projects.
the class InboundEndpointTests method testInt2809JavaTypePropertiesToAmqp.
@Test
public void testInt2809JavaTypePropertiesToAmqp() throws Exception {
Connection connection = mock(Connection.class);
doAnswer(invocation -> mock(Channel.class)).when(connection).createChannel(anyBoolean());
ConnectionFactory connectionFactory = mock(ConnectionFactory.class);
when(connectionFactory.createConnection()).thenReturn(connection);
SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();
container.setConnectionFactory(connectionFactory);
container.setAcknowledgeMode(AcknowledgeMode.MANUAL);
AmqpInboundChannelAdapter adapter = new AmqpInboundChannelAdapter(container);
adapter.setMessageConverter(new Jackson2JsonMessageConverter());
PollableChannel channel = new QueueChannel();
adapter.setOutputChannel(channel);
adapter.setBeanFactory(mock(BeanFactory.class));
adapter.afterPropertiesSet();
Object payload = new Foo("bar1");
Transformer objectToJsonTransformer = new ObjectToJsonTransformer();
Message<?> jsonMessage = objectToJsonTransformer.transform(new GenericMessage<Object>(payload));
MessageProperties amqpMessageProperties = new MessageProperties();
amqpMessageProperties.setDeliveryTag(123L);
org.springframework.amqp.core.Message amqpMessage = new SimpleMessageConverter().toMessage(jsonMessage.getPayload(), amqpMessageProperties);
DefaultAmqpHeaderMapper.inboundMapper().fromHeadersToRequest(jsonMessage.getHeaders(), amqpMessageProperties);
ChannelAwareMessageListener listener = (ChannelAwareMessageListener) container.getMessageListener();
Channel rabbitChannel = mock(Channel.class);
listener.onMessage(amqpMessage, rabbitChannel);
Message<?> result = channel.receive(1000);
assertEquals(payload, result.getPayload());
assertSame(rabbitChannel, result.getHeaders().get(AmqpHeaders.CHANNEL));
assertEquals(123L, result.getHeaders().get(AmqpHeaders.DELIVERY_TAG));
}
use of org.springframework.amqp.support.converter.Jackson2JsonMessageConverter in project spring-integration by spring-projects.
the class InboundEndpointTests method testInt2809JavaTypePropertiesFromAmqp.
@Test
public void testInt2809JavaTypePropertiesFromAmqp() throws Exception {
Connection connection = mock(Connection.class);
doAnswer(invocation -> mock(Channel.class)).when(connection).createChannel(anyBoolean());
ConnectionFactory connectionFactory = mock(ConnectionFactory.class);
when(connectionFactory.createConnection()).thenReturn(connection);
SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();
container.setConnectionFactory(connectionFactory);
AmqpInboundChannelAdapter adapter = new AmqpInboundChannelAdapter(container);
PollableChannel channel = new QueueChannel();
adapter.setOutputChannel(channel);
adapter.setBeanFactory(mock(BeanFactory.class));
adapter.afterPropertiesSet();
Object payload = new Foo("bar1");
MessageProperties amqpMessageProperties = new MessageProperties();
org.springframework.amqp.core.Message amqpMessage = new Jackson2JsonMessageConverter().toMessage(payload, amqpMessageProperties);
ChannelAwareMessageListener listener = (ChannelAwareMessageListener) container.getMessageListener();
listener.onMessage(amqpMessage, null);
Message<?> receive = channel.receive(1000);
Message<?> result = new JsonToObjectTransformer().transform(receive);
assertEquals(payload, result.getPayload());
}
use of org.springframework.amqp.support.converter.Jackson2JsonMessageConverter in project spring-integration by spring-projects.
the class InboundEndpointTests method testMessageConverterJsonHeadersHavePrecedenceOverMessageHeaders.
@Test
public void testMessageConverterJsonHeadersHavePrecedenceOverMessageHeaders() throws Exception {
Connection connection = mock(Connection.class);
doAnswer(invocation -> mock(Channel.class)).when(connection).createChannel(anyBoolean());
ConnectionFactory connectionFactory = mock(ConnectionFactory.class);
when(connectionFactory.createConnection()).thenReturn(connection);
SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();
container.setConnectionFactory(connectionFactory);
container.setAcknowledgeMode(AcknowledgeMode.MANUAL);
DirectChannel channel = new DirectChannel();
final Channel rabbitChannel = mock(Channel.class);
channel.subscribe(new MessageTransformingHandler(message -> {
assertSame(rabbitChannel, message.getHeaders().get(AmqpHeaders.CHANNEL));
assertEquals(123L, message.getHeaders().get(AmqpHeaders.DELIVERY_TAG));
return MessageBuilder.fromMessage(message).setHeader(JsonHeaders.TYPE_ID, "foo").setHeader(JsonHeaders.CONTENT_TYPE_ID, "bar").setHeader(JsonHeaders.KEY_TYPE_ID, "baz").build();
}));
RabbitTemplate rabbitTemplate = spy(new RabbitTemplate());
rabbitTemplate.setMessageConverter(new Jackson2JsonMessageConverter());
CountDownLatch sendLatch = new CountDownLatch(1);
Mockito.doAnswer(invocation -> {
org.springframework.amqp.core.Message message = invocation.getArgument(2);
Map<String, Object> headers = message.getMessageProperties().getHeaders();
assertTrue(headers.containsKey(JsonHeaders.TYPE_ID.replaceFirst(JsonHeaders.PREFIX, "")));
assertNotEquals("foo", headers.get(JsonHeaders.TYPE_ID.replaceFirst(JsonHeaders.PREFIX, "")));
assertFalse(headers.containsKey(JsonHeaders.CONTENT_TYPE_ID.replaceFirst(JsonHeaders.PREFIX, "")));
assertFalse(headers.containsKey(JsonHeaders.KEY_TYPE_ID.replaceFirst(JsonHeaders.PREFIX, "")));
assertFalse(headers.containsKey(JsonHeaders.TYPE_ID));
assertFalse(headers.containsKey(JsonHeaders.KEY_TYPE_ID));
assertFalse(headers.containsKey(JsonHeaders.CONTENT_TYPE_ID));
sendLatch.countDown();
return null;
}).when(rabbitTemplate).send(anyString(), anyString(), any(org.springframework.amqp.core.Message.class), isNull());
AmqpInboundGateway gateway = new AmqpInboundGateway(container, rabbitTemplate);
gateway.setMessageConverter(new Jackson2JsonMessageConverter());
gateway.setRequestChannel(channel);
gateway.setBeanFactory(mock(BeanFactory.class));
gateway.setDefaultReplyTo("foo");
gateway.afterPropertiesSet();
Object payload = new Foo("bar1");
MessageProperties amqpMessageProperties = new MessageProperties();
amqpMessageProperties.setDeliveryTag(123L);
org.springframework.amqp.core.Message amqpMessage = new Jackson2JsonMessageConverter().toMessage(payload, amqpMessageProperties);
ChannelAwareMessageListener listener = (ChannelAwareMessageListener) container.getMessageListener();
listener.onMessage(amqpMessage, rabbitChannel);
assertTrue(sendLatch.await(10, TimeUnit.SECONDS));
}
Aggregations