如需使用最新稳定版本,请使用 Spring Integration 7.0.4spring-doc.cadn.net.cn

元数据存储

许多外部系统、服务或资源是非事务性的(例如 Twitter、RSS、文件系统,等等),并且没有任何能力将数据标记为已读。 此外,有时您可能需要在某些集成解决方案中实现企业集成模式 幂等接收器。 为了实现这一目标并在与外部系统的下一次交互之前存储端点的先前状态,或者处理下一条消息,Spring Integration 提供了元数据存储组件,该组件作为 org.springframework.integration.metadata.MetadataStore 接口的实现,并提供通用的键值契约。spring-doc.cadn.net.cn

元数据存储旨在存储各种类型的通用元数据(例如,已处理的最后一个馈送条目的发布日期),以帮助诸如馈送适配器之类的组件处理重复项。 如果某个组件未直接获得对 MetadataStore 的引用,则定位元数据存储的算法如下:首先,在应用程序上下文中查找 ID 为 metadataStore 的 Bean。 如果找到,则使用它。 否则,创建 SimpleMetadataStore 的新实例,这是一个内存中的实现,仅在当前运行的应用程序上下文的生命周期内持久化元数据。 这意味着,重启后,您可能会遇到重复条目。spring-doc.cadn.net.cn

如果您需要在应用程序上下文重启之间持久化元数据,该框架提供以下持久的 MetadataStoresspring-doc.cadn.net.cn

The PropertiesPersistingMetadataStore 由属性文件和一个 PropertiesPersister 提供支持。spring-doc.cadn.net.cn

默认情况下,它仅在应用程序上下文正常关闭时持久化状态。 它实现了Flushable,以便您可以通过调用flush()按需持久化状态。 以下示例展示了如何使用 XML 配置'PropertiesPersistingMetadataStore':spring-doc.cadn.net.cn

<bean id="metadataStore"
    class="org.springframework.integration.metadata.PropertiesPersistingMetadataStore"/>

或者,您可以提供MetadataStore接口的自定义实现(例如JdbcMetadataStore),并将其作为 Bean 配置在应用程序上下文中。spring-doc.cadn.net.cn

从版本 4.0 开始,SimpleMetadataStorePropertiesPersistingMetadataStoreRedisMetadataStore 实现了 ConcurrentMetadataStore。 这些提供了原子更新功能,可用于多个组件或应用程序实例之间。spring-doc.cadn.net.cn

幂等接收器和元数据存储

元数据存储对于实现 EIP 幂等接收器 模式非常有用,当需要过滤已处理过的传入消息时,您可以丢弃该消息或执行其他逻辑。 以下配置展示了如何实现这一点:spring-doc.cadn.net.cn

<int:filter input-channel="serviceChannel"
			output-channel="idempotentServiceChannel"
			discard-channel="discardChannel"
			expression="@metadataStore.get(headers.businessKey) == null"/>

<int:publish-subscribe-channel id="idempotentServiceChannel"/>

<int:outbound-channel-adapter channel="idempotentServiceChannel"
                              expression="@metadataStore.put(headers.businessKey, '')"/>

<int:service-activator input-channel="idempotentServiceChannel" ref="service"/>

幂等条目的 value 可以是过期日期,此后该条目应由某个定时清理程序从元数据存储中移除。spring-doc.cadn.net.cn

MetadataStoreListener

某些元数据存储(目前仅支持 ZooKeeper)支持注册监听器以在项发生变化时接收事件,如下例所示:spring-doc.cadn.net.cn

public interface MetadataStoreListener {

	void onAdd(String key, String value);

	void onRemove(String key, String oldValue);

	void onUpdate(String key, String newValue);
}

查看 Javadoc 以获取更多信息。 如果您只感兴趣于部分事件,可以继承 MetadataStoreListenerAdapter 类。spring-doc.cadn.net.cn