Example usage for org.springframework.integration.support MessageBuilderFactory fromMessage

List of usage examples for org.springframework.integration.support MessageBuilderFactory fromMessage

Introduction

In this page you can find the example usage for org.springframework.integration.support MessageBuilderFactory fromMessage.

Prototype

<T> AbstractIntegrationMessageBuilder<T> fromMessage(Message<T> message);

Source Link

Usage

From source file:xolpoc.plugins.StreamPlugin.java

private void track(final Module module, MessageChannel channel, final Map<String, Object> historyProps) {
    final MessageBuilderFactory messageBuilderFactory = module.getComponent(
            IntegrationUtils.INTEGRATION_MESSAGE_BUILDER_FACTORY_BEAN_NAME, MessageBuilderFactory.class) == null
                    ? new DefaultMessageBuilderFactory()
                    : module.getComponent(IntegrationUtils.INTEGRATION_MESSAGE_BUILDER_FACTORY_BEAN_NAME,
                            MessageBuilderFactory.class);
    if (channel instanceof ChannelInterceptorAware) {
        ((ChannelInterceptorAware) channel).addInterceptor(new ChannelInterceptorAdapter() {

            @Override//from   ww  w . j av  a  2 s. com
            public Message<?> preSend(Message<?> message, MessageChannel channel) {
                @SuppressWarnings("unchecked")
                Collection<Map<String, Object>> history = (Collection<Map<String, Object>>) message.getHeaders()
                        .get(XdHeaders.XD_HISTORY);
                if (history == null) {
                    history = new ArrayList<Map<String, Object>>(1);
                } else {
                    history = new ArrayList<Map<String, Object>>(history);
                }
                Map<String, Object> map = new LinkedHashMap<String, Object>();
                map.putAll(historyProps);
                map.put("thread", Thread.currentThread().getName());
                history.add(map);
                Message<?> out = messageBuilderFactory.fromMessage(message)
                        .setHeader(XdHeaders.XD_HISTORY, history).build();
                return out;
            }
        });
    }
}

From source file:org.springframework.integration.history.MessageHistory.java

@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);/*  w  w  w .  ja v  a 2  s. co m*/
        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;
}