|
该版本仍在开发中,尚未被视为稳定。对于最新的稳定版本,请使用 Spring Integration 7.0.0! |
消息历史
消息传递架构的主要优势是松耦合,使得参与组件之间不保持任何对方的感知。 仅这一点就使应用极具灵活性,允许你更改组件而不影响流程的其他部分,改变消息路由,改变消息消费风格(轮询还是事件驱动),等等。 然而,这种不起眼的架构风格在出错时可能会变得困难。 调试时,你可能需要尽可能多的信息(消息来源、经过的通道以及其他细节)。
消息历史是其中一种模式,它通过提供你保持对消息路径一定程度的感知选项,无论是用于调试还是维护审计轨迹。 Spring集成提供了一种简单的方法,通过在消息中添加一个头部,并在消息经过跟踪组件时更新该头,来配置消息流以维护消息历史。
消息历史配置
要启用消息历史,只需定义消息历史元素(或@EnableMessageHistory在你的配置中,如下例所示:
-
Java
-
XML
@Configuration
@EnableIntegration
@EnableMessageHistory
<int:message-history/>
现在,所有有“id”定义的命名组件都会被追踪。
框架会在你的消息中设置“历史”头部。
其值为<物业列表>.
考虑以下配置示例:
-
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}]
要访问消息历史,只需访问消息历史页眉。
以下示例展示了如何实现:
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"));
你可能不想跟踪所有组件。
为了根据名称将历史限制在某些组件之间,你可以提供履带分量属性并指定一个逗号分隔的组件名称和模式列表,以匹配你想追踪的组件。
以下示例展示了如何实现:
-
Java
-
XML
@Configuration
@EnableIntegration
@EnableMessageHistory("*Gateway", "sample*", "aName")
<int:message-history tracked-components="*Gateway, sample*, aName"/>
在上述示例中,消息历史仅维护以“Gateway”结尾、以“sample”开头或与名称“aName”完全匹配的组件。
此外,消息历史配置器Bean现已被IntegrationMBeanExporter(参见 MBean 导出器),允许你在运行时更改模式。
但请注意,必须停止豆子(关闭消息历史)才能更改图案。
此功能可能有助于暂时打开历史以分析系统。
MBean 的对象名称为<domain>:name=messageHistoryConfigurer,type=MessageHistoryConfigurer.
只有一个@EnableMessageHistory(或<消息历史/>在应用上下文中必须声明为跟踪配置组件的单一源。
不要用通用的Beans定义来描述消息历史配置器. |
在6.3版本之前,消息历史头是不可变的(你无法重写历史):每一条轨道不仅创建了新的实例消息历史,但却是全新的消息副本。
现在它只能附加模式:第一轨生成一条新消息,包含新的消息历史容器。
其他的MessageHistory.write()调用会在现有头部添加新条目——且不会生成新消息。
这显著提升了应用性能。
框架中的所有组件,可以向多个消费者发送同一消息(发布订阅频道,摘要消息路由器,窃听等等),或者分流器根据输入消息产生多个输出,现在正在克隆已有的消息消息历史把头写进那些新消息里。
对于框架范围之外的其他多生产用例,摘要集成信息构建器.cloneMessageHistoryIfAny()推荐API以确保并行下游子流贡献自己的消息历史轨迹。 |