Reconfiguring Apache Camel route parameters at runtime - apache-camel

I have aggregation configured in my code:
.aggregate(new BodyAggregationStrategy())
.constant(true)
.completionSize(1000) // Static value
.completionTimeout(300) // Static value
After I have started the Apache Camel context is it possible to change various parameters like completionSize and completionTimeout values?
When context is running a lot of data are transfering throught it and I want to increase some parameters like queue size and so on.

What you could do, is via the CameContext
stop the route
remove the route
add the route with new parameters
start the route

Have you tried to connect via JMX and see if those parameters can be configured?

Related

restarting a route initialized with File component does not poll the existing files again

Thanks to JMX (java console), I try to restart a route with a file component consumer endpoint.
from("file:<some dir>?noop=true")
I am using the wiretap pattern to record the intermediate data transformation through other files endpoint.
On first start of the camel application, everything is fine, and all the files already present in the input directory are polled and processed.
But when I try to restart the route thanks to jmx, nothing happens.
I try to manually removed .camel directory - created by I guess the default FileIdempotentRepository - before restarting the route, in vain.
I also tried to change the kind of IdempotentRepository with a MemoryIdempotentRepository :
from("file:<somedir>?noop=true").idempotentConsumer(header("CamelFileName"), MemoryIdempotentRepository.memoryIdempotentRepository(1000))
Even if I trigger the clear() operation of this MemoryIdempotentRepository before restarting the route in java console, nothing is polled from the input directory after restarting.
If I add a file, it works. Everything behaves like if there is a persistent history of the files already polled once.
I wonder if the use of the option "noop=true" creates an unmanaged idempotent repository I cannot control with jmx.
If true, the file is not moved or deleted in any way. This option is
good for readonly data, or for ETL type requirements. If noop=true,
Camel will set idempotent=true as well, to avoid consuming the same
files over and over again.
Any idea ?
(i am using camel-core 2.21)
I found the solution to my issue.
I made a bad use of idempotentConsumer; I needed to initialize the endpoint idempotent consumer inside the endpoint URI parameters list.
First, create an entry in a bean registry:
registry.put("myIdempotentRepository", MemoryIdempotentRepository.memoryIdempotentRepository(1000));
Then, refer to this idempotentRepository in the endpoint:
from("file:<somedire>noop=true&initialDelay=10&delay=1000&idempotentRepository=#myIdempotentRepository")
By doing this, GenericFileEndPoint:
will not create a default idempotentRepository
will add the idempotentRepository given in options of the endpoint to the services of the camel context. This means that it will be possible to manage it thanks to JMX
I think it would be useful to be allowed to manage the default idempotent repository in the FileEndPoint class of camel-core.

pollEnrich with dynamic URI and its number of executions

I want to listen on ActiveMQ topic based on the hostname of system and some other logic. I planned to use pollEnrich for it so I evaluate my logic and provide topic name in pollEnrich but as per document:
pollEnrich or enrich does not access any data from the current Exchange which means when polling it cannot use any of the existing headers you may have set on the Exchange. For example you cannot set a filename in the Exchange.FILE_NAME header and use pollEnrich to consume only that file. For that you must set the filename in the endpoint URI.
How i can figure out this
from("timer://ipc?repeatCount=1")
.. some logic..
.setHeader("topic_no",simple("{{env:HOSTNAME}}"))
.pollEnrich("mqtt:foo?host=tcp://0.0.0.0:1883&subscribeTopicNames=${header.topic_no}/status&clientId=ipc")
.to("log:my?showAll=true&multiline=true");
Please don't suggest to use hostname directly in URI. As I highlighted I have to compute other logic too.
What other option or way I can use?
Will pollEnrich kept listening on topic or it will listen once and end the route?
Update1:
I figured out we can use simple expression with for dynamic URI, But one issue with pollEnrich it only pick one message how i can make sure it kept on listening as consumer? I want that before pollEnrich part get execute once and TopicListener kept listening till application is up.
Will pollEnrich kept listening on topic or it will listen once and end the route?
Same as the fact you have figure out, Camel pollEnrich component will listen on topic and consume at most one message per call.
What other option or way I can use?
Repeat pollEnrich by loop
Create new route at run-time by routeBuilder
Option 1 is naive, but simple in concept. pollEnrich will do once and loop will repeat it. However, this method need to handle more scenario than you might expected.
Option 2 is a better approach. You create a route at run-time and the consumer endpoint URI is pass by variable. That said, you can create the consumer route dynamically after your computation logic.
Example for routeBuilder

Removing camel route is not removing tibco queue receivers count

We have a stand alone application where we should be able to stand up JMS listeners using database configuration dynamically. If there is any change in the concurrentConsumers count in the database, then we need to update the listeners accordingly. We are using camel 2.16.2 and Tibco 6.3.0. Precisely, the below are our requirements.
Ability to add and remove JMS listeners dynamically.
To achieve this, we are creating our OwnRouteBuilder (which extends camel RouteBuilder) and adding it using camelContext.addRoutes(RouteBuilder) dynamically which is working as expected.
Ability to increase or decrease JMS listeners dynamically.
To achieve this, initially we tried to stop the route, update concurrentConsumers count, and then start route. It is working fine only when we increase the concurrentConsumers count. But its not working if we decrease( its not changing the concurrentConsumers count).We can still see the same receivers count from tibco console.
As an alternate solution , we tried with camelContext.stopRoute(routeId) and then camelContext.removeRoute(routeId) so that we can add a fresh route with updated concurrentConsumers count later. In this case also, though it is stopping the routes from consuming messages, it is not changing the receivers count from tibco console.
I tried with camelContext.removeEndpoints(endpointUri), camelContext.shutdownRoute(routeId), camelContext.removeRouteDefinition(routedefinition) methods.
But all these methods only stopping the routes from consuming messages, but not clearing those receivers count from tibco console.
The receivers count from tibco console is becoming zero only when we terminate the application.
Is there any way that we can make it from camel so that those receivers count from tibco console becomes zero.
You can change the concurrentConsumer counts dynamically at runtime using JMX to set the values on the JMX attribute on the endpoint. Then the route dont have to be stopped/removed/started etc.

camel change route policy at runtime via jmx

is it possible to change at runtime the route policy? for instance, if i have the code below
CronScheduledRoutePolicy startPolicy = new CronScheduledRoutePolicy();
startPolicy.setRouteStartTime("* 0 * * * ?");
startPolicy.setRouteStopTime("* 30 * * * ?");
from("direct:foo").routeId("myRoute").routePolicy(startPolicy).autoStartup(false).to("does://not-matter");
I would like to change the cron parameters during the camel execution. In JConsole I can just access to the getRoutePolicyList which returns
CronScheduledRoutePolicy(0x6dc7efb5)
Is it possible in some way access to the startPolicy object and re-instantiate it with a new value? Have I extend the mbean class of camel with some getter and setters?
No not out of the box. But yeah it would be a nice new feature to register the CronScheduledRoutePolicy as a JMX MBean so people can adjust it at runtime with JMX.
I have logged a ticket: https://issues.apache.org/jira/browse/CAMEL-6334
What you can do is to stop the route. And then adjust the startPolicy settings, and then start the route again.
There is JMX operations for starting and stopping routes. What you may need is to expose some JMX operations to adjust the cron policy.
I managed to do this using hawt.io. But for this to work, you need to upgrade to Camel version 2.13.0.
Using hawt.io, you can change cron expressions at runtime in a very user-friendly way.

How can one get local host address in Apache Camel

In a route (that exposes a REST service) i need to get the local host address/domain (so as I can build links to be included in a response body)
Is there a way to get the local host address from within camel ?
PS : running camel inside servicemix
there isn't a Camel specific way (that I know of), I'd just use an external property file to configure environment specific settings like this...you can then use camel properties to pull this in, etc.
A good idea when posting questions is to provide more details.
Which camel component do you use for REST. And which version of SMX/Camel do you use etc.
As you question assume that the rest component in Camel will expose a hostname information.
This questions is not Camel specific per see, as you can use the regular Java API to get the hostname.

Resources