此版本仍在开发中,尚未被视为稳定版本。对于最新的稳定版本,请使用 Spring Integration 6.4.3! |
Channel Adapter
通道适配器是一个消息端点,它允许将单个发送方或接收方连接到消息通道。 Spring 集成提供了许多适配器来支持各种传输,例如 JMS、文件、HTTP、Web 服务、邮件等。 本参考指南的后续章节将讨论每个适配器。 但是,本章重点介绍简单但灵活的方法调用通道适配器支持。 有入站和出站适配器,每个适配器都可以使用 core 命名空间中提供的 XML 元素进行配置。 这些提供了一种扩展 Spring Integration 的简单方法,只要你有一个可以作为源或目标调用的方法。
配置入站通道适配器
一inbound-channel-adapter
元素 (一个SourcePollingChannelAdapter
)可以在 Spring Management 的对象上调用任何方法,并将非 null 返回值发送到MessageChannel
将方法的输出转换为Message
.
激活适配器的订阅后,轮询器会尝试从源接收消息。
轮询器是使用TaskScheduler
根据提供的配置。
要为单个通道适配器配置轮询间隔或 cron 表达式,你可以提供一个 'poller' 元素,其中包含一个调度属性,例如 'fixed-rate' 或 'cron'。
以下示例定义了两个inbound-channel-adapter
实例:
-
Java DSL
-
Java
-
Kotlin DSL
-
XML
@Bean
public IntegrationFlow source1() {
return IntegrationFlow.from(() -> new GenericMessage<>(...),
e -> e.poller(p -> p.fixedRate(5000)))
...
.get();
}
@Bean
public IntegrationFlow source2() {
return IntegrationFlow.from(() -> new GenericMessage<>(...),
e -> e.poller(p -> p.cron("30 * 9-17 * * MON-FRI")))
...
.get();
}
public class SourceService {
@InboundChannelAdapter(channel = "channel1", poller = @Poller(fixedRate = "5000"))
Object method1() {
...
}
@InboundChannelAdapter(channel = "channel2", poller = @Poller(cron = "30 * 9-17 * * MON-FRI"))
Object method2() {
...
}
}
@Bean
fun messageSourceFlow() =
integrationFlow( { GenericMessage<>(...) },
{ poller { it.fixedRate(5000) } }) {
...
}
<int:inbound-channel-adapter ref="source1" method="method1" channel="channel1">
<int:poller fixed-rate="5000"/>
</int:inbound-channel-adapter>
<int:inbound-channel-adapter ref="source2" method="method2" channel="channel2">
<int:poller cron="30 * 9-17 * * MON-FRI"/>
</int:channel-adapter>
另请参阅 Channel Adapter 表达式和脚本。
如果未提供 Poller,则必须在上下文中注册单个默认 Poller。 有关更多详细信息,请参阅 Endpoint Namespace Support 。 |
轮询终端节点的默认触发器是PeriodicTrigger 具有 1 秒固定延迟周期的实例。 |
重要:轮询器配置
所有
在第一个配置中,轮询任务每次轮询调用一次,并且在每个任务(轮询)期间,根据
请注意,没有 但是,在 但是,如果您确定您的方法可以返回 null,并且您需要在每次轮询中轮询尽可能多的可用源,则应显式设置
从版本 5.5 开始,一个 从版本 6.2 开始, 另请参阅 Global Default Poller 了解更多信息。 |
配置出站通道适配器
一outbound-channel-adapter
元素 (一个@ServiceActivator
对于 Java 配置)也可以连接一个MessageChannel
到任何 POJO 使用者方法,该方法应该使用发送到该通道的消息的有效负载来调用。
以下示例显示如何定义出站通道适配器:
-
Java DSL
-
Java
-
Kotlin DSL
-
XML
@Bean
public IntegrationFlow outboundChannelAdapterFlow(MyPojo myPojo) {
return f -> f
.handle(myPojo, "handle");
}
public class MyPojo {
@ServiceActivator(channel = "channel1")
void handle(Object payload) {
...
}
}
@Bean
fun outboundChannelAdapterFlow(myPojo: MyPojo) =
integrationFlow {
handle(myPojo, "handle")
}
<int:outbound-channel-adapter channel="channel1" ref="target" method="handle"/>
<beans:bean id="target" class="org.MyPojo"/>
如果要适配的通道是PollableChannel
,您必须提供一个 Poller 子元素(@Poller
sub-annotation 的@ServiceActivator
),如下例所示:
-
Java
-
XML
public class MyPojo {
@ServiceActivator(channel = "channel1", poller = @Poller(fixedRate = "3000"))
void handle(Object payload) {
...
}
}
<int:outbound-channel-adapter channel="channel2" ref="target" method="handle">
<int:poller fixed-rate="3000" />
</int:outbound-channel-adapter>
<beans:bean id="target" class="org.MyPojo"/>
您应该使用ref
属性,如果 POJO 消费者实现可以在其他<outbound-channel-adapter>
定义。
但是,如果使用者实现仅由<outbound-channel-adapter>
,您可以将其定义为内部 Bean,如下例所示:
<int:outbound-channel-adapter channel="channel" method="handle">
<beans:bean class="org.Foo"/>
</int:outbound-channel-adapter>
同时使用ref attribute 和内部处理程序定义位于同一<outbound-channel-adapter> 不允许配置,因为它会产生不明确的条件。
此类配置会导致引发异常。 |
任何通道适配器都可以在没有channel
引用,在这种情况下,它会隐式地创建一个DirectChannel
.
创建的频道名称与id
属性的<inbound-channel-adapter>
或<outbound-channel-adapter>
元素。
因此,如果channel
未提供、id
是必需的。
通道适配器表达式和脚本
与许多其他 Spring 集成组件一样,<inbound-channel-adapter>
和<outbound-channel-adapter>
还提供对 SPEL 表达式评估的支持。
要使用 SPEL,请在'expression'属性中提供表达式字符串,而不是提供用于在 Bean 上调用方法的'ref'和'method'属性。
当一个表达式被求值时,它遵循与 method-invocation 相同的契约,其中:一个<inbound-channel-adapter>
每当评估结果为非 null 值时都会生成一条消息,而<outbound-channel-adapter>
必须等效于返回 void 的方法调用。
从 Spring Integration 3.0 开始,<int:inbound-channel-adapter/>
也可以使用 SPEL 进行配置<expression/>
(甚至使用<script/>
) 子元素,当需要比简单的 'expression' 属性更复杂时。
如果您将脚本作为Resource
通过使用location
属性,您还可以设置refresh-check-delay
,这允许定期刷新资源。
如果希望在每次轮询时检查脚本,则需要将此设置与 Poller 的触发器协调,如下例所示:
<int:inbound-channel-adapter ref="source1" method="method1" channel="channel1">
<int:poller max-messages-per-poll="1" fixed-delay="5000"/>
<script:script lang="ruby" location="Foo.rb" refresh-check-delay="5000"/>
</int:inbound-channel-adapter>
另请参阅cacheSeconds
属性ReloadableResourceBundleExpressionSource
使用<expression/>
sub-元素。
有关表达式的更多信息,请参阅 Spring 表达式语言 (SpEL)。
有关脚本,请参阅 Groovy 支持和脚本支持。
这<int:inbound-channel-adapter/> (SourcePollingChannelAdapter ) 是一个端点,它通过定期触发以轮询某些底层MessageSource .
由于在轮询时没有消息对象,因此表达式和脚本无权访问根Message ,因此在大多数其他消息传递 SPEL 表达式中没有可用的 payload 或 headers 属性。
该脚本可以生成并返回一个完整的Message 对象,或者仅是有效负载,框架将其添加到具有基本标头的消息中。 |