Loading
 
What's Next: View All Tutorial →

Multicast aggregationStrategy in Apache camel

In this article you will learn how to implement Multicast aggregationStrategy in Apache camel Multicast aggregationStrategy is used to combine the muticast request to single output. we can combine the output in any object. In this example,...

Published Date: 05/05/2015  Last Modified Date: 05/05/2015    New Post



In this article you will learn how to implement Multicast aggregationStrategy in Apache camel

Multicast aggregationStrategy is used to combine the muticast request to single output. we can combine the output in any object.

In this example, we combine all the output in the map object
 

Program

package com.mycompany.camel;

import java.util.HashMap;
import java.util.Map;
import org.apache.camel.CamelContext;
import org.apache.camel.Exchange;
import org.apache.camel.ExchangePattern;
import org.apache.camel.ProducerTemplate;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.impl.DefaultCamelContext;
import org.apache.camel.processor.aggregate.AggregationStrategy;

public class CamelMulticast {

    public static void main(String args[]) throws Exception {

        CamelContext context = new DefaultCamelContext();
        context.addRoutes(new RouteBuilder() {

            @Override
            public void configure() throws Exception {
                from("direct:A")
                        .multicast().aggregationStrategy(new AggregationStrategy() {

                            @Override
                            public Exchange aggregate(Exchange oldExchange, Exchange newExchange) {
                                Map map = null;
                                //oldExchange will be null at the first route
                                if (oldExchange == null) {
                                    map = new HashMap();
                                } else {
                                    map = (Map) oldExchange.getIn().getBody();
                                }

                                map.put(newExchange.getIn().getHeader("id"), newExchange.getIn().getBody());
                                newExchange.getIn().setBody(map);

                                return newExchange;
                            }
                        })
                        .to("direct:B", "direct:C", "direct:D")
                        .end();

                from("direct:B")
                        .setHeader("id", simple("routeB"))
                        .setBody(simple("This is routeB body response", String.class))
                        .log("${body}").end();
                from("direct:C")
                        .setHeader("id", simple("routeC"))
                        .setBody(simple("This is routeC body response", String.class))
                        .log("${body}").end();
                from("direct:D")
                        .setHeader("id", simple("routeD"))
                        .setBody(simple("This is routeD body response", String.class))
                        .log("${body}").end();
            }
        });
        context.start();
        ProducerTemplate producer = context.createProducerTemplate();

        //To get all three endpoint message in the single map output. The key is based on the header id
        Map map = (Map) producer.sendBody("direct:A", ExchangePattern.InOut, "MessageA");

        System.out.println("Aggregatge Response :" + map);

    }
}


 

.aggregationStrategy( )  - To process the message of each endpoint and to combine it.


To do parallelProcessing after the multicast,  .multicast().parallelProcessing().aggregationStrategy(...

Output:
route2                         INFO  This is routeB body response
route3                         INFO  This is routeC body response
route4                         INFO  This is routeD body response
Aggregatge Response :{routeD=This is routeD body response, routeC=This is routeC body response, routeB=This is routeB body response}







 
Awaiting for Administrator approval





Tags: Basic Multicast EIP

← Back to list


Related Post




×