use of org.springframework.integration.support.MutableMessage in project spring-integration by spring-projects.
the class RedisMessageGroupStoreTests method testJsonSerialization.
@Test
@RedisAvailable
public void testJsonSerialization() {
RedisConnectionFactory jcf = getConnectionFactoryForTest();
RedisMessageStore store = new RedisMessageStore(jcf);
ObjectMapper mapper = JacksonJsonUtils.messagingAwareMapper();
GenericJackson2JsonRedisSerializer serializer = new GenericJackson2JsonRedisSerializer(mapper);
store.setValueSerializer(serializer);
Message<?> genericMessage = new GenericMessage<>(new Date());
Message<?> mutableMessage = new MutableMessage<>(UUID.randomUUID());
Message<?> adviceMessage = new AdviceMessage<>("foo", genericMessage);
ErrorMessage errorMessage = new ErrorMessage(new RuntimeException("test exception"), mutableMessage);
store.addMessagesToGroup(this.groupId, genericMessage, mutableMessage, adviceMessage, errorMessage);
MessageGroup messageGroup = store.getMessageGroup(this.groupId);
assertEquals(4, messageGroup.size());
List<Message<?>> messages = new ArrayList<>(messageGroup.getMessages());
assertEquals(genericMessage, messages.get(0));
assertEquals(mutableMessage, messages.get(1));
assertEquals(adviceMessage, messages.get(2));
Message<?> errorMessageResult = messages.get(3);
assertEquals(errorMessage.getHeaders(), errorMessageResult.getHeaders());
assertThat(errorMessageResult, instanceOf(ErrorMessage.class));
assertEquals(errorMessage.getOriginalMessage(), ((ErrorMessage) errorMessageResult).getOriginalMessage());
assertEquals(errorMessage.getPayload().getMessage(), ((ErrorMessage) errorMessageResult).getPayload().getMessage());
Message<Foo> fooMessage = new GenericMessage<>(new Foo("foo"));
try {
store.addMessageToGroup(this.groupId, fooMessage).getMessages().iterator().next();
fail("SerializationException expected");
} catch (Exception e) {
assertThat(e.getCause().getCause(), instanceOf(IllegalArgumentException.class));
assertThat(e.getMessage(), containsString("The class with " + "org.springframework.integration.redis.store.RedisMessageGroupStoreTests$Foo and name of " + "org.springframework.integration.redis.store.RedisMessageGroupStoreTests$Foo " + "is not in the trusted packages:"));
}
mapper = JacksonJsonUtils.messagingAwareMapper(getClass().getPackage().getName());
serializer = new GenericJackson2JsonRedisSerializer(mapper);
store.setValueSerializer(serializer);
store.removeMessageGroup(this.groupId);
messageGroup = store.addMessageToGroup(this.groupId, fooMessage);
assertEquals(1, messageGroup.size());
assertEquals(fooMessage, messageGroup.getMessages().iterator().next());
mapper = JacksonJsonUtils.messagingAwareMapper("*");
serializer = new GenericJackson2JsonRedisSerializer(mapper);
store.setValueSerializer(serializer);
store.removeMessageGroup(this.groupId);
messageGroup = store.addMessageToGroup(this.groupId, fooMessage);
assertEquals(1, messageGroup.size());
assertEquals(fooMessage, messageGroup.getMessages().iterator().next());
}
use of org.springframework.integration.support.MutableMessage in project spring-integration by spring-projects.
the class MessageHistory method write.
@SuppressWarnings("unchecked")
public static <T> Message<T> write(Message<T> message, NamedComponent component, MessageBuilderFactory messageBuilderFactory) {
Assert.notNull(message, "Message must not be null");
Assert.notNull(component, "Component must not be null");
Properties metadata = extractMetadata(component);
if (!metadata.isEmpty()) {
MessageHistory previousHistory = message.getHeaders().get(HEADER_NAME, MessageHistory.class);
List<Properties> components = (previousHistory != null) ? new ArrayList<Properties>(previousHistory) : new ArrayList<Properties>();
components.add(metadata);
MessageHistory history = new MessageHistory(components);
if (message instanceof MutableMessage) {
message.getHeaders().put(HEADER_NAME, history);
} else if (message instanceof ErrorMessage) {
IntegrationMessageHeaderAccessor headerAccessor = new IntegrationMessageHeaderAccessor(message);
headerAccessor.setHeader(HEADER_NAME, history);
Throwable payload = ((ErrorMessage) message).getPayload();
ErrorMessage errorMessage = new ErrorMessage(payload, headerAccessor.toMessageHeaders());
message = (Message<T>) errorMessage;
} else if (message instanceof AdviceMessage) {
IntegrationMessageHeaderAccessor headerAccessor = new IntegrationMessageHeaderAccessor(message);
headerAccessor.setHeader(HEADER_NAME, history);
message = new AdviceMessage<T>(message.getPayload(), headerAccessor.toMessageHeaders(), ((AdviceMessage<?>) message).getInputMessage());
} else {
if (!(message instanceof GenericMessage) && (messageBuilderFactory instanceof DefaultMessageBuilderFactory || messageBuilderFactory instanceof MutableMessageBuilderFactory)) {
if (logger.isWarnEnabled()) {
logger.warn("MessageHistory rebuilds the message and produces the result of the [" + messageBuilderFactory + "], not an instance of the provided type [" + message.getClass() + "]. Consider to supply a custom MessageBuilderFactory " + "to retain custom messages during MessageHistory tracking.");
}
}
message = messageBuilderFactory.fromMessage(message).setHeader(HEADER_NAME, history).build();
}
}
return message;
}
use of org.springframework.integration.support.MutableMessage in project spring-integration by spring-projects.
the class MessageHistoryTests method testCorrectMutableMessageAfterWrite.
@Test
public void testCorrectMutableMessageAfterWrite() {
MutableMessage<String> original = new MutableMessage<>("foo");
assertNull(MessageHistory.read(original));
Message<String> result1 = MessageHistory.write(original, new TestComponent(1));
assertThat(result1, instanceOf(MutableMessage.class));
assertSame(original, result1);
MessageHistory history1 = MessageHistory.read(result1);
assertNotNull(history1);
assertEquals("testComponent-1", history1.toString());
Message<String> result2 = MessageHistory.write(result1, new TestComponent(2));
assertSame(original, result2);
MessageHistory history2 = MessageHistory.read(result2);
assertNotNull(history2);
assertEquals("testComponent-1,testComponent-2", history2.toString());
}
Aggregations