Source:
<code>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 { message = messageBuilderFactory.fromMessage(message).setHeader(HEADER_NAME, history).build(); <START> } <END> } return message; }</code><technical_language>I if consider making message history header mutable - a MessageHistoryHolder; way, rebuild message in trackable component; subsequent track get a new MessageHistory MB.build() detect existing holder, create a new transfer existing history. way, pub/subs inherit history to-date, a unique holder changes leg of pub/sub reflected in other other leg. If MB.build() create a new holder, get cross-talk. MessageHistory is a container object, able avoid a new class - clone in MB replace components new writes</technical_language>

Target:
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; }