use of org.springframework.integration.support.MutableMessageBuilderFactory in project spring-integration by spring-projects.
the class AggregatingMessageGroupProcessorHeaderTests method testNullHeaderValue.
@Test
public void testNullHeaderValue() {
DefaultAggregatingMessageGroupProcessor processor = new DefaultAggregatingMessageGroupProcessor();
DirectFieldAccessor dfa = new DirectFieldAccessor(processor);
dfa.setPropertyValue("messageBuilderFactory", new MutableMessageBuilderFactory());
Map<String, Object> headers1 = new HashMap<String, Object>();
headers1.put("k1", "foo");
headers1.put("k2", null);
Message<?> message1 = MutableMessageBuilder.withPayload("test").setCorrelationId(1).setSequenceNumber(1).setSequenceSize(2).copyHeadersIfAbsent(headers1).build();
Map<String, Object> headers2 = new HashMap<String, Object>();
headers2.put("k1", "bar");
headers2.put("k2", 123);
Message<?> message2 = correlatedMessage(1, 2, 2, headers2);
List<Message<?>> messages = Arrays.<Message<?>>asList(message1, message2);
MessageGroup group = new SimpleMessageGroup(messages, 1);
Object result = processor.processMessageGroup(group);
assertNotNull(result);
assertTrue(result instanceof Message<?>);
Message<?> resultMessage = (Message<?>) result;
assertNull(resultMessage.getHeaders().get("k1"));
assertNull(resultMessage.getHeaders().get("k2"));
headers1 = new HashMap<String, Object>();
headers1.put("k1", "foo");
headers1.put("k2", 123);
message1 = correlatedMessage(1, 2, 1, headers1);
headers2 = new HashMap<String, Object>();
headers2.put("k1", "bar");
headers2.put("k2", null);
message2 = MutableMessageBuilder.withPayload("test").setCorrelationId(1).setSequenceNumber(2).setSequenceSize(2).copyHeadersIfAbsent(headers2).build();
messages = Arrays.<Message<?>>asList(message1, message2);
group = new SimpleMessageGroup(messages, 1);
result = processor.processMessageGroup(group);
resultMessage = (Message<?>) result;
assertNull(resultMessage.getHeaders().get("k1"));
assertNull(resultMessage.getHeaders().get("k2"));
}
use of org.springframework.integration.support.MutableMessageBuilderFactory 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.MutableMessageBuilderFactory in project spring-integration by spring-projects.
the class MessageBuilderTests method mutableFromImmutableMutate.
@Test
public void mutableFromImmutableMutate() {
Message<String> message1 = MessageBuilder.withPayload("test").setHeader("foo", "bar").build();
Message<String> message2 = new MutableMessageBuilderFactory().fromMessage(message1).setHeader("another", 1).build();
assertEquals("bar", message2.getHeaders().get("foo"));
assertSame(message1.getHeaders().getId(), message2.getHeaders().getId());
assertNotSame(message1, message2);
assertFalse(message2 == message1);
}
Aggregations