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

Apache Camel 支持

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>7.0.5-SNAPSHOT</version>
</dependency>
compile "org.springframework.integration:spring-integration-camel:7.0.5-SNAPSHOT"

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

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

为了使开发过程更加简便并避免不必要的网络跳转,Spring Integration 现在提供了一种通道适配器来调用 Apache Camel 端点,并可选择性地等待回复。 不存在入站通道适配器,因为使用 Apache Camel Bean 绑定足以调用 Spring 应用上下文中的任何 Bean,包括 消息网关spring-doc.cadn.net.cn

Apache Camel 的出站通道适配器

The CamelMessageHandler 是一个 AbstractReplyProducingMessageHandler 实现,可以在单向(默认)和请求 - 回复模式下工作。 它使用 org.apache.camel.ProducerTemplate 将消息发送(或发送并接收)到 org.apache.camel.Endpoint 中。 交互模式可以通过 ExchangePattern 选项进行控制(该选项可通过 SpEL 表达式在运行时针对请求消息进行评估)。 目标 Apache Camel 端点可以显式配置,也可以作为在运行时评估的 SpEL 表达式。 否则,它将回退到 ProducerTemplate 上提供的 defaultEndpoint。 与其指定端点,不如提供一个内联的、显式的 LambdaRouteBuilder,例如,为了调用一个在 Spring Integration 中没有通道适配器支持的 Apache Camel 组件。spring-doc.cadn.net.cn

此外,可以提供 HeaderMapper<org.apache.camel.Message>CamelHeaderMapper 是默认实现),以确定在 Spring Integration 和 Apache Camel 消息之间映射哪些标头。 默认情况下,所有标头都会被映射。spring-doc.cadn.net.cn

The CamelMessageHandler 支持一种 async 模式,调用 ProducerTemplate.asyncSend() 并生成一个 CompletableFuture 用于回复处理(如果有)。spring-doc.cadn.net.cn

exchangeProperties 可以通过 SpEL 表达式进行自定义,该表达式的计算结果必须为 Mapspring-doc.cadn.net.cn

如果未提供 ProducerTemplate,则将通过从应用程序上下文中解析的 CamelContext 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 流定义,此通道适配器可以使用 Camel 工厂提供的几种变体进行配置: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()}"));
}