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 )



Monday, December 31, 2012

Camel - Timer 1

Timer Component is used to generate message exchanges when the timer fires - for advanced scheduling options please see Quartz component.

we will explain timer component in a series of posts, and we build our demonstration on simple example : this example print on the output console each time the timer fires.

basic example :


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

@Override
public void configure() throws Exception {
from("timer://timer1?period=1000&delay=100").
                                               setBody().simple("hello world").to("stream:out");
}
});
ctx.start();
Thread.sleep(999999);
ctx.stop();
}

just put previous snippet of code in a main method and include camel-core in the classpath and it will run directly.
in the previous example we create default camel context , start it for time period (9999999) then it will be stopped and we added to this context only one route.

in this route we create timer and when it fires it gives us empty exchange , we populate this body of this exchange with "hello world" text then stream it out to the console.

let's take a closer look to timer uri format : timer:name[?options]
                              from("timer://timer1?period=1000&delay=100").
timer1: represent timer name , to be referenced with.
period and delay , both are properties of this timer , period tell the timer to run each 1 second and delay tell the timer to fire first event after 100 millisecond.

in the next posts we will cover all the options and properties.