Reputation: 106
Is there a way create a custom Apache Camel Timer as an object defined in java code rather than defining it as a dsl string pattern in the endpoint URI?
In the docs: https://camel.apache.org/components/latest/timer-component.html there is mention of this timer URI query param:
but I haven't found examples of creating that "advanced" custom Timer.
Rather than specify a timer like this:
from("timer://foo?fixedRate=true&period=60000").to("bean:myBean?method=someMethodName");
I would like to specify it as:
from("timer://foo?timer=com.MyCustomTimer").to("bean:myBean?method=someMethodName");
which would be accompanied by:
class MyCustomTimer implements TimerInterfaceICantFind{
public MyCustomTimer(){
setFixedRate(true);
setPeriod(60000);
}
}
I'm wanting to do this so I can specify timer properties dynamically through java setters rather than substituting them into a string URI being constructed.
At the time of this writing, others have asked of Timer URI string syntax, but not of custom timers. Ex:
Apache Camel timer route URI syntax
I saw in the camel source code, it looks like TimerComponent.getTimer() is returning a java.util.Timer
Would that imply that one needs to create the core java class: java.util.Timer - and set properties on it rather than using a camel version of a Timer object for a custom Timer?
Upvotes: 1
Views: 3671
Reputation: 106
The custom Timer that's referrred to in the camel docs is not a camel object but rather a java.util.Timer object. It is lower level and not user friendly to work with manually in camel routes. It's not recommended to use.
You can't instantiate a camel TimerEndpoint object and use setters for its values without also manually specifying the endpoint string uri.
Without providing that uri - camel throws an exception with the message:
"endpointUri is not specified and org.apache.camel.component.timer.TimerEndpoint does not implement createEndpointUri() to create a default value".
If using camel 3+ there is an endpoint dsl option available, EndpointRouteBuilder, that can create a timer endpoint through a builder:
RouteBuilder.addRoutes(camelContext, rb ->
rb.from(timer("myTimer").fixedRate(true).period(500).advanced().synchronous(false))
.delay(simple("${random(250,1000)}"))
.process(this::newOrder)
.to(direct("cafe")));
Upvotes: 1
Reputation: 45
If you insist to have the timer generated dynamically, perhaps you can take a look at Camel EndpointDSL which allows you to construct endpoints dynamically. Here is for timer and an example to use it.
Upvotes: -1