Camel 组件之 Timer

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

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

作用:定时产生一条Message。

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

Timer用法

1
timer:name[?options]

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

NameDefault ValueDescription
timenulljava.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.
patternnullAllows you to specify a custom Date pattern to use for setting the time option using URI syntax.
period1000If greater than 0, generate periodic events every period milliseconds.
delay0The number of milliseconds to wait before the first event is generated. Should not be used in conjunction with the time option.
fixedRatefalseEvents take place at approximately regular intervals, separated by the specified period.
daemontrueSpecifies whether or not the thread associated with the timer endpoint runs as a daemon.
repeatCount0Camel 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分别为:

NameTypeDescription
Exchange.TIMER_NAMEStringThe value of the name option.
Exchange.TIMER_TIMEDateThe value of the time option.
Exchange.TIMER_PERIODlongThe value of the period option.
Exchange.TIMER_FIRED_TIMEDateThe time when the consumer fired.
Exchange.TIMER_COUNTERLongCamel 2.8: The current fire counter. Starts from 1.

同时Message也有一个Header:

NameTypeDescription
Exchange.TIMER_FIRED_TIMEjava.util.DateThe 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 ]

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值