// use requestBody as its InOut Objectout= template.requestBody("direct:start", "A@B@C"); assertEquals(expectedBody, out); LOG.debug("Response to caller: " + out);
@Override protected RouteBuilder createRouteBuilder()throws Exception { returnnewRouteBuilder() { @Override publicvoidconfigure()throws Exception { // START SNIPPET: e1 // this routes starts from the direct:start endpoint // the body is then splitted based on @ separator // the splitter in Camel supports InOut as well and for that we // need // to be able to aggregate what response we need to send back, // so we provide our // own strategy with the class MyOrderStrategy. from("direct:start").split(body().tokenize("@"), newMyOrderStrategy()) // each splitted message is then send to this bean where we // can process it .to("bean:MyOrderService?method=handleOrder") // this is important to end the splitter route as we do not // want to do more routing // on each splitted message .end() // after we have splitted and handled each message we want // to send a single combined // response back to the original caller, so we let this bean // build it for us // this bean will receive the result of the aggregate // strategy: MyOrderStrategy .to("bean:MyOrderService?method=buildCombinedResponse") // END SNIPPET: e1 .to("mock:result"); } }; }
/** * We just handle the order by returning a id line for the order */ public String handleOrder(String line) { LOG.debug("HandleOrder: " + line); return"(id=" + ++counter + ",item=" + line + ")"; }
/** * We use the same bean for building the combined response to send back to the original caller */ public String buildCombinedResponse(String line) { LOG.debug("BuildCombinedResponse: " + line); return"Response[" + line + "]"; } } // END SNIPPET: e2
// START SNIPPET: e3 /** * This is our own order aggregation strategy where we can control how each splitted message should be combined. As * we do not want to loos any message we copy from the new to the old to preserve the order lines as long we process * them */ publicstaticclassMyOrderStrategyimplementsAggregationStrategy {
@Override public Exchange aggregate(Exchange oldExchange, Exchange newExchange) { // put order together in old exchange by adding the order from new // exchange
if (oldExchange == null) { // the first time we aggregate we only have the new exchange, // so we just return it return newExchange; }
LOG.debug("Aggregate old orders: " + orders); LOG.debug("Aggregate new order: " + newLine);
// put orders together separating by semi colon orders = orders + ";" + newLine; // put combined order back on old to preserve it oldExchange.getIn().setBody(orders);
// return old as this is the one that has all the orders gathered // until now return oldExchange; } } // END SNIPPET: e3