http://www.xeclipse.com/?p=1053

Timer是常用的定时组件,下面简单讲解一下这个组件的基础用法。

作用:定时产生一条Message。

注意:这个组件仅仅能用作consumer,不能用作producer。简单地说,就是只能放在from()里面,不能放在to()里面。

Timer用法

1
timer:name[?options]

非常简单,需要一个名字,以及必要的参数:

Name Default Value Description
time null java.util.Date the first event should be generated. If using the URI, the pattern expected is: yyyy-MM-dd HH:mm:ss or yyyy-MM-dd'T'HH:mm:ss.
pattern null Allows you to specify a custom Date pattern to use for setting the time option using URI syntax.
period 1000 If greater than 0, generate periodic events every period milliseconds.
delay 0 The number of milliseconds to wait before the first event is generated. Should not be used in conjunction with the time option.
fixedRate false Events take place at approximately regular intervals, separated by the specified period.
daemon true Specifies whether or not the thread associated with the timer endpoint runs as a daemon.
repeatCount 0 Camel 2.8: Specifies a maximum limit of number of fires. So if you set it to 1, the timer will only fire once. If you set it to 5, it will only fire five times. A value of zero or negative means fire forever.

Timer产生的Message,带着特定的Properties,这些Properties分别为:

Name Type Description
Exchange.TIMER_NAME String The value of the name option.
Exchange.TIMER_TIME Date The value of the time option.
Exchange.TIMER_PERIOD long The value of the period option.
Exchange.TIMER_FIRED_TIME Date The time when the consumer fired.
Exchange.TIMER_COUNTER Long Camel 2.8: The current fire counter. Starts from 1.

同时Message也有一个Header:

Name Type Description
Exchange.TIMER_FIRED_TIME java.util.Date The time when the consumer fired

Timer示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import org.apache.camel.CamelContext;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.impl.DefaultCamelContext;
  
/**
 * A Camel Router
 */
public class TimerRouteBuilder extends RouteBuilder {
  
    /**
     * A main() so we can easily run these routing rules in our IDE
     */
    public static void main(String... args) throws Exception {
  
        CamelContext camelContext = new DefaultCamelContext();
        camelContext.addRoutes(new TimerRouteBuilder());
        camelContext.start();
  
        Thread.sleep(100000000);
    }
  
    /**
     * Lets configure the Camel routing rules using Java code...
     */
    public void configure() {
        from("timer://myTimer?period=2000").setBody()
                .simple("Current time is ${header.firedTime}").to("log:out");
    }
  
}

每隔2秒钟,产生一条Message,并将消息的内容设为Timer触发的时间:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
[                          main] 12 Feb 10 14:41:16,985 DefaultCamelContext            INFO  Apache Camel 2.8.0 (CamelContext: camel-1) is starting
[                          main] 12 Feb 10 14:41:16,985 DefaultCamelContext            INFO  JMX enabled. Using ManagedManagementStrategy.
[                          main] 12 Feb 10 14:41:17,315 AnnotationTypeConverterLoader  INFO  Found 3 packages with 14 @Converter classes to load
[                          main] 12 Feb 10 14:41:17,355 DefaultTypeConverter           INFO  Loaded 153 core type converters (total 153 type converters)
[                          main] 12 Feb 10 14:41:17,375 AnnotationTypeConverterLoader  INFO  Loaded 4 @Converter classes
[                          main] 12 Feb 10 14:41:17,395 DefaultTypeConverter           INFO  Loaded additional 22 type converters (total 175 type converters) in 0.040 seconds
[                          main] 12 Feb 10 14:41:17,805 DefaultCamelContext            INFO  Route: route1 started and consuming from: Endpoint[timer://myTimer?period=2000]
[                          main] 12 Feb 10 14:41:17,805 DefaultCamelContext            INFO  Total 1 routes, of which 1 is started.
[                          main] 12 Feb 10 14:41:17,805 DefaultCamelContext            INFO  Apache Camel 2.8.0 (CamelContext: camel-1) started in 0.820 seconds
[                       myTimer] 12 Feb 10 14:41:17,846 out                            INFO  Exchange[ExchangePattern:InOnly, BodyType:String, Body:Current time is Fri Feb 10 14:41:17 CST 2012]
[                       myTimer] 12 Feb 10 14:41:19,806 out                            INFO  Exchange[ExchangePattern:InOnly, BodyType:String, Body:Current time is Fri Feb 10 14:41:19 CST 2012]
[                       myTimer] 12 Feb 10 14:41:21,807 out                            INFO  Exchange[ExchangePattern:InOnly, BodyType:String, Body:Current time is Fri Feb 10 14:41:21 CST 2012]
[                       myTimer] 12 Feb 10 14:41:23,808 out                            INFO  Exchange[ExchangePattern:InOnly, BodyType:String, Body:Current time is Fri Feb 10 14:41:23 CST 2012]
[                       myTimer] 12 Feb 10 14:41:25,809 out                            INFO  Exchange[ExchangePattern:InOnly, BodyType:String, Body:Current time is Fri Feb 10 14:41:25 CST 2012]

Camel 组件之 Timer相关推荐

  1. camel apache_Apache Camel 3 –新增功能前10名

    camel apache Apache Camel 3于2019年11月28日星期四发布,也正是美国感恩节这一天. 这不是故意的,但我们可以向社区提供了一个全新的主要版本的Camel,这是我们的极大感 ...

  2. apache.camel_Apache Camel 2.18发布–包含内容

    apache.camel 本周发布了Apache Camel 2.18.0 . 此版本是重要版本,我将在此博客文章中重点介绍. Java 8 Camel 2.18是第一个需要Java 1.8的版本(例 ...

  3. apache camel_Apache Camel请向我解释这些端点选项的含义

    apache camel 在即将发布的Apache Camel 2.15中,我们使Camel更智能. 现在,它可以充当老师,并向您说明其配置方式以及这些选项的含义. Camel可以做的第一课是告诉您如 ...

  4. Apache Camel 3 –新增功能前10名

    Apache Camel 3于2019年11月28日星期四发布,也正是美国感恩节这一天. 这不是故意的,但我们可以向社区提供了一个全新的主要版本的Camel,这是我们的极大感谢–这并不经常发生. 实际 ...

  5. Apache Camel 2.18发布–包含内容

    本周发布了Apache Camel 2.18.0 . 此版本是重要版本,我将在此博客文章中重点介绍. Java 8 Camel 2.18是要求Java 1.8的第一个发行版(例如,容易记住的Camel ...

  6. Apache Camel请向我解释这些端点选项的含义

    在即将发布的Apache Camel 2.15中,我们使Camel更智能. 现在,它可以充当老师,并向您说明其配置方式以及这些选项的含义. Camel可以做的第一课是告诉您如何配置所有端点以及这些选项 ...

  7. Apache Camel是个什么玩意?

    初探Apache Camel Apache Camel 是基于EIP(Enterprise Integration Patterns)的一款开源框架.适用于异构系统间的集成和处理数据. 核心 Came ...

  8. Winform中使用Timer实现滚动字幕效果(附代码下载)

    场景 效果 注: 博客主页: https://blog.csdn.net/badao_liumang_qizhi 关注公众号 霸道的程序猿 获取编程相关电子书.教程推送与免费下载. 实现 新建一个Fo ...

  9. apache.camel_Apache Camel 2.19发布–新增功能

    apache.camel Apache Camel 2.19于2017年5月5日发布,大约在一段时间后,我做了一个小博客,介绍了该版本包含的值得注意的新功能和改进. 这是值得注意的新功能和改进的列表. ...

最新文章

  1. Python-Pandas 如何shuffle(打乱)数据?
  2. python为什么中文要encoding-python 中文编码问题如何解决?
  3. jQuery笔记总结篇
  4. 比传统菜单更为方便的系统菜单模式-Spring.Net.Framwork春天快速开发平台-新型菜单...
  5. Codeforces Round #720 (Div. 2) C. Nastia and a Hidden Permutation 交互
  6. uva 1347——Tour
  7. liberOJ#6006. 「网络流 24 题」试题库 网络流, 输出方案
  8. HTML5 Canvas 裁剪区域
  9. 欧空局2018机器学习系列课程发布:从概念到实践(视频+PPT)
  10. Linux_service cloudera-scm-server start failed
  11. JNI FindClass出错的一种特殊情况
  12. python得语言编程模式_一图看懂编程语言迁移模式:终点站是Python、Go、JS!
  13. WebRTC源码研究(27)TURN协议
  14. Android基础学习整理知识点
  15. 惠普触控板使用指南_手势操作更简便 笔记本触控板使用简介
  16. xz (压缩文件格式)
  17. 深度学习、机器学习领域毕业设计选题方法及建议
  18. vue中浏览器全屏和退出全屏
  19. 源码级剖析了 Naive UI 的 Button 完整过程
  20. 如何修改Oracle VM virtualbox虚拟机的屏幕大小

热门文章

  1. iOS逆向之深入解析如何使用Theos开发插件
  2. Python之将彩色图片批量转化为黑白图片
  3. LeetCode Algorithm 1566. 重复至少 K 次且长度为 M 的模式
  4. 高级指引——概念解释——图形 Shape 及其属性
  5. Netty实战 IM即时通讯系统(十二)构建客户端与服务端pipeline
  6. 很多字段的数据要插入另一张表_一文看懂数据库设计之逻辑设计,值得收藏
  7. 腾讯云服务器 linux 镜像安装项目环境mysql心得
  8. md5可以解密吗_Python训练营作业1:加密解密
  9. LVS(8)——tcpdump查看数据包到底如何传递
  10. Spark(4)——transformation、action、persist