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
Newsletter
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

  • The SDLC After AI
  • Should Developers Switch From SQLite to DuckDB?
  • Applying Microservices Lessons to Multi-Agent AI Systems
  • Stop Writing Database Migration Scripts: Declarative Schema Sync for Java

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  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
·
Jul. 07, 17 · Tutorial
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
29.8K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

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

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • 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