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.
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.
No comments:
Post a Comment