此版本仍在开发中,尚未被视为稳定版本。对于最新的稳定版本,请使用 Spring Integration 6.3.1! |
此版本仍在开发中,尚未被视为稳定版本。对于最新的稳定版本,请使用 Spring Integration 6.3.1! |
消息传递网桥是一个相对简单的端点,它连接两个消息通道或通道适配器。
例如,您可能希望将 a 连接到 a,以便订阅终结点不必担心任何轮询配置。
相反,消息传递网桥提供轮询配置。PollableChannel
SubscribableChannel
通过在两个通道之间提供中间轮询器,可以使用消息传递桥来限制入站消息。
轮询器的触发器确定消息到达第二个通道的速率,轮询器的属性强制限制吞吐量。maxMessagesPerPoll
消息网桥的另一个有效用途是连接两个不同的系统。 在这种情况下,Spring Integration 的作用仅限于在这些系统之间建立连接,并在必要时管理轮询器。 在两个系统之间至少有一个转换器,以便在它们的格式之间进行转换可能更常见。 在这种情况下,通道可以作为变压器端点的“输入通道”和“输出通道”提供。 如果不需要数据格式转换,则消息传递桥接可能确实足够了。
使用 XML 配置网桥
可以使用该元素在两个消息通道或通道适配器之间创建消息传递桥接。
为此,请提供 and 属性,如以下示例所示:<bridge>
input-channel
output-channel
<int:bridge input-channel="input" output-channel="output"/>
如上所述,消息传递网桥的一个常见用例是将 a 连接到 .
在执行此角色时,消息传递网桥还可以充当节流器:PollableChannel
SubscribableChannel
<int:bridge input-channel="pollable" output-channel="subscribable">
<int:poller max-messages-per-poll="10" fixed-rate="5000"/>
</int:bridge>
您可以使用类似的机制来连接通道适配器。
以下示例显示了 Spring Integration 命名空间中的 和 适配器之间的简单“回显”:stdin
stdout
stream
<int-stream:stdin-channel-adapter id="stdin"/>
<int-stream:stdout-channel-adapter id="stdout"/>
<int:bridge id="echo" input-channel="stdin" output-channel="stdout"/>
类似的配置适用于其他(可能更有用的)通道适配器网桥,例如文件到 JMS 或邮件到文件。 接下来的章节将介绍各种通道适配器。
如果未在网桥上定义“output-channel”,则使用入站消息提供的回复通道(如果可用)。 如果输出和应答通道都不可用,则会引发异常。 |
如果未在网桥上定义“output-channel”,则使用入站消息提供的回复通道(如果可用)。 如果输出和应答通道都不可用,则会引发异常。 |
使用 Java 配置配置网桥
以下示例演示如何使用注释在 Java 中配置网桥:@BridgeFrom
@Bean
public PollableChannel polled() {
return new QueueChannel();
}
@Bean
@BridgeFrom(value = "polled", poller = @Poller(fixedDelay = "5000", maxMessagesPerPoll = "10"))
public SubscribableChannel direct() {
return new DirectChannel();
}
以下示例演示如何使用注释在 Java 中配置网桥:@BridgeTo
@Bean
@BridgeTo(value = "direct", poller = @Poller(fixedDelay = "5000", maxMessagesPerPoll = "10"))
public PollableChannel polled() {
return new QueueChannel();
}
@Bean
public SubscribableChannel direct() {
return new DirectChannel();
}
或者,您可以使用 ,如以下示例所示:BridgeHandler
@Bean
@ServiceActivator(inputChannel = "polled",
poller = @Poller(fixedRate = "5000", maxMessagesPerPoll = "10"))
public BridgeHandler bridge() {
BridgeHandler bridge = new BridgeHandler();
bridge.setOutputChannelName("direct");
return bridge;
}
使用 Java DSL 配置网桥
您可以使用 Java 域特定语言 (DSL) 来配置网桥,如以下示例所示:
@Bean
public IntegrationFlow bridgeFlow() {
return IntegrationFlow.from("polled")
.bridge(e -> e.poller(Pollers.fixedDelay(5000).maxMessagesPerPoll(10)))
.channel("direct")
.get();
}