Tuesday, January 1, 2013

Camel - Timer 3

in addition to timer options , there is properties that timer component add to the generated exchange.
Timer Component add 5 properties in exchange properties and 1 header in the IN message.

exchange properties :

  • Exchange.TIMER_NAME ( property.CamelTimerName ) : using the following line will print the timer name :
                       from("timer://Demo2Timer?period=1000&repeatCount=1").setBody().simple("time now     is ${property.CamelTimerName}").to("stream:out");
           it will print : Demo2Timer
  • Exchange.TIMER_TIME ( property.CamelTimerTime) : this property have the value of time option if it is used in timer URI 
  • Exchange.TIMER_PERIOD ( property.CamelTimerPeriod ) : this property have the value of period option if used in timer URI
  • Exchange.TIMER_FIRED_TIME ( property.CamelTimerFiredTime ) :  this will hold date object contains the date when the timer fired
  • Exchange.TIMER_COUNTER ( property.CamelTimerCounter ) : this will print the counter of the timer starting from 1 

IN message Header:
  • Exchange.TIMER_FIRED_TIME ( header.CamelTimerFiredTime ) :  this will hold date object contains the date when the timer fired

Camel - Timer 2

Timer Options :

in the previous post "Camel - Timer 1" , we explored two options , delay and period where delay is tell the timer how much to wait until fire first event , and period option tell the timer the interval between each event in millisecond.

there are another two nice options : time and repeatCount.
Say you want the following scenario , at date time 20-12-2012 20:12:3 you want the timer to fire 3 times and each time generate hello world message ( as used in the previous example )


CamelContext ctx = new DefaultCamelContext();
ctx.addRoutes(new RouteBuilder() {

@Override
public void configure() throws Exception {
from("timer://Demo2Timer?period=1000&repeatCount=3&time=2012-12-20 20:12:03").
setBody().simple("Hello World :) ")
.to("stream:out");
}
});
ctx.start();
Thread.sleep(999999);
ctx.stop();


running this example will generate 3 events ( repeatCount=3) that first event fired at our specified date ( time=2012-12-20 20:12:03) and between each one timer period one second ( period = 1000 )