此版本仍在开发中,尚未被视为稳定版本。对于最新的稳定版本,请使用 Spring Integration 6.4.3spring-doc.cadn.net.cn

Channel Adapter

通道适配器是一个消息端点,它允许将单个发送方或接收方连接到消息通道。 Spring 集成提供了许多适配器来支持各种传输,例如 JMS、文件、HTTP、Web 服务、邮件等。 本参考指南的后续章节将讨论每个适配器。 但是,本章重点介绍简单但灵活的方法调用通道适配器支持。 有入站和出站适配器,每个适配器都可以使用 core 命名空间中提供的 XML 元素进行配置。 这些提供了一种扩展 Spring Integration 的简单方法,只要你有一个可以作为源或目标调用的方法。spring-doc.cadn.net.cn

配置入站通道适配器

inbound-channel-adapter元素 (一个SourcePollingChannelAdapter)可以在 Spring Management 的对象上调用任何方法,并将非 null 返回值发送到MessageChannel将方法的输出转换为Message. 激活适配器的订阅后,轮询器会尝试从源接收消息。 轮询器是使用TaskScheduler根据提供的配置。 要为单个通道适配器配置轮询间隔或 cron 表达式,你可以提供一个 'poller' 元素,其中包含一个调度属性,例如 'fixed-rate' 或 'cron'。 以下示例定义了两个inbound-channel-adapter实例:spring-doc.cadn.net.cn

@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>
如果未提供 Poller,则必须在上下文中注册单个默认 Poller。 有关更多详细信息,请参阅 Endpoint Namespace Support
轮询终端节点的默认触发器是PeriodicTrigger具有 1 秒固定延迟周期的实例。
重要:轮询器配置

所有inbound-channel-adapter类型由SourcePollingChannelAdapter,这意味着它们包含一个轮询MessageSource(要调用一个自定义方法,该方法生成的值将变为Messagepayload) 的 Payload 中。 以下示例显示了两个 Poller 的配置:spring-doc.cadn.net.cn

<int:poller max-messages-per-poll="1" fixed-rate="1000"/>

<int:poller max-messages-per-poll="10" fixed-rate="1000"/>

在第一个配置中,轮询任务每次轮询调用一次,并且在每个任务(轮询)期间,根据max-messages-per-poll属性值。 在第二种配置中,轮询任务每次轮询调用 10 次,或者直到它返回 'null',因此每次轮询可能会生成 10 条消息,而每次轮询的间隔为 1 秒。 但是,如果配置类似于以下示例,会发生什么情况:spring-doc.cadn.net.cn

<int:poller fixed-rate="1000"/>

请注意,没有max-messages-per-poll指定。 正如我们稍后介绍的,PollingConsumer(例如,service-activator,filter,router等)的默认值为-1max-messages-per-poll,这意味着“不停地执行轮询任务,除非轮询方法返回 null(可能是因为QueueChannel)“,然后睡一秒钟。spring-doc.cadn.net.cn

但是,在SourcePollingChannelAdapter,这有点不同。 的默认值max-messages-per-poll1,除非您将其显式设置为负值(例如-1). 这确保了 Poller 可以对生命周期事件(例如启动和停止)做出反应,并防止它可能在无限循环中旋转(如果MessageSource有可能永远不会返回 null,并且恰好是不可中断的。spring-doc.cadn.net.cn

但是,如果您确定您的方法可以返回 null,并且您需要在每次轮询中轮询尽可能多的可用源,则应显式设置max-messages-per-poll设置为负值,如下例所示:spring-doc.cadn.net.cn

<int:poller max-messages-per-poll="-1" fixed-rate="1000"/>

从版本 5.5 开始,一个0的值max-messages-per-poll具有特殊含义 - 跳过MessageSource.receive()调用,这可能被视为此入站通道适配器的暂停,直到maxMessagesPerPoll稍后更改为非零值,例如通过 Control Bus。spring-doc.cadn.net.cn

从版本 6.2 开始,fixed-delayfixed-rate可以 ISO 8601 Duration 格式进行配置,例如PT10S,P1D等。 此外,一个initial-interval底层证券PeriodicTrigger也以与fixed-delayfixed-rate.spring-doc.cadn.net.cn

另请参阅 Global Default Poller 了解更多信息。spring-doc.cadn.net.cn

配置出站通道适配器

outbound-channel-adapter元素 (一个@ServiceActivator对于 Java 配置)也可以连接一个MessageChannel到任何 POJO 消费者方法,该方法应该使用发送到该通道的消息的有效负载来调用。 以下示例说明如何定义出站通道适配器:spring-doc.cadn.net.cn

@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 子元素(@Pollersub-annotation 的@ServiceActivator),如下例所示:spring-doc.cadn.net.cn

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,如下例所示:spring-doc.cadn.net.cn

<int:outbound-channel-adapter channel="channel" method="handle">
    <beans:bean class="org.Foo"/>
</int:outbound-channel-adapter>
同时使用refattribute 和内部处理程序定义位于同一<outbound-channel-adapter>不允许配置,因为它会产生不明确的条件。 此类配置会导致引发异常。

任何通道适配器都可以在没有channel引用,在这种情况下,它会隐式地创建一个DirectChannel. 创建的频道名称与id属性的<inbound-channel-adapter><outbound-channel-adapter>元素。 因此,如果channel未提供、id是必需的。spring-doc.cadn.net.cn

通道适配器表达式和脚本

与许多其他 Spring 集成组件一样,<inbound-channel-adapter><outbound-channel-adapter>还提供对 SPEL 表达式评估的支持。 要使用 SPEL,请在'expression'属性中提供表达式字符串,而不是提供用于在 Bean 上调用方法的'ref'和'method'属性。 当一个表达式被求值时,它遵循与 method-invocation 相同的契约,其中:一个<inbound-channel-adapter>每当评估结果为非 null 值时都会生成一条消息,而<outbound-channel-adapter>必须等效于返回 void 的方法调用。spring-doc.cadn.net.cn

从 Spring Integration 3.0 开始,<int:inbound-channel-adapter/>也可以使用 SPEL 进行配置<expression/>(甚至使用<script/>) 子元素,当需要比简单的 'expression' 属性更复杂时。 如果您将脚本作为Resource通过使用location属性,您还可以设置refresh-check-delay,这允许定期刷新资源。 如果希望在每次轮询时检查脚本,则需要将此设置与 Poller 的触发器协调,如下例所示:spring-doc.cadn.net.cn

<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 支持脚本支持spring-doc.cadn.net.cn

<int:inbound-channel-adapter/> (SourcePollingChannelAdapter) 是一个端点,它通过定期触发以轮询某些底层MessageSource. 由于在轮询时没有消息对象,因此表达式和脚本无权访问根Message,因此在大多数其他消息传递 SPEL 表达式中没有可用的 payload 或 headers 属性。 该脚本可以生成并返回一个完整的Message对象,或者仅是有效负载,框架将其添加到具有基本标头的消息中。