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

Related

  • Building an Image Classification Pipeline With Apache Camel and Deep Java Library (DJL)
  • Building a Sentiment Analysis Pipeline With Apache Camel and Deep Java Library (DJL)
  • Robust Integration Solutions With Apache Camel and Spring Boot
  • Powering LLMs With Apache Camel and LangChain4j

Trending

  • Data Contracts as the "Circuit Breaker" for Model Reliability
  • Building a DevOps-Ready Internal Developer Platform: A Hands-On Guide to Golden Paths, Self-Service, and Automated Delivery Pipelines
  • From 24 Hours to 2 Hours: How We Fixed a Broken BI System With Apache Airflow
  • Architecting Zero-Trust AI Agents: How to Handle Data Safely
  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
·
Jul. 07, 17 · Tutorial
Likes (3)
Comment
Save
Tweet
Share
29.6K 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

  • Building an Image Classification Pipeline With Apache Camel and Deep Java Library (DJL)
  • Building a Sentiment Analysis Pipeline With Apache Camel and Deep Java Library (DJL)
  • Robust Integration Solutions With Apache Camel and Spring Boot
  • Powering LLMs With Apache Camel and LangChain4j

Partner Resources

×

Comments

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

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

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 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook