Search in sources :

Example 1 with MutableMessageBuilderFactory

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"));
}
Also used : Message(org.springframework.messaging.Message) HashMap(java.util.HashMap) MessageGroup(org.springframework.integration.store.MessageGroup) SimpleMessageGroup(org.springframework.integration.store.SimpleMessageGroup) SimpleMessageGroup(org.springframework.integration.store.SimpleMessageGroup) DirectFieldAccessor(org.springframework.beans.DirectFieldAccessor) MutableMessageBuilderFactory(org.springframework.integration.support.MutableMessageBuilderFactory) Test(org.junit.Test)

Example 2 with MutableMessageBuilderFactory

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;
}
Also used : AdviceMessage(org.springframework.integration.message.AdviceMessage) ErrorMessage(org.springframework.messaging.support.ErrorMessage) MutableMessage(org.springframework.integration.support.MutableMessage) Message(org.springframework.messaging.Message) GenericMessage(org.springframework.messaging.support.GenericMessage) AdviceMessage(org.springframework.integration.message.AdviceMessage) Properties(java.util.Properties) IntegrationMessageHeaderAccessor(org.springframework.integration.IntegrationMessageHeaderAccessor) GenericMessage(org.springframework.messaging.support.GenericMessage) MutableMessage(org.springframework.integration.support.MutableMessage) MutableMessageBuilderFactory(org.springframework.integration.support.MutableMessageBuilderFactory) DefaultMessageBuilderFactory(org.springframework.integration.support.DefaultMessageBuilderFactory) ErrorMessage(org.springframework.messaging.support.ErrorMessage)

Example 3 with MutableMessageBuilderFactory

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);
}
Also used : MutableMessageBuilderFactory(org.springframework.integration.support.MutableMessageBuilderFactory) Test(org.junit.Test)

Aggregations

MutableMessageBuilderFactory (org.springframework.integration.support.MutableMessageBuilderFactory)3 Test (org.junit.Test)2 Message (org.springframework.messaging.Message)2 HashMap (java.util.HashMap)1 Properties (java.util.Properties)1 DirectFieldAccessor (org.springframework.beans.DirectFieldAccessor)1 IntegrationMessageHeaderAccessor (org.springframework.integration.IntegrationMessageHeaderAccessor)1 AdviceMessage (org.springframework.integration.message.AdviceMessage)1 MessageGroup (org.springframework.integration.store.MessageGroup)1 SimpleMessageGroup (org.springframework.integration.store.SimpleMessageGroup)1 DefaultMessageBuilderFactory (org.springframework.integration.support.DefaultMessageBuilderFactory)1 MutableMessage (org.springframework.integration.support.MutableMessage)1 ErrorMessage (org.springframework.messaging.support.ErrorMessage)1 GenericMessage (org.springframework.messaging.support.GenericMessage)1