对于最新的稳定版本,请使用 Spring Integration 6.5.1! |
通道适配器
通道适配器是一个消息端点,用于将单个发送方或接收方连接到消息通道。 Spring Integration 提供了许多适配器来支持各种传输,例如 JMS、文件、HTTP、Web 服务、邮件等。 本参考指南的后续章节将讨论每个适配器。 但是,本章重点介绍简单但灵活的方法调用通道适配器支持。 有入站和出站适配器,每个适配器都可以配置为核心命名空间中提供的 XML 元素。 这些提供了一种扩展 Spring Integration 的简单方法,只要您有一个可以作为源或目标调用的方法。
配置入站通道适配器
一inbound-channel-adapter
元素(一个SourcePollingChannelAdapter
在 Java 配置中)可以调用 Spring 托管对象上的任何方法,并将非空返回值发送到MessageChannel
将方法的输出转换为Message
.
激活适配器的订阅后,轮询程序将尝试从源接收消息。
轮询器被调度为TaskScheduler
根据提供的配置。
要为单个通道适配器配置轮询间隔或 cron 表达式,您可以为具有调度属性之一的“轮询器”元素提供,例如“固定速率”或“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>
另请参阅通道适配器表达式和脚本。
如果未提供轮询器,则必须在上下文中注册单个默认轮询器。 有关更多详细信息,请参阅端点命名空间支持。 |
轮询终结点的默认触发器是PeriodicTrigger 具有 1 秒固定延迟期的实例。 |
重要提示:轮询器配置
所有
在第一个配置中,每次轮询调用一次轮询任务,并且在每个任务(轮询)期间,该方法(导致消息生成)调用一次,基于
请注意,没有 但是,在 但是,如果您确定您的方法可以返回 null,并且您需要为每个轮询轮询尽可能多的可用源,则应显式设置
从 5.5 版开始,一个 从 6.2 版开始, 另请参阅全局默认轮询器了解更多信息。 |
配置出站通道适配器
一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
子注释@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 属性和内部处理程序定义<outbound-channel-adapter> 不允许配置,因为它会产生不明确的条件。
这样的配置会导致引发异常。 |
可以在没有channel
引用,在这种情况下,它会隐式创建DirectChannel
.
创建的通道名称与id
属性的<inbound-channel-adapter>
或<outbound-channel-adapter>
元素。
因此,如果channel
未提供,id
是必需的。
通道适配器表达式和脚本
与许多其他 Spring Integration 组件一样,<inbound-channel-adapter>
和<outbound-channel-adapter>
还为 SpEL 表达评估提供支持。
要使用 SpEL,请在 'expression' 属性中提供表达式字符串,而不是提供用于在 Bean 上调用方法的 'ref' 和 'method' 属性。
当计算表达式时,它遵循与方法调用相同的契约,其中:一个<inbound-channel-adapter>
每当评估结果为非空值时都会生成一条消息,而<outbound-channel-adapter>
必须等效于返回 void 的方法调用。
从 Spring Integration 3.0 开始,一个<int:inbound-channel-adapter/>
也可以配置 SpEL<expression/>
(甚至使用<script/>
) 子元素,用于需要比使用简单的“expression”属性更复杂的情况。
如果您将脚本作为Resource
通过使用location
属性,也可以设置refresh-check-delay
,这允许定期刷新资源。
如果希望在每次轮询时检查脚本,则需要将此设置与轮询器的触发器协调,如以下示例所示:
<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/>
子元素。
有关表达式的更多信息,请参阅 Spring 表达式语言 (SpEL)。
有关脚本,请参阅 Groovy 支持和脚本支持。
这<int:inbound-channel-adapter/> (SourcePollingChannelAdapter ) 是一个端点,它通过定期触发轮询某些底层来启动消息流MessageSource .
由于在轮询时没有消息对象,因此表达式和脚本无权访问根Message ,因此没有大多数其他消息传递 SpEL 表达式中可用的有效负载或标头属性。
脚本可以生成并返回完整的Message 对象,带有标头和有效负载,或者仅有效负载,该负载由框架添加到具有基本标头的消息中。 |