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

通道适配器

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

配置入站通道适配器

inbound-channel-adapter元素(一个SourcePollingChannelAdapter在 Java 配置中)可以调用 Spring 托管对象上的任何方法,并将非空返回值发送到MessageChannel将方法的输出转换为Message. 激活适配器的订阅后,轮询程序将尝试从源接收消息。 轮询器被调度为TaskScheduler根据提供的配置。 要为单个通道适配器配置轮询间隔或 cron 表达式,您可以为具有调度属性之一的“轮询器”元素提供,例如“固定速率”或“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>
如果未提供轮询器,则必须在上下文中注册单个默认轮询器。 有关更多详细信息,请参阅端点命名空间支持
轮询终结点的默认触发器是PeriodicTrigger实例与1第二个固定延迟期。
重要提示:轮询器配置

所有inbound-channel-adapter类型由SourcePollingChannelAdapter,这意味着它们包含轮询器配置,该配置轮询MessageSource(调用一个自定义方法,该方法生成成为Message有效负载)基于轮询器中指定的配置。 以下示例显示了两个轮询器的配置: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 条消息,而每次轮询以一秒的间隔发生。 但是,如果配置如下所示,会发生什么情况: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). 这确保轮询器可以对生命周期事件(例如启动和停止)做出反应,并防止它在无限循环中旋转,如果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 版开始,一个0max-messages-per-poll具有特殊含义 - 跳过MessageSource.receive()调用,这可能被视为此入站通道适配器的暂停,直到maxMessagesPerPoll稍后更改为非零值,例如通过控制总线。spring-doc.cadn.net.cn

从 6.2 版开始,fixed-delayfixed-rate可以配置为 ISO 8601 持续时间格式,例如PT10S,P1D等。 此外,一个initial-intervalPeriodicTrigger也以类似于fixed-delayfixed-rate.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子注释@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>
同时使用ref属性和内部处理程序定义<outbound-channel-adapter>不允许配置,因为它会产生不明确的条件。 这样的配置会导致引发异常。

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

通道适配器表达式和脚本

与许多其他 Spring Integration 组件一样,<inbound-channel-adapter><outbound-channel-adapter>还为 SpEL 表达评估提供支持。 要使用 SpEL,请在 'expression' 属性中提供表达式字符串,而不是提供用于在 Bean 上调用方法的 'ref' 和 'method' 属性。 当计算表达式时,它遵循与方法调用相同的契约,其中:一个<inbound-channel-adapter>每当评估结果为非空值时都会生成一条消息,而<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,这允许定期刷新资源。 如果希望在每次轮询时检查脚本,则需要将此设置与轮询器的触发器协调,如以下示例所示: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/>子元素。 有关表达式的更多信息,请参阅 Spring 表达式语言 (SpEL)。 有关脚本,请参阅 Groovy 支持脚本支持spring-doc.cadn.net.cn

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