Skip to content

Instantly share code, notes, and snippets.

@heiko-braun
Created December 4, 2023 10:45
Show Gist options
  • Save heiko-braun/1e0361839704263ffccceb35092d1964 to your computer and use it in GitHub Desktop.
Save heiko-braun/1e0361839704263ffccceb35092d1964 to your computer and use it in GitHub Desktop.

Keywords: Apache Camel route scheduler | multiple threads | interval | execute

Messages

user: I created new apache camel route scheduler and I want to be invoked by multiple threads on an interval. want to execute x number of threads. I am unsure how to go about it

assistant: To invoke a route by multiple threads on an interval in Apache Camel, you can use the ScheduledPollConsumer to achieve this. The ScheduledPollConsumer allows you to trigger the route execution at a predefined interval and run it in multiple threads. You can control the number of threads, interval, initial delay, etc.

Here's an example of how to create a route that is triggered by the ScheduledPollConsumer:

import org.apache.camel.builder.RouteBuilder;

public class MyRouteBuilder extends RouteBuilder {
    @Override
    public void configure() throws Exception {
        from("direct:start")
            .threads(5) // Specify the number of threads
            .pollEnrich().simple("timer://foo?fixedRate=true&period=5000") // Trigger the route at an interval of 5 seconds
            .to("log:output");
    }
}

In this example, .threads(5) is used to specify the number of threads to be used for running the route. The timer://foo?fixedRate=true&period=5000 triggers the route every 5 seconds. The pollEnrich() method is then used to enrich the message exchange with the content from the timer endpoint.

To use this route, you should add the necessary Camel dependencies to your Maven project, such as the following in your pom.xml:

<dependency>
    <groupId>org.apache.camel</groupId>
    <artifactId>camel-core</artifactId>
    <version>x.x.x</version>
</dependency>
<dependency>
    <groupId>org.apache.camel</groupId>
    <artifactId>camel-timer</artifactId>
    <version>x.x.x</version>
</dependency>

Replace x.x.x with the version of Camel you are using.

I hope this helps! Let me know if you need further assistance.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment