消息历史

消息架构的关键优势在于松耦合,参与组件彼此之间不保持任何感知。 仅此一点就使应用程序极其灵活,允许您在不影响流程其余部分的情况下更改组件、更改消息路由、更改消息消费样式(轮询与事件驱动)等等。 然而,当出现问题时,这种不显眼的架构风格可能会变得困难。 在调试时,您可能希望获得关于消息的尽可能多的信息(其来源、它所遍历的通道以及其他详细信息)。 消息历史是其中一种模式,它通过提供一个选项来帮助您维护消息路径的某种程度的感知,无论是用于调试目的还是用于维护审计跟踪。 Spring Integration 提供了一种简单的方法来配置您的消息流,通过向消息添加一个头并在消息每次通过被跟踪的组件时更新该头来维护消息历史。

消息历史配置

要启用消息历史,您只需在配置中定义 message-history 元素(或 @EnableMessageHistory),如以下示例所示:

  • Java

  • XML

@Configuration
@EnableIntegration
@EnableMessageHistory
<int:message-history/>

现在,每个命名组件(定义了“id”的组件)都被跟踪。 框架在您的消息中设置“history”头。 其值为 List<Properties>

考虑以下配置示例:

  • Java

  • XML

@MessagingGateway(defaultRequestChannel = "bridgeInChannel")
public interface SampleGateway {
   ...
}

@Bean
@Transformer(inputChannel = "enricherChannel", outputChannel="filterChannel")
HeaderEnricher sampleEnricher() {
    HeaderEnricher enricher =
           new HeaderEnricher(Collections.singletonMap("baz", new StaticHeaderValueMessageProcessor("baz")));
    return enricher;
}
<int:gateway id="sampleGateway"
    service-interface="org.springframework.integration.history.sample.SampleGateway"
    default-request-channel="bridgeInChannel"/>

<int:header-enricher id="sampleEnricher" input-channel="enricherChannel" output-channel="filterChannel">
    <int:header name="baz" value="baz"/>
</int:header-enricher>

上述配置产生了一个简单的消息历史结构,输出类似于以下内容:

[{name=sampleGateway, type=gateway, timestamp=1283281668091},
 {name=sampleEnricher, type=header-enricher, timestamp=1283281668094}]

要访问消息历史,您只需访问 MessageHistory 头。 以下示例展示了如何操作:

Iterator<Properties> historyIterator =
    message.getHeaders().get(MessageHistory.HEADER_NAME, MessageHistory.class).iterator();
assertTrue(historyIterator.hasNext());
Properties gatewayHistory = historyIterator.next();
assertEquals("sampleGateway", gatewayHistory.get("name"));
assertTrue(historyIterator.hasNext());
Properties chainHistory = historyIterator.next();
assertEquals("sampleChain", chainHistory.get("name"));

您可能不想跟踪所有组件。 要根据组件名称将历史限制在某些组件,您可以提供 tracked-components 属性并指定一个逗号分隔的组件名称和模式列表,这些模式匹配您要跟踪的组件。 以下示例展示了如何操作:

  • Java

  • XML

@Configuration
@EnableIntegration
@EnableMessageHistory("*Gateway", "sample*", "aName")
<int:message-history tracked-components="*Gateway, sample*, aName"/>

在前面的示例中,消息历史仅为以“Gateway”结尾、以“sample”开头或精确匹配名称“aName”的组件维护。

此外,MessageHistoryConfigurer bean 现在由 IntegrationMBeanExporter 作为 JMX MBean 公开(请参阅 MBean Exporter),允许您在运行时更改模式。 但是请注意,必须停止 bean(关闭消息历史)才能更改模式。 此功能可能有助于暂时打开历史以分析系统。 MBean 的对象名称是 <domain>:name=messageHistoryConfigurer,type=MessageHistoryConfigurer

应用程序上下文中只能声明一个 @EnableMessageHistory(或 <message-history/>)作为组件跟踪配置的单一来源。 不要为 MessageHistoryConfigurer 使用通用 bean 定义。

在 6.3 版本之前,消息历史头是不可变的(您不能重写历史):每次跟踪不仅创建 MessageHistory 的新实例,而且还创建了一个全新的消息副本。 现在它以仅追加模式工作:第一次跟踪创建了一个带有新 MessageHistory 容器的新消息。 所有其余的 MessageHistory.write() 调用都会将新条目添加到现有头中 - 并且不会创建新消息。 这显著提高了应用程序性能。 框架中的所有组件,其中相同的消息可以发送给多个消费者(PublishSubscribeChannelAbstractMessageRouterWireTap 等),或者拆分器根据输入消息产生多个输出,现在都将现有的 MessageHistory 头克隆到这些新消息中。 对于框架范围之外的任何其他多生成用例,建议使用 AbstractIntegrationMessageBuilder.cloneMessageHistoryIfAny() API,以确保并行的下游子流贡献它们自己的消息历史轨迹。