Reputation: 149
I have a scenario where I'm using Apache Camel (version 2.15.2) to serve up a REST service that allows "Profile" creation. The service attempts to split the Profile payload into multiple objects of different types (Section 1, 2, and 3) and delegates the creation of each object type to different applications. The complication is there's data that is created as part of Section 1, that needs to be used to create Sections 2 & 3. Here's an example route definition:
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");
The problem I'm seeing is that the log statement at the very end of the route, prints the response body exactly how I would expect it to. However, when I invoke this route from PostMan what returns is the result from the ".unmarshal().json(JsonLibrary.Jackson, Section1.class)".
I've tried debugging and enabling tracing on the route and I have yet to uncover an explanation as to why the result of my aggregation (which I've confirmed is working as expected) is not returned.
Any ideas?
Upvotes: 0
Views: 138
Reputation: 55750
The output of the aggregate is running in a separate thread from the input. And therefore the logging is happening after the REST response has sent back to the client.
The splitter has built-in aggregator you can use to aggregate in the same request, so you can use that as part of the REST response.
Upvotes: 0