Hi,
Do you need to use a bean/producer template to do this?

Some of this might be built right in to the Recipient List EIP.  For
example, in Spring:

<recipientList delimiter="|" parallelProcessing="true"
strategyRef="aggregateResponses" >
    <header>endpointsToCall</header>
</recipientList>

In the above snippet, you set a header with your endpoints, a delimiter of
'|' and a strategy to aggregate all your responses (if you need to).  This
also give you the advantage of being able to call your endpoint in parallel
to speed up your POST (if your requirements allow you to do so). 

In my test case, I needed to invoke a REST service so I used the HTTP Jetty
component to do the POST.  I used the recipient list because I was calling
the service multiple times .   My aggregator was simple as well:

        @Override
        public Exchange aggregate(Exchange oldExchange, Exchange newExchange) {
                log.debug("Entering message aggregator");
                log.debug("Class of new exchange: " +
newExchange.getIn().getBody().getClass());
                
                StringBuffer sb = new StringBuffer();
                
                //If we have information in the old exchange, append the old 
and new
exchange together
                //Another processor will put a thin wrapper around them
                if (oldExchange != null)
                {
                        String oldMessage = 
oldExchange.getIn().getBody(String.class);
                                        
                        //Remove the XML declaration if it exists because we 
are combining into a
single message
                        oldMessage = StringUtils.remove(oldMessage, "<?xml 
version=\"1.0\"
encoding=\"UTF-8\"?>");
                        
                        sb.append(oldMessage);
                        
                }       
                
                String newMessage = newExchange.getIn().getBody(String.class);
                newMessage = StringUtils.remove(newMessage, "<?xml 
version=\"1.0\"
encoding=\"UTF-8\"?>");
        
                sb.append(newMessage);
                
                newExchange.getIn().setBody(sb.toString());
                
                return newExchange;
        }       

After that, I had a simple bean processor that wrapped the XML messages in a
thin wrapper:

        public void wrapExchangeBody(Exchange exchange) {
                log.debug("Entering wrap exchange body");
                
                StringBuffer sb = new StringBuffer();
                
                String aggregatedMessage = 
exchange.getIn().getBody(String.class);
                
                sb.append("<Wrapper>");
                sb.append(aggregatedMessage);
                sb.append("</Wrapper>");
                
                exchange.getIn().setBody(sb.toString());
                
        }       

After that I had all the rest responses wrapped and ran them through an XSLT
to make the desired response message I needed.

Would something like this help?

Thanks,
Yogesh

--
View this message in context: 
http://camel.465427.n5.nabble.com/Best-Practice-for-splitting-on-header-tp5713433p5713571.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Reply via email to