Loading
 
What's Next: View All Tutorial →

Multicast in Apache Camel

  Multicasts messages to all its child outputs; so that each processor and destination gets a copy of the original message to avoid the processors interfering with each other.        package com.mycompany.camel; imp...

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



 

Multicasts messages to all its child outputs; so that each processor and destination gets a copy of the original message to avoid the processors interfering with each other.     

 
package com.mycompany.camel;

import org.apache.camel.CamelContext;
import org.apache.camel.ExchangePattern;
import org.apache.camel.ProducerTemplate;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.impl.DefaultCamelContext;

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()
                        .to("direct:B", "direct:C", "direct:D")
                        .end();

                from("direct:B").setBody(simple("${body} - MessageB", String.class))
                        .log("${body}").end();
                from("direct:C").setBody(simple("${body} - MessageC", String.class))
                        .log("${body}").end();
                from("direct:D").setBody(simple("${body} - MessageD", String.class))
                        .log("${body}").end();
            }
        });
        context.start();
        ProducerTemplate producer = context.createProducerTemplate();

        Object res = producer.sendBody("direct:A", ExchangePattern.InOut, "MessageA");
        System.out.println(res);

    }
}

Output:
DefaultCamelContext            INFO  Apache Camel 2.15.1 (CamelContext: camel-1) is starting
ManagedManagementStrategy      INFO  JMX is enabled
DefaultTypeConverter           INFO  Loaded 197 type converters
DefaultCamelContext            INFO  AllowUseOriginalMessage is enabled. If access to the original message is not needed, then its recommended to turn this option off as it may improve performance.
DefaultCamelContext            INFO  StreamCaching is not in use. If using streams then its recommended to enable stream caching. See more details at http://camel.apache.org/stream-caching.html
DefaultCamelContext            INFO  Route: route1 started and consuming from: Endpoint[direct://A]
DefaultCamelContext            INFO  Route: route2 started and consuming from: Endpoint[direct://B]
DefaultCamelContext            INFO  Route: route3 started and consuming from: Endpoint[direct://C]
DefaultCamelContext            INFO  Route: route4 started and consuming from: Endpoint[direct://D]
DefaultCamelContext            INFO  Total 4 routes, of which 4 is started.
DefaultCamelContext            INFO  Apache Camel 2.15.1 (CamelContext: camel-1) started in 1.123 seconds
route2                         INFO  MessageA - MessageB
route3                         INFO  MessageA - MessageC
route4                         INFO  MessageA - MessageD
MessageA - MessageD


Points to remember

The response will be the last router. As each one take a separate copy of it.




If we want to combine the out of all three router, we can go for AggregationStrategy

Also, if we want to run all the router at same time, we can go for parallelProcessing
 
Awaiting for Administrator approval





Tags: Basic

← Back to list


Related Post




×