问题
I am trying to receive a xml message from a rest call, then make a call to the db, map each row into an object and then return the complete marshalled object as the rest response.
However my db query result is being returned as the rest response even before the last process has executed. Can you please advise if there is something wrong I am doing that is resulting in the dB query being returned?
Please also advise if my solution is the correct approach, As the real db query will have about 25000 lines and I am worried my web service times out.
Route:
from("jetty://http://localhost:8888/...")
.log("Message Received")
.setExchangePattern(ExchangePattern.InOut)
.unmarshal(soapMessage)
.removeHeaders("CamelHttp")
.to("direct:ProcessRequest");
from("direct:ProcessRequest")
.setBody(constant("SELECT * FROM table"))
.to("jdbc:dataSource").split(body()).parallelProcessing()
.process(rowProcessor)
.aggregate(constant(true), new ArrayListAggregationStrategy())
.completionFromBatchConsumer()
.process(combinedProcess)
rowProcessor
RowResult rowResult = new RowResult();
Map<String, Object> row = exchange.getIn().getBody(Map.class);
System.out.println("Processing " + row);
rowResult.setName((String) row.get("name"));
rowResult.setSurname((String) row.get("surname"));
exchange.getIn().setBody(responserowResultDetail);
combinedProcess
System.out.println("Bulk process");
People people = new People();
List<rowResult> rowResults = (List<rowResult>)exchange.getIn().getBody();
people.getEveryone().addAll(rowResults);
exchange.getIn().setBody(people);
exchange.getIn().setHeader(Exchange.CONTENT_TYPE, MediaType.APPLICATION_XML);
I am getting the following from rest response:
[{id=1, name = test ...... }]
回答1:
I think that your direct:ProcessRequest
thread is doing exactly what it should do - i.e. it is returning what went into the split as you've not got an aggregation strategy linked to the split. What I think you need to do is get the split to do the aggregation, and then it will work. I've not tested this, but I think you need something like this:
from("direct:ProcessRequest")
.setBody(constant("SELECT * FROM table"))
.to("jdbc:dataSource")
.split(body(), new ArrayListAggregationStrategy() )
.parallelProcessing()
.process(rowProcessor)
.end()
.completionFromBatchConsumer()
.process(combinedProcess)
来源:https://stackoverflow.com/questions/57671284/camel-rest-to-db-query-returning-incorrect-response