Camel 组件之 Timer
                                                            生活随笔
收集整理的這篇文章主要介紹了
                                Camel 组件之 Timer
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.                        
                                http://www.xeclipse.com/?p=1053
Timer是常用的定時(shí)組件,下面簡單講解一下這個(gè)組件的基礎(chǔ)用法。
作用:定時(shí)產(chǎn)生一條Message。
注意:這個(gè)組件僅僅能用作consumer,不能用作producer。簡單地說,就是只能放在from()里面,不能放在to()里面。
Timer用法
| 1 | timer:name[?options] | 
非常簡單,需要一個(gè)名字,以及必要的參數(shù):
| 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?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產(chǎn)生的Message,帶著特定的Properties,這些Properties分別為:
| 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. | 
同時(shí)Message也有一個(gè)Header:
| 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秒鐘,產(chǎn)生一條Message,并將消息的內(nèi)容設(shè)為Timer觸發(fā)的時(shí)間:
| 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] | 
?
?
總結(jié)
以上是生活随笔為你收集整理的Camel 组件之 Timer的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: Camel中的几个重要概念之Routes
- 下一篇: Maven实战——常用Maven插件介绍
