http://www.xeclipse.com/?p=1053
Timer是常用的定时组件,下面简单讲解一下这个组件的基础用法。
作用:定时产生一条Message。
注意:这个组件仅仅能用作consumer,不能用作producer。简单地说,就是只能放在from()里面,不能放在to()里面。
Timer用法
timer:name[?options]
非常简单,需要一个名字,以及必要的参数:
Name | Default Value | Description |
time | null | A 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 |
period | 1000 | If greater than 0, generate periodic events every period |
delay | 0 | The number of milliseconds to wait before the first event is generated. Should not be used in conjunction with the time |
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 |
Exchange.TIMER_TIME | Date | The value of the time |
Exchange.TIMER_PERIOD | long | The value of the period |
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示例
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触发的时间:
[ 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 ]