Hello,

I am adding a message header inside of the aggregate and then I am checking
that header in route's onCompletion callback.
It seems that this does not work, header does not exist inside of the
callback. Is this an error or should it really work this way?

Also is onCompletion called once per incoming message or only once? I have
seen both variants.

Sample code:

package ee.test;

import org.apache.camel.CamelContext;
import org.apache.camel.Exchange;
import org.apache.camel.ProducerTemplate;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.impl.DefaultCamelContext;
import org.apache.camel.processor.aggregate.AggregationStrategy;

/**
 * A Camel Router
 */
public class MyRouteBuilder extends RouteBuilder {

    /**
     * Let's configure the Camel routing rules using Java code...
     */
    @Override
    public void configure() {

        from("direct:start")
            .onCompletion()
                .log("commonHdr: ${header.commonHdr}")
                .log("aggregationHdr: ${header.aggregationHdr}")
                .to("log:end")
                .end()
            .setHeader("commonHdr", constant("header"))
            .aggregate(header("id"), new
StringAggregationStrategy()).completionSize(2)
                .setHeader("aggregationHdr", constant("header"))
                .to("mock:aggregatedEqual");
    }

    //simply combines Exchange String body values using '+' as a delimiter
    class StringAggregationStrategy implements AggregationStrategy {

        @Override
        public Exchange aggregate(Exchange oldExchange, Exchange
newExchange) {
            if (oldExchange == null) {
                return newExchange;
            }

            String oldBody = oldExchange.getIn().getBody(String.class);
            String newBody = newExchange.getIn().getBody(String.class);
            oldExchange.getIn().setBody(oldBody + "+" + newBody);
            return oldExchange;
        }
    }
    /**
     * A main() so we can easily run these routing rules in our IDE
     */
    public static void main(String... args) throws Exception {
        CamelContext context = new DefaultCamelContext();
        context.addRoutes(new MyRouteBuilder());
        
        ProducerTemplate template = context.createProducerTemplate();
        context.start();
        
        for (int i = 0; i < 2; i++) {
            template.requestBodyAndHeader("direct:start", "bar", "id", "1");
        }
    }

}

Thanks, Toomas.

--
View this message in context: 
http://camel.465427.n5.nabble.com/Setting-message-header-in-aggregate-and-onCompletion-callback-tp5712636.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Reply via email to