You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@camel.apache.org by Jared O'Connor <ja...@hotmail.com> on 2012/02/07 23:06:53 UTC

Message Correlation in Request/Reply


Hi
Note: Please ignore my previous email to the developers mailing list; my mail client seemed to mangle the line breaks, resulting in an un-readable message. Regardless, this users list seems more appropriate, given that I am not a Camel developer.
I recently encountered an interesting problem, where I needed to set an output message via an enricher with a dynamic URI or an aggregator. The StackOverflow link below explains the situation.

http://stackoverflow.com/questions/9171291/camel-request-reply-correlation

I discussed this with "cschneide" on IRC, who I believe is a Camel developer, and it would seem new Camel functionality may need to be investigated. Enabling dynamic URIs on enrichers is one option, but I would like to suggest an alternative. See below.

Web Service

@Component @Path("/")public class WebService {  @GET @Path("files/{id}")  public String getFile(@PathParam("id") String id) {  return null; }}
Aggregation Strategy
@Componentpublic class Aggregator implements AggregationStrategy {  public Exchange aggregate(Exchange oldExchange, Exchange newExchange) {    if (oldExchange == null) {   return newExchange;  }    else {   oldExchange.getOut().setBody(newExchange.getIn().getBody());   return oldExchange;  }  }}
Routes
<route>  <from uri="cxfrs://bean:webService"/>  <choice>    <when>      <simple>${in.headers.operationName} == 'getFile'</simple>      <setHeader headerName="correlationId">        <simple>mandatoryBodyAs(java.lang.String)</simple>      </setHeader>      <to uri="seda:aggregation"/>    </when>  <choice></route><route>  <from uri="file://someFolder"/>  <setHeader headerName="correlationId">    <simple>file:name</simple>  </setHeader>  <convertBodyTo type="java.lang.String"/>  <to uri="seda:aggregation"/></route><route>  <from uri="seda:aggregation"/>  <aggregate completionSize="2" completionTimeout="10000" strategyRef="aggregator">    <correlationExpression>      <simple>in.headers.correlationId</simple>    </correlationExpression>    <stop/>  </aggregate>  <transform>    <simple>${out.body}NOT AGGREGATED</simple>  </transform></route>
Explanation

The aggregator component currently only handles "InOnly" exchanges. Thus, the routes above would immediately return "NOT AGGREGATED", rather than returning the file contents after aggregation has occurred. I suggest allowing aggregators to handle "InOut" exchanges. This solution is overkill for simple correlations that could be achieved via an expression on a polling enricher. However, it would allow you to respond with messages that have to be read or parsed for the correlation identifier. In other words, this allows you to use a message from one route as the response for another.

What are your thoughts? Is there any way to achieve this in Camel currently?
Thanks