Apache camel 如何在Apache Camel中使用间隔时间启动一条到另一条的路由

Apache camel 如何在Apache Camel中使用间隔时间启动一条到另一条的路由,apache-camel,spring-camel,Apache Camel,Spring Camel,我想从route1调用route2,它应该执行间隔时间,我创建了下面的代码,是否正确的代码,从方法编写多个,有人能给我建议吗 //Route1 from("timer:repeatcount=1"). .to("direct:route2 "); //Route2 from("direct:route2"). from("timer://simpleTimer?period=1000&quo

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

//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版)示例,说明如何使用组件触发路由(
timerrout
),以及如何调用另一个路由(
SaveFileRoute
)。该示例是用Camel模块实现的

路由使用EIP来使用和组件生成的消息,并使用EIP向消费者生成(或“发送”)消息

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

from(“timer:exampleTimer?fixedRate=true&period=3s”)
在第一条路由的末尾,消息被转发到第二条路由:

.to(“直接:保存文件”)
下一步发生在第二条路由中,消息在该路由中被使用:

from(“直接:保存文件”)
因为第二条路由不会将消息转发到消息流结束的任何地方

请注意,在字符串
“direct:savefile”
中:

  • direct
    是生成端点的组件类型
  • saveFile
    是唯一标识端点的名称
完整示例:

//https://camel.apache.org/components/latest/others/main.html
导入org.apache.camel.builder.RouteBuilder;
导入org.apache.camel.main.main;
公共类应用程序{
公共静态void main(字符串[]args)引发异常{
Main Main=新Main();
main.configure().addRoutesBuilder(
新RouteBuilder(){
public void configure(){
从(“直接:保存文件”)
.routeId(“保存文件路由”)
.log(“路由开始:body=${body}”)
//只是在这里登录
//.至(”file://outbox")
.log(“路由结束:”)
;
}
}
);
main.configure().addRoutesBuilder(
新RouteBuilder(){
public void configure(){
//每3秒触发一次
// https://camel.apache.org/components/latest/timer-component.html
从(“计时器:示例计时器?fixedRate=true&周期=3s”)
.routeId(“TimerRoute”)
.log(“路由开始:”)
// https://camel.apache.org/components/latest/eips/setBody-eip.html
.setBody(常量(“来自计时器的您好!”)
.to(“直接:保存文件”)
.log(“路由结束:”)
;
}
}
);
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$
$mvn compile exec:java
[信息]正在扫描项目。。。
[信息]
[信息]-------------------------------------------------
[信息]建筑004定时器1.0
[信息]------------------------------------[jar]---------------------------------
[信息]
[信息]---maven资源插件:2.6:资源(默认资源)@003定时器---
[信息]使用“UTF-8”编码复制筛选的资源。
[信息]正在复制1个资源
[信息]
[信息]---maven编译器插件:3.8.1:编译(默认编译)@003定时器---
[信息]无需编译-所有类都是最新的
[信息]
[信息]---exec maven插件:3.0.0:java(默认cli)@004定时器---
[App.main()]BaseMain支持信息使用的属性来自:classpath:application.properties;可选=真
[App.main()]DefaultRouteCollector信息未从以下位置找到其他骆驼XML路由:classpath:Camel/*.XML
[App.main()]DefaultRouteCollector信息未从以下位置发现其他骆驼XML休息:classpath:Camel rest/*.XML
[App.main()]AbstractCamelContext信息Apache Camel 3.4.3(Camel-1)正在启动
[App.main()]AbstractCamelContext信息流缓存未被使用。如果使用流,则建议启用流缓存。有关更多详细信息,请参阅http://camel.apache.org/stream-caching.html
[App.main()]InternalRouteStartupManager信息路由:SaveFileRoute从以下位置启动和使用:direct://savefile
[App.main()]InternalRouteStartupManager信息路由:时间路由从以下位置启动和使用:timer://exampleTimer
[App.main()]AbstractCamelContext信息共2条路由,其中2条已启动
[App.main()]AbstractCamelContext信息Apache Camel 3.4.3(Camel-1)在0.031秒内启动
[阅读#0-timer://exampleTimer]TimerRoute信息路由开始:
[阅读#0-timer://exampleTimer]SaveFileRoute INFO路由开始:body=来自计时器的您好!
[阅读#0-timer://exampleTimer]SaveFileRoute信息路由结束:
[阅读#0-timer://exampleTimer]TimerRoute信息路由结束:
[阅读#0-timer://exampleTimer]TimerRoute信息路由开始:
[阅读#0-timer://exampleTimer]SaveFileRoute INFO路由开始:body=来自计时器的您好!
[阅读#0-timer://exampleTimer]SaveFileRoute信息路由结束:
[阅读#0-timer://exampleTimer]TimerRoute信息路由结束:
[阅读#0-timer://exampleTimer]TimerRoute信息路由开始:
[阅读#0-timer://exampleTimer]SaveFileRoute INFO路由开始:body=来自计时器的您好!
[阅读#0-timer://exampleTimer]SaveFileRoute信息路由结束:
[阅读#0-timer://exampleTimer]TimerRoute信息路由结束:
[阅读#0-timer://exampleTimer]TimerRoute信息