【问题标题】:Camel route response differs from the last point in my route骆驼路线响应与我路线中的最后一点不同
【发布时间】:2016-01-15 18:25:52
【问题描述】:

我有一个场景,我使用 Apache Camel(版本 2.15.2)来提供允许创建“配置文件”的 REST 服务。该服务尝试将 Profile 有效负载拆分为多个不同类型的对象(第 1、2 和 3 节),并将每个对象类型的创建委托给不同的应用程序。复杂之处在于,作为第 1 节的一部分创建的数据需要用于创建第 2 节和第 3 节。这是一个示例路线定义:

rest("/v1/Profile")
    .post().consumes("application/json").produces("application/json")
           .type(Profile.class)
           .description("Create a new profile")
           .route()
           // Save the original JSON payload into an exchange property
           .setProperty(ORIGINAL_PAYLOAD_KEY, simple("${in.body}"))

           // validate the payload
           .to(postProfileValidationEndpoint)

           // Extract Section 1 from the request 
           .marshal().json(JsonLibrary.Jackson)
           .transform().jsonpath("$.section1")
           .marshal().json(JsonLibrary.Jackson)

           // send the request to Section 1 app
           .to(section1Queue)
           .unmarshal().json(JsonLibrary.Jackson, Section1.class)

           // Save the profile id of the newly created section 1 instance
           .setHeader("profileId", new JsonPathExpression("$.profileId"))

           // Based on the original payload (see above), extract Section 2 and 3 as separate messages
           .split().method("profileSplitter", "generateProfileCreateMessages")
               .parallelProcessing()
               .choice()
                   .when(header(SECTION_2_REQUEST))
                       .to(section2Queue)
                   .when(header(SECTION_3_REQUEST))
                       .to(section3Queue)
               .end()

           // consolidate responses from section 2 and 3 applications
           .aggregate(header("breadcrumbId"), profileAggregationStrategy)
               .completionTimeout(timeout)                                                                     
               .completionSize(exchangeProperty(COMPLETION_SIZE_PROPERTY))
           .to("log:com.example.profile.route?level=DEBUG&showAll=true&multiline=true");

我看到的问题是,路径末尾的日志语句完全按照我的预期打印响应正文。但是,当我从 PostMan 调用此路由时,返回的是“.unmarshal().json(JsonLibrary.Jackson, Section1.class)”的结果。

我已经尝试调试并启用对路由的跟踪,但我还没有解释为什么我的聚合结果(我已经确认按预期工作)没有返回。

有什么想法吗?

【问题讨论】:

    标签: apache-camel integration


    【解决方案1】:

    聚合的输出在与输入不同的线程中运行。因此,日志记录是在 REST 响应发送回客户端之后发生的。

    拆分器具有内置聚合器,可用于在同一请求中聚合,因此您可以将其用作 REST 响应的一部分。

    【讨论】:

    • 感谢您的回复。我认为您的说法可能会发生,但是,当我调试路由时,直到我的自定义聚合策略完成后,响应才会返回给客户端。 FWIW,我还单独配置了聚合部分,因此我可以使用自定义聚合策略类以及完成超时和完成大小条件。有没有办法做到这一点并明确地将聚合器绑定到 split() 的范围?
    猜你喜欢
    • 2018-09-05
    • 1970-01-01
    • 2011-12-05
    • 2013-12-04
    • 2011-05-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多