Synchronizing camel route(s) - apache-camel

I have multiple routes deployed in a single camel bundle and what I'm trying to achieve is that once a single route starts execution, the other routes should not be executed until the route that got started is finished with execution.
I understand that it is possible to have the whole camelContext encompassing my routes be made single threaded but I see a drawback here in terms of performance.
Has someone had a similar use case and whats the best way to solve this? Since I'm using OSGi Blueprint DSL, any examples will be welcome.

You can use Camel's Control Bus if you need to control other routes selectively, such as starting and stopping them.
First, you need to prevent the routes you want from executing when your application is started. This is achieved with autoStartup=false in your route definition:
<route id="foo" autoStartup="false">
<from uri="activemq:queue:special"/>
<to uri="file://backup"/>
</route>
Then at the point in your running route where you want to start another route after doing stuff, simply:
<to uri="controlbus:route?routeId=foo&action=start"/>
You can also order the startup (and shutdown) of routes, explained in the Camel documentation here.

Related

stop certain routes instead of completely stopping the whole camel context

I am wondering how can we stop certain routes instead of completely stopping the whole camel context and also is there any other alternative for Thread.sleep(); method for processing.
I would like to run a route once and stop the context when the route is completed. Currently I do the usual Thread.sleep(3000) in the main Java class to leave some time for the route to finish.
I saw some documentation saying camel route supports graceful shutdown but if i don't use Thread.sleep() route shut downs immediately without processing.
the CamelContext has APIs to stop individual routes (startRoute(routeId), stopRoute(routeId), etc) or you can use route policies for more advanced route control
also, to keep your context running, you don't need to use Thread.sleep, just bootstrap the context with Spring or using a Main class...

Transacted camel route with auto-startup set to false

I am in the process of developing a message router which has a bunch of routes that are started and stopped at runtime based some certain conditions.
By default all these routes are configured with auto-starup=false
Now I am trying to add transactional support to these routes and it seems that you cannot define a transacted route and control the its startup behavior at the same time. This is because RouteDefinition.transacted() returns a TransactedDefinition instance which does not have an autoStartup(boolean autoStartup) method.
I am sure I am not the only one to need this kind of functionality and just wondering what is the camel way of addressing such requirements.
Thank you in advance for your inputs
Maybe just set autoStartup first, eg
from("direct:start").autoStartup(false)
.transacted()
.to("mock:result");

Apache Camel - Dynamically changing throttle values

Can anyone please give a sample on how to dynamically change the maxRequestsPerPeriod by using a Throttler processor instance or using a throttle element in Apache Camel ? (Reference - How to change Processor properties during runtime using Camel?)
We cannot use Expression with header because if the header is absent then the Throttler uses the old value. What we need is, in a bean based on some condition we have to update the throttle value so that it will be used until next update. In our case, we cannot use message header for this purpose.
How can we navigate the runtime processors in the route and find the Throttler to change it dynamically? Please help with a sample.
Thanks.
Thanks Claus..We will check jmx mbeans in upcoming Camel 2.16 release.
Now the following solution worked for us with Camel 2.15.2 :
Java DSL:
from("direct:start")
.routeId("throttleroute")
.throttle(ExpressionBuilder.beanExpression("throttleBean","getThrottle"))
.timePeriodMillis(2000)
.to("jms:test.MyQueue")
.beanRef("throttleBean", "receiveData");
Spring DSL:
<route id="throttleroute">
<from uri="direct:start" />
<throttle timePeriodMillis="2000">
<method ref="throttleBean" method="getThrottle" />
<to uri="jms:test.MyQueue" />
</throttle>
<to uri="bean:throttleBean?method=receiveData" />
</route>
Here throttleBean.getThrottle() method will be having the logic to generate and return the required throttle value dynamically.
You can change it using JMX eg the management api.
http://camel.apache.org/maven/current/camel-core/apidocs/org/apache/camel/api/management/mbean/ManagedThrottlerMBean.html
The mbean has JMX attributes to change the values at runtime.
In the upcoming Camel 2.16 release you can easier get hold of the jmx mbeans from java code using
https://github.com/apache/camel/blob/master/camel-core/src/main/java/org/apache/camel/CamelContext.java#L545
Just that you know the id of the mbean. You can assign ids in the routes, so its using a known id, instead of auto generated. Which btw also makes it easier to find the mbean using pure JMX api.

Bundle no longer valid when camel program is being run

I have my camel bundle deployed in service mix and it runs well for some times and now after many runs the same bundle tells the below exception.
org.apache.camel.runtimeexception :java.lang.illegalstateexception: bundlecontext is no longer valid
The same scenario was working fine some runs and suddenly for some consecutive runs it throws the above errors.
Will restart of smx or uninstall of the bundle resolve the issue? this is the third time I am facing this issue and all the time a small restart solved it. Please suggest if this is some other problem.
After My Comment below
I have used in the code i have used like below code.
<choice>
<when>
<simple>some condition</simple>
</when>
<when>
<simple>some other condition</simple>
<stop/>
<when>
</choice>
<!--Still more code and logic exists-->
If I have used here and that is the problem here what should I do to exit when the second condition comes.
Is there any way I can stop the further execution here? that is what is the way to stop the execution for that particular request alone and not stop the route? In this scenario there are other requests which will not be affected by this stop and will work for other requests.
I think in the http://camel.apache.org/intercept.html link it says that to use to stop further execution but will it stop the route any way.
Will this cause the bundle no longer valid scenario. From this there arises a question where what will be the spring config equivalent of CamelContext.stop() ?
After My another Comment below which directs here.
Also we work in an environment where the camel has more customized bundles which ease our development. This has more specifically loading the routes dynamically. Will this be one reason where the routes may not be properly started when we give camelCOntext.Start() etc....?
From the camel route, I cann't tell anything is wrong.
As you just asked another question I just post my answer here:
stop element in the route only works for the exchange, which means the exchange which is routed to this point won't be processed any more. If you want to stop the route or camel context you need call the CamelContext.stop().
If can do it in Spring configuration by using camel-controlbus component.

Start Camel route in suspended state

Is there a way to build Camel routes that starts in suspended mode?
I'm looking so to say "declutch" at start up, then at some stage quickly start processing messages by just calling resumeRoute(routeId)
I could perhaps just create the route and then quickly call suspendRoute(routeId), after the route has been created, but at that stage, it would probably have consumed some messages (for instance in the case of JMS routes or polling consumer routes).
generally, you'd just disable the route by using autostartup(false)...
I assume you are asking though because you need the route started (warmed), but not active. In that case, then you should be able to use a custom route policy and some external variable to get this behavior

Resources