Messaging Bridge
消息桥接器是一个相对微不足道的端点,连接两个消息通道或通道适配器。例如,您可能希望将 PollableChannel
连接到 SubscribableChannel
,以便订阅端点不必担心任何轮询配置。相反,消息桥接器提供轮询配置。
A messaging bridge is a relatively trivial endpoint that connects two message channels or channel adapters.
For example, you may want to connect a PollableChannel
to a SubscribableChannel
so that the subscribing endpoints do not have to worry about any polling configuration.
Instead, the messaging bridge provides the polling configuration.
通过在两个通道之间提供一个中介轮询器,您可以使用消息桥接器来限制入站消息。轮询器的触发器决定了消息到达第二个通道的速率,而轮询器的 maxMessagesPerPoll
属性对吞吐量执行限制。
By providing an intermediary poller between two channels, you can use a messaging bridge to throttle inbound messages.
The poller’s trigger determines the rate at which messages arrive at the second channel, and the poller’s maxMessagesPerPoll
property enforces a limit on the throughput.
消息桥接器的另一个有效用途是连接两个不同的系统。在这种情况下,Spring 集成的作用仅限于在这些系统之间建立连接和管理轮询器(如果需要)。在两个系统之间至少有一个转换器(转换它们的格式)可能是更为常见的。在这种情况下,通道可以作为转换器端点的“输入通道”和“输出通道”提供。如果不需要数据格式转换,则消息桥接器实际上就足够了。
Another valid use for a messaging bridge is to connect two different systems. In such a scenario, Spring Integration’s role is limited to making the connection between these systems and managing a poller, if necessary. It is probably more common to have at least a transformer between the two systems, to translate between their formats. In that case, the channels can be provided as the 'input-channel' and 'output-channel' of a transformer endpoint. If data format translation is not required, the messaging bridge may indeed be sufficient.
Configuring a Bridge with XML
可以使用 <bridge>
元素在两个消息管道或管道适配器之间创建一个消息桥。要进行此操作,请提供 input-channel
和 output-channel
属性,如以下示例所示:
You can use the <bridge>
element is used to create a messaging bridge between two message channels or channel adapters.
To do so, provide the input-channel
and output-channel
attributes, as the following example shows:
<int:bridge input-channel="input" output-channel="output"/>
如上所述,消息桥的一个常见用例是将 PollableChannel
连接到 SubscribableChannel
。在执行此角色时,消息桥还可以用作限流器:
As mentioned above, a common use case for the messaging bridge is to connect a PollableChannel
to a SubscribableChannel
.
When performing this role, the messaging bridge may also serve as a throttler:
<int:bridge input-channel="pollable" output-channel="subscribable">
<int:poller max-messages-per-poll="10" fixed-rate="5000"/>
</int:bridge>
可以使用类似的机制连接管道适配器。以下示例展示了 Spring Integration 的 stream
命名空间中 stdin
和 stdout
适配器之间的简单“echo
”:
You can use a similar mechanism to connecting channel adapters.
The following example shows a simple “echo” between the stdin
and stdout
adapters from Spring Integration’s stream
namespace:
<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 或邮件到文件。即将到来的章节将介绍各种管道适配器。
Similar configurations work for other (potentially more useful) Channel Adapter bridges, such as file-to-JMS or mail-to-file. Upcoming chapters cover the various channel adapters.
如果桥接上未定义“output-channel”, 则将使用输入消息提供的响应通道(如果可用)。如果既没有输出信道也没有响应信道可用,则会引发异常。 |
If no 'output-channel' is defined on a bridge, the reply channel provided by the inbound message is used, if available. If neither an output nor a reply channel is available, an exception is thrown. |
Configuring a Bridge with Java Configuration
以下示例展示了如何通过使用 @BridgeFrom
注解在 Java 中配置一个桥:
The following example shows how to configure a bridge in Java by using the @BridgeFrom
annotation:
@Bean
public PollableChannel polled() {
return new QueueChannel();
}
@Bean
@BridgeFrom(value = "polled", poller = @Poller(fixedDelay = "5000", maxMessagesPerPoll = "10"))
public SubscribableChannel direct() {
return new DirectChannel();
}
以下示例演示了如何通过使用 @BridgeTo
注解在 Java 中配置桥:
The following example shows how to configure a bridge in Java by using the @BridgeTo
annotation:
@Bean
@BridgeTo(value = "direct", poller = @Poller(fixedDelay = "5000", maxMessagesPerPoll = "10"))
public PollableChannel polled() {
return new QueueChannel();
}
@Bean
public SubscribableChannel direct() {
return new DirectChannel();
}
或者,可以使用 BridgeHandler
,如下例所示:
Alternately, you can use a BridgeHandler
, as the following example shows:
@Bean
@ServiceActivator(inputChannel = "polled",
poller = @Poller(fixedRate = "5000", maxMessagesPerPoll = "10"))
public BridgeHandler bridge() {
BridgeHandler bridge = new BridgeHandler();
bridge.setOutputChannelName("direct");
return bridge;
}
Configuring a Bridge with the Java DSL
可以使用 Java 领域特定语言 (DSL) 来配置桥,如下例所示:
You can use the Java Domain Specific Language (DSL) to configure a bridge, as the following example shows:
@Bean
public IntegrationFlow bridgeFlow() {
return IntegrationFlow.from("polled")
.bridge(e -> e.poller(Pollers.fixedDelay(5000).maxMessagesPerPoll(10)))
.channel("direct")
.get();
}