DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Related

  • Robust Integration Solutions With Apache Camel and Spring Boot
  • Powering LLMs With Apache Camel and LangChain4j
  • Java 21 Is a Major Step for Java: Non-Blocking IO and Upgraded ZGC
  • Microservices With Apache Camel and Quarkus (Part 5)

Trending

  • Optimizing Integration Workflows With Spark Structured Streaming and Cloud Services
  • How To Develop a Truly Performant Mobile Application in 2025: A Case for Android
  • Cosmos DB Disaster Recovery: Multi-Region Write Pitfalls and How to Evade Them
  • Endpoint Security Controls: Designing a Secure Endpoint Architecture, Part 2
  1. DZone
  2. Coding
  3. Frameworks
  4. Implementing Multicasting With Apache Camel

Implementing Multicasting With Apache Camel

Learn to use multicasting in Apache Camel to route a message to multiple endpoints or destinations, sequentially, with parallel processing, or with aggregation.

By 
Jitendra Bafna user avatar
Jitendra Bafna
DZone Core CORE ·
Jul. 07, 17 · Tutorial
Likes (3)
Comment
Save
Tweet
Share
28.9K Views

Join the DZone community and get the full member experience.

Join For Free

1.0 Overview

Multicasting allows you to route the same message to multiple endpoints or destinations and process it in different ways. Multicasting can be done in sequentially in the same thread or using parallel processing.

Image titleTo use multicasting, call multicast() in the Java DSL and then pass the destinations to the to() method. Below, we consume message from direct:start and it is multicast to three different destinations direct:a, direct:b and direct:c.

from("direct:start")
 .multicast()
 .to("direct:a", "direct:b", "direct:c");

It can be achieved by multiple calls to to() methods in sequence.

from("direct:start")
 .multicast()
 .to("direct:a")
 .to("direct:b")
 .to("direct:c");

2.0 Multicasting Sequentially

A message can be sent to multiple endpoints in a single thread sequentially one by one. One drawback of using multicasting this way is that it sends the message sequentially to all the destinations, which can often be a bottleneck in a high traffic organization.

package com.camel.router;

import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.CamelContext;
import org.apache.camel.impl.DefaultCamelContext;
import org.apache.camel.Processor;

public class MulticastRouter {
 public static void main(String[] args) throws Exception {
  CamelContext camelContext = new DefaultCamelContext();
  camelContext.addRoutes(new RouteBuilder() {
   @Override
   public void configure() throws Exception {
    from("file:F:\\Apache_Camel_Test\\IN")
     .multicast()
     .to("file:F:\\Apache_Camel_Test\\OUT", "file:F:\\Apache_Camel_Test\\OUT1", "file:F:\\Apache_Camel_Test\\OUT2");
   }
  });
  System.out.println("Starting Camel Context......");
  camelContext.start();
  Thread.sleep(3000);
  camelContext.stop();
 }
}

3.0 Multicasting With Parallel Processing

A message can be sent to multiple endpoints simultaneously using parallel processing. 

import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.CamelContext;
import org.apache.camel.impl.DefaultCamelContext;
import org.apache.camel.Processor;

public class MulticastRouter {
 public static void main(String[] args) throws Exception {
  CamelContext camelContext = new DefaultCamelContext();
  camelContext.addRoutes(new RouteBuilder() {
   @Override
   public void configure() throws Exception {
    from("file:F:\\Apache_Camel_Test\\IN")
     .multicast().parallelProcessing()
     .to("file:F:\\Apache_Camel_Test\\OUT", "file:F:\\Apache_Camel_Test\\OUT1", "file:F:\\Apache_Camel_Test\\OUT2");
   }
  });
  System.out.println("Starting Camel Context......");
  camelContext.start();
  Thread.sleep(3000);
  camelContext.stop();
 }
}

If you want to add parallel processing to the above example, simply add parallelProcessing(). By default, the thread pool size of 10 is used; you can configure your own size of the pool by adding your own ExecutorService using executorService.

ExecutorService executor = Executors.newFixedThreadPool(16);
from("file:F:\\Apache_Camel_Test\\IN")
 .multicast()
 .stopOnException()
 .parallelProcessing().executorService(executor).
to("file:F:\\Apache_Camel_Test\\OUT", "file:F:\\Apache_Camel_Test\\OUT1");

4.0 Multicasting With Aggregation Strategy

An AggregationStrategy is used for aggregating all reply messages. The default is to only use the latest reply message and discard any earlier replies.

from("direct:start")
 .multicast(new MyAggregationStrategy())
 .parallelProcessing().timeout(500).to("direct:a", "direct:b", "direct:c")
 .end()
 .to("mock:result");

5.0 Conclusion

Multicasting is an Enterprise Integration Pattern (EIP) and a widely used design pattern when it comes to sending the same message to multiple destinations. For example, an order can be sent to a fulfillment system queue and a logging queue when the order is received.

Now you know how to implement multicasting with Apache Camel.

Apache Camel

Opinions expressed by DZone contributors are their own.

Related

  • Robust Integration Solutions With Apache Camel and Spring Boot
  • Powering LLMs With Apache Camel and LangChain4j
  • Java 21 Is a Major Step for Java: Non-Blocking IO and Upgraded ZGC
  • Microservices With Apache Camel and Quarkus (Part 5)

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: