跳到主要内容

入站通道适配器

QWen Plus 中英对照 Inbound Channel Adapters

通常情况下,消息流从入站通道适配器开始(例如 <int-jdbc:inbound-channel-adapter>)。适配器配置有 <poller>,并且它会定期要求 MessageSource<?> 生成消息。Java DSL 也允许从 MessageSource<?> 启动 IntegrationFlow。为此,IntegrationFlow 流式 API 提供了一个重载的 IntegrationFlow.from(MessageSource<?> messageSource) 方法。您可以将 MessageSource<?> 配置为一个 bean 并将其作为该方法的参数提供。IntegrationFlow.from() 的第二个参数是一个 Consumer<SourcePollingChannelAdapterSpec> lambda,它允许您为 SourcePollingChannelAdapter 提供选项(如 PollerMetadataSmartLifecycle)。以下示例展示了如何使用流式 API 和 lambda 创建 IntegrationFlow

@Bean
public MessageSource<Object> jdbcMessageSource() {
return new JdbcPollingChannelAdapter(this.dataSource, "SELECT * FROM something");
}

@Bean
public IntegrationFlow pollingFlow() {
return IntegrationFlow.from(jdbcMessageSource(),
c -> c.poller(Pollers.fixedRate(100).maxMessagesPerPoll(1)))
.transform(Transformers.toJson())
.channel("furtherProcessChannel")
.get();
}
java

对于那些没有直接构建 Message 对象需求的情况,你可以使用基于 java.util.function.SupplierIntegrationFlow.fromSupplier() 变体。Supplier.get() 的结果会自动包装在一个 Message 中(如果它不是一个 Message 的话)。