如何在 Apache Camel 中以间隔时间开始一条到另一条路线

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

我想从route1调用route2,它应该执行间隔时间,我创建了下面的代码,代码是否正确,写了多个from方法,有人可以给我建议吗?

//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");

下面是一个最小的完整 Camel 3(版本 3.4.3)示例,如何使用 Timer component and how to call another route (SaveFileRoute). The example is implemented with Camel Main 模块触发路由 (TimerRoute)。

路由使用 From EIP to consume messages from endpoints generated by Timer and Direct components and To EIP 向消费者生成(或“发送”)消息。

首先Timer组件用于自动生成路由调用。这是消息流的起点:

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

在第一个路由的末尾,消息被转发到第二个路由:

.to("direct:savefile")

下一步发生在消费消息的第二个路由中:

from("direct:savefile")

因为第二条路由不会将消息转发到消息流结束的任何地方。

注意在字符串中 "direct:savefile":

  • direct是生成端点的组件类型
  • saveFile是唯一标识端点的名称

完整示例:

// 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);
    }
}

示例运行:

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