对于最新的稳定版本,请使用 Spring Integration 6.3.1Spring中文文档

对于最新的稳定版本,请使用 Spring Integration 6.3.1Spring中文文档

消息传递体系结构的主要优点是松耦合,使得参与的组件不会保持对彼此的任何感知。 仅这一事实就使应用程序变得非常灵活,允许您在不影响流的其余部分的情况下更改组件、更改消息传递路由、更改消息使用样式(轮询与事件驱动)等。 然而,当出现问题时,这种不起眼的建筑风格可能会被证明是困难的。 调试时,您可能希望尽可能多地获得有关消息的信息(其来源、已遍历的通道以及其他详细信息)。Spring中文文档

消息历史记录是其中一种模式,它为您提供了一个选项,用于维护对消息路径的某种程度的感知,以用于调试目的或维护审计跟踪。 Spring 集成提供了一种简单的方法来配置消息流以维护消息历史记录,方法是向消息添加标头并在每次消息通过跟踪组件时更新该标头。Spring中文文档

消息历史记录配置

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

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

现在,每个命名组件(定义了“id”)都会被跟踪。 该框架在消息中设置“历史记录”标头。 它的值是 a .List<Properties>Spring中文文档

请考虑以下配置示例:Spring中文文档

@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>

上述配置生成一个简单的消息历史记录结构,其输出类似于以下内容:Spring中文文档

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

要访问邮件历史记录,您只需要访问标头。 以下示例演示如何执行此操作:MessageHistorySpring中文文档

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-componentsSpring中文文档

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

在前面的示例中,仅对以“Gateway”结尾、以“sample”开头或与名称“aName”完全匹配的组件维护消息历史记录。Spring中文文档

此外,Bean 现在由 (参见 MBean 导出器) 公开为 JMX MBean,允许您在运行时更改模式。 但是请注意,必须停止 Bean (关闭消息历史记录) 才能更改模式。 此功能对于临时打开历史记录以分析系统可能很有用。 MBean 的对象名称是 。MessageHistoryConfigurerIntegrationMBeanExporter<domain>:name=messageHistoryConfigurer,type=MessageHistoryConfigurerSpring中文文档

在应用程序上下文中,只需声明一个(或)作为组件跟踪配置的单一源。 不要对 .@EnableMessageHistory<message-history/>MessageHistoryConfigurer
根据定义,消息历史记录标头是不可变的(您不能重写历史记录)。 因此,在写入消息历史记录值时,组件要么创建新消息(当组件是源时),要么从请求消息中复制历史记录,对其进行修改并在回复消息上设置新列表。 无论哪种情况,即使消息本身跨越线程边界,也可以附加这些值。 这意味着历史记录值可以大大简化异步消息流中的调试。
在应用程序上下文中,只需声明一个(或)作为组件跟踪配置的单一源。 不要对 .@EnableMessageHistory<message-history/>MessageHistoryConfigurer
根据定义,消息历史记录标头是不可变的(您不能重写历史记录)。 因此,在写入消息历史记录值时,组件要么创建新消息(当组件是源时),要么从请求消息中复制历史记录,对其进行修改并在回复消息上设置新列表。 无论哪种情况,即使消息本身跨越线程边界,也可以附加这些值。 这意味着历史记录值可以大大简化异步消息流中的调试。