Search code examples
apache-camelspring-camel

How to start one route to another with interval time in Apache Camel


I wanna call route2 from route1 and it should execute interval time, I created below code, is it correct code, write multiple from methods, can anyone please give me suggestions?

//Route1
  from("timer:repeatcount=1").
    .to("direct:route2 ");
//Route2    
    from("direct:route2").
    from("timer://simpleTimer?period=1000")
    .setBody(simple("Hello from timer at ${header.firedTime}"))
    .to("stream:out");

Solution

  • Below you'll find a minimal complete Camel 3 (version 3.4.3) example how to trigger a route (TimerRoute) with Timer component and how to call another route (SaveFileRoute). The example is implemented with Camel Main module.

    The routes use From EIP to consume messages from endpoints generated by Timer and Direct components and To EIP to produce (or "to send") messages to the consumers.

    First the Timer component is used to automatically generate route invocations. This is the starting point of the message flow:

    from("timer:exampleTimer?fixedRate=true&period=3s")
    

    In the end of the first route the message is forwarded to the second route:

    .to("direct:savefile")
    

    Next step takes place in the second route where the message is consumed:

    from("direct:savefile")
    

    Because the second route doesn't forward the message anywhere the message flow ends there.

    Note that in the string "direct:savefile":

    • direct is the type of component generating the endpoint
    • saveFile is a name that uniquely identifies the endpoint

    The complete example:

    // https://camel.apache.org/components/latest/others/main.html
    import org.apache.camel.builder.RouteBuilder;
    import org.apache.camel.main.Main;
    
    public class App {
        public static void main(String[] args) throws Exception {
            Main main = new Main();
            main.configure().addRoutesBuilder(
                new RouteBuilder() {
                    public void configure() {
                        from("direct:savefile")
                        .routeId("SaveFileRoute")
                        .log("ROUTE START: body = ${body}")
                        // just logging here
                        // .to("file://outbox")
                        .log("ROUTE END:")
                        ;
                    }
                }
            );
            main.configure().addRoutesBuilder(
                new RouteBuilder() {
                    public void configure() {
                        // triggers on every 3 seconds
                        // https://camel.apache.org/components/latest/timer-component.html
                        from("timer:exampleTimer?fixedRate=true&period=3s")
                        .routeId("TimerRoute")
                        .log("ROUTE START:")
                        // https://camel.apache.org/components/latest/eips/setBody-eip.html
                        .setBody(constant("HELLO FROM TIMER!"))
                        .to("direct:savefile")
                        .log("ROUTE END:")
                        ;
                    }
                }
            );
            main.run(args);
        }
    }
    

    Example run:

    $ mvn compile exec:java
    [INFO] Scanning for projects...
    [INFO] 
    [INFO] -----------------------< net.jani-hur:004-timer >-----------------------
    [INFO] Building 004-timer 1.0
    [INFO] --------------------------------[ jar ]---------------------------------
    [INFO] 
    [INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ 003-timer ---
    [INFO] Using 'UTF-8' encoding to copy filtered resources.
    [INFO] Copying 1 resource
    [INFO] 
    [INFO] --- maven-compiler-plugin:3.8.1:compile (default-compile) @ 003-timer ---
    [INFO] Nothing to compile - all classes are up to date
    [INFO] 
    [INFO] --- exec-maven-plugin:3.0.0:java (default-cli) @ 004-timer ---
    [                    App.main()] BaseMainSupport                INFO  Using properties from: classpath:application.properties;optional=true
    [                    App.main()] DefaultRoutesCollector         INFO  No additional Camel XML routes discovered from: classpath:camel/*.xml
    [                    App.main()] DefaultRoutesCollector         INFO  No additional Camel XML rests discovered from: classpath:camel-rest/*.xml
    [                    App.main()] AbstractCamelContext           INFO  Apache Camel 3.4.3 (camel-1) is starting
    [                    App.main()] AbstractCamelContext           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
    [                    App.main()] InternalRouteStartupManager    INFO  Route: SaveFileRoute started and consuming from: direct://savefile
    [                    App.main()] InternalRouteStartupManager    INFO  Route: TimerRoute started and consuming from: timer://exampleTimer
    [                    App.main()] AbstractCamelContext           INFO  Total 2 routes, of which 2 are started
    [                    App.main()] AbstractCamelContext           INFO  Apache Camel 3.4.3 (camel-1) started in 0.031 seconds
    [read #0 - timer://exampleTimer] TimerRoute                     INFO  ROUTE START:
    [read #0 - timer://exampleTimer] SaveFileRoute                  INFO  ROUTE START: body = HELLO FROM TIMER!
    [read #0 - timer://exampleTimer] SaveFileRoute                  INFO  ROUTE END:
    [read #0 - timer://exampleTimer] TimerRoute                     INFO  ROUTE END:
    [read #0 - timer://exampleTimer] TimerRoute                     INFO  ROUTE START:
    [read #0 - timer://exampleTimer] SaveFileRoute                  INFO  ROUTE START: body = HELLO FROM TIMER!
    [read #0 - timer://exampleTimer] SaveFileRoute                  INFO  ROUTE END:
    [read #0 - timer://exampleTimer] TimerRoute                     INFO  ROUTE END:
    [read #0 - timer://exampleTimer] TimerRoute                     INFO  ROUTE START:
    [read #0 - timer://exampleTimer] SaveFileRoute                  INFO  ROUTE START: body = HELLO FROM TIMER!
    [read #0 - timer://exampleTimer] SaveFileRoute                  INFO  ROUTE END:
    [read #0 - timer://exampleTimer] TimerRoute                     INFO  ROUTE END:
    [read #0 - timer://exampleTimer] TimerRoute                     INFO  ROUTE START:
    [read #0 - timer://exampleTimer] SaveFileRoute                  INFO  ROUTE START: body = HELLO FROM TIMER!
    [read #0 - timer://exampleTimer] SaveFileRoute                  INFO  ROUTE END:
    [read #0 - timer://exampleTimer] TimerRoute                     INFO  ROUTE END:
    [read #0 - timer://exampleTimer] TimerRoute                     INFO  ROUTE START:
    [read #0 - timer://exampleTimer] SaveFileRoute                  INFO  ROUTE START: body = HELLO FROM TIMER!
    [read #0 - timer://exampleTimer] SaveFileRoute                  INFO  ROUTE END:
    [read #0 - timer://exampleTimer] TimerRoute                     INFO  ROUTE END:
    [read #0 - timer://exampleTimer] TimerRoute                     INFO  ROUTE START:
    [read #0 - timer://exampleTimer] SaveFileRoute                  INFO  ROUTE START: body = HELLO FROM TIMER!
    [read #0 - timer://exampleTimer] SaveFileRoute                  INFO  ROUTE END:
    [read #0 - timer://exampleTimer] TimerRoute                     INFO  ROUTE END:
    ^C$