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

阿帕奇骆驼支持

Spring Integration 提供了一个 API 和配置,用于与在同一应用上下文中声明的 Apache Camel 端点通信。spring-doc.cadn.net.cn

你需要把这种依赖性纳入你的项目中:spring-doc.cadn.net.cn

<dependency>
    <groupId>org.springframework.integration</groupId>
    <artifactId>spring-integration-camel</artifactId>
    <version>6.3.12-SNAPSHOT</version>
</dependency>
compile "org.springframework.integration:spring-integration-camel:6.3.12-SNAPSHOT"

Spring Integration 和 Apache Camel 实现了企业集成模式,并提供了方便的组合方式,但这两个项目在 API 和抽象实现上采用了不同的方法。Spring Integration 完全依赖于 Spring Core 的依赖注入容器。它在通道适配器实现中使用了许多其他 Spring 项目(如 Spring Data、Spring AMQP、Spring for Apache Kafka 等)。它还使用消息频道抽象作为一等公民,开发者在编写集成流程时需要注意这一点。 而Apache Camel则不提供一流公民的消息通道抽象,而是通过内部交换来构建路由,且对API隐藏。 此外,它需要一些额外的依赖和配置,才能在 Spring 应用中使用。spring-doc.cadn.net.cn

即使最终企业集成解决方案的各个部分如何实现并不重要,开发者的体验和高生产力也会被考虑。 因此,开发者可能会出于多种原因选择其中一个框架,或者如果某些目标系统支持存在缺口,则两者都选择。 Spring Integration 和 Apache Camel 应用程序可以通过多种外部协议相互交互,并为其实现了通道适配器。 例如,Spring集成流程可能会将记录发布到Apache Kafka主题,该记录被消费者端的Apache Camel端点消耗。 或者,Apache Camel路由可以将数据写入目录的SFTP文件,由Spring Integration的SFTP入站通道适配器轮询。 或者,在同一Spring应用上下文中,它们可以通过ApplicationEvent 抽象spring-doc.cadn.net.cn

为了简化开发过程并避免不必要的网络跳转,Apache Camel 提供了一个模块,通过消息通道与 Spring Integration 通信。 只需引用一个消息频道从应用上下文中,用于发送或接收消息。 当Apache Camel路由作为消息流的发起者,而Spring Integration仅作为解决方案的一部分辅助角色时,这种方法效果良好。spring-doc.cadn.net.cn

为了实现类似的开发体验,Spring Integration 现在提供了一个通道适配器,可以调用 Apache Camel 端点,并可选地等待回复。 没有入站通道适配器,因为订阅消息频道从Spring集成API和抽象角度来看,用于消费Apache Camel消息就足够了。spring-doc.cadn.net.cn

Apache Camel 的出站通道适配器

骆驼消息处理器摘要回复制作消息处理器实现 和 可以同时工作于单向(默认)和请求-回复模式。 它使用了一个org.apache.camel.ProducerTemplate发送(或发送并接收)到org.apache.camel.Endpoint. 交互模式可以通过交换模式选项(可通过 SpEL 表达式在运行时与请求消息进行评估)。 目标Apache Camel端点可以显式配置,也可以作为运行时评估的SpEL表达式配置。 否则,它会回落到defaultEndpoint提供于制作模板. 没有指定端点,而是内联,显式LambdaRouteBuilder例如,可以调用 Spring Integration 中不支持通道适配器的 Apache Camel 组件。spring-doc.cadn.net.cn

此外,还有一个HeaderMapper<org.apache.camel.Message>骆驼头地图可以提供 Spring Integration 和 Apache Camel 消息之间映射哪些头部。 默认情况下,所有头部都是映射的。spring-doc.cadn.net.cn

骆驼消息处理器支撑一个异步模式调用ProducerTemplate.asyncSend()并产生完成未来用于回复处理(如果有的话)。spring-doc.cadn.net.cn

交换属性可以通过SpEL表达式进行定制,该表达式必须计算为地图.spring-doc.cadn.net.cn

如果制作模板不提供,而是通过骆驼语境Bean从应用上下文中解析。spring-doc.cadn.net.cn

@Bean
@ServiceActivator(inputChannel = "sendToCamel")
CamelMessageHandler camelService(ProducerTemplate producerTemplate) {
    CamelHeaderMapper headerMapper = new CamelHeaderMapper();
    headerMapper.setOutboundHeaderNames("");
    headerMapper.setInboundHeaderNames("testHeader");

    CamelMessageHandler camelMessageHandler = new CamelMessageHandler(producerTemplate);
    camelMessageHandler.setEndpointUri("direct:simple");
    camelMessageHandler.setExchangePatternExpression(spelExpressionParser.parseExpression("headers.exchangePattern"));
    camelMessageHandler.setHeaderMapper(headerMapper);
    return camelMessageHandler;
}

对于Java DSL流定义,该通道适配器可以通过以下几种变体配置骆驼厂:spring-doc.cadn.net.cn

@Bean
IntegrationFlow camelFlow() {
    return f -> f
            .handle(Camel.gateway().endpointUri("direct:simple"))
            .handle(Camel.route(this::camelRoute))
            .handle(Camel.handler().endpointUri("log:com.mycompany.order?level=WARN"));
}

private void camelRoute(RouteBuilder routeBuilder) {
    routeBuilder.from("direct:inbound").transform(routeBuilder.simple("${body.toUpperCase()}"));
}