Camel 组件之 Timer

来源:互联网 发布:软件总体技术方案 编辑:程序博客网 时间:2024/04/26 19:50

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

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

作用:定时产生一条Message。

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

Timer用法

1
timer:name[?options]

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

NameDefault ValueDescriptiontimenulljava.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分别为:

NameTypeDescriptionExchange.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:

NameTypeDescriptionExchange.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 extendsRouteBuilder {
  
    /**
     * A main() so we can easily run these routing rules in our IDE
     */
    publicstatic void main(String... args) throws Exception {
  
        CamelContext camelContext =new DefaultCamelContext();
        camelContext.addRoutes(newTimerRouteBuilder());
        camelContext.start();
  
        Thread.sleep(100000000);
    }
  
    /**
     * Lets configure the Camel routing rules using Java code...
     */
    publicvoid 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 1014:41:16,985DefaultCamelContext            INFO  Apache Camel 2.8.0(CamelContext: camel-1) is starting
[                          main]12 Feb 1014:41:16,985DefaultCamelContext            INFO  JMX enabled. Using ManagedManagementStrategy.
[                          main]12 Feb 1014:41:17,315AnnotationTypeConverterLoader  INFO  Found 3packages with 14 @Converter classes to load
[                          main]12 Feb 1014:41:17,355DefaultTypeConverter           INFO  Loaded 153core type converters (total 153type converters)
[                          main]12 Feb 1014:41:17,375AnnotationTypeConverterLoader  INFO  Loaded 4@Converter classes
[                          main]12 Feb 1014:41:17,395DefaultTypeConverter           INFO  Loaded additional 22 type converters (total175 type converters) in0.040 seconds
[                          main]12 Feb 1014:41:17,805DefaultCamelContext            INFO  Route: route1 started and consuming from: Endpoint[timer://myTimer?period=2000]
[                          main]12 Feb 1014:41:17,805DefaultCamelContext            INFO  Total 1routes, of which 1is started.
[                          main]12 Feb 1014:41:17,805DefaultCamelContext            INFO  Apache Camel 2.8.0(CamelContext: camel-1) started in0.820 seconds
[                       myTimer]12 Feb 1014:41:17,846out                            INFO  Exchange[ExchangePattern:InOnly, BodyType:String, Body:Current time is Fri Feb10 14:41:17CST 2012]
[                       myTimer]12 Feb 1014:41:19,806out                            INFO  Exchange[ExchangePattern:InOnly, BodyType:String, Body:Current time is Fri Feb10 14:41:19CST 2012]
[                       myTimer]12 Feb 1014:41:21,807out                            INFO  Exchange[ExchangePattern:InOnly, BodyType:String, Body:Current time is Fri Feb10 14:41:21CST 2012]
[                       myTimer]12 Feb 1014:41:23,808out                            INFO  Exchange[ExchangePattern:InOnly, BodyType:String, Body:Current time is Fri Feb10 14:41:23CST 2012]
[                       myTimer]12 Feb 1014:41:25,809out                            INFO  Exchange[ExchangePattern:InOnly, BodyType:String, Body:Current time is Fri Feb10 14:41:25CST 2012]

 

 

原创粉丝点击