Bundle no longer valid when camel program is being run - apache-camel

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.

Related

Missing routeId in direct-vm routes

We have found that when we have 2 routes connected via direct-vm in a transaction, with MDC logging enabled, at the point when the second route returns to the first route after the direct-vm call, the routeId in the MDC is missing (everything else in the MDC is intact).
We are on version 2.24.3 and the routes are defined in JAVA DSL bootstrapped within a springboot application.
Debugging with Camel source code takes us to
this class
https://github.com/apache/camel/blob/camel-2.24.3/camel-core/src/main/java/org/apache/camel/impl/MDCUnitOfWork.java
and we can see that on line 112, that is where the routeId is removed
// not running in route, so clear (should ideally not happen)
MDC.remove(MDC_ROUTE_ID);
But I don't fully understand how route context fits into the picture, how a route context is being created in the first place.
There isn't much documentation I can find about route context except that it can be used as a way to group routes in separate XML files and imported into a camel context, which is irrelevant in our case.
Anyone can shed some lights would be much appreciated!
Thanks in advance.
As the UOW on the direct-vm call is done, the CamelInternalProcessor .UnitOfWorkProcessorAdvice.after(Exchange exchange, UnitOfWork uow) is called;
On line
https://github.com/apache/camel/blob/camel-2.24.x/camel-core/src/main/java/org/apache/camel/processor/CamelInternalProcessor.java#L685
the routeContextStack in the UOW object is cleared at
https://github.com/apache/camel/blob/camel-2.24.x/camel-core/src/main/java/org/apache/camel/impl/DefaultUnitOfWork.java#L179
then on line
https://github.com/apache/camel/blob/camel-2.24.x/camel-core/src/main/java/org/apache/camel/processor/CamelInternalProcessor.java#L690
it's trying to get the route context object again from the stack
https://github.com/apache/camel/blob/camel-2.24.x/camel-core/src/main/java/org/apache/camel/impl/MDCUnitOfWork.java#L106
which would be always null as the stack has been cleared;
hence the routeId is removed from MDC as
https://github.com/apache/camel/blob/camel-2.24.x/camel-core/src/main/java/org/apache/camel/impl/MDCUnitOfWork.java#L112
We are on Camel 2.24.3 but the latest Camel master branch at this point seems to behave the same.

How to capture error at compile level in Apache Camel

I have route like
.bean(OrderService.class, "doSomething")
Now my question is if for any reason any developer misspell method name in route, we would not be able to identify at compile level or we may realize after going to production.
How to handle these scenario?
At a minimum I would recommend a test which ensures that your Camel components are registered with the Camel context, and if they're not, one would expect an exception to be thrown at application startup time.
In essence, you would want a Spring test suite (since Camel will leverage Spring's context for its own context) to ensure that your bean is wired in correctly here.
This cannot be a compile-time error or check since this is specific to how the application context is built, which is done dynamically at runtime.

Synchronizing camel route(s)

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.

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");

Debugging drools when deployed in camel / fuseesb

I am currently building upon the camel-drools example found here: https://github.com/FuseByExample/camel-drools-example
The route is as follows:
<route trace="false" id="testRoute">
<description>Example route that will regularly create a Person with a random age and verify their age</description>
<from uri="timer:testRoute"/>
<bean method="createTestPerson" ref="personHelper"/>
<to uri="drools:node1/ksession1?action=insertBody" id="AgeVerification">
<description>valid 'action' values are:
'execute' that takes a 'Command' object (default)
'insertBody' that inserts the Exchange.in.body, and executes rules
'insertMessage' that inserts the Exchange.in (type org.apache.camel.Message), and executes rules
'insertExchange' that inserts the Exchange (type org.apache.camel.Exchange), and executes rules
</description>
</to>
<choice>
<when id="CanDrink">
<simple>${body.canDrink}</simple>
<log logName="Bar" message="Person ${body.name} can go to the bar"/>
</when>
<otherwise>
<log logName="Home" message="Person ${body.name} is staying home"/>
</otherwise>
</choice>
</route>
I have expanded on this example for my own project and have added more complicated rules and different Facts, which I would now like to debug, however I can't figure out how to get Drools debugging working in the camel / fuse environment.
I would ideally like to see all the various debugging views that the Drools IDE provides such as the agenda view, working memory view, etc (as per http://docs.jboss.org/drools/release/5.5.0.Final/drools-expert-docs/html/ch06.html#d0e8478). I have converted my Eclipse project to a Drools project. I have created a new 'Drools application' debug configuration, but have no idea what to put in the 'main class' section. I don't have my own main class since it's camel that invokes the firing of the rules and inserting facts into the working memory.
I've tried debugging the application as a normal Java application, so I set breakpoints before the drools part of the application is executed. I have followed the drools documentation that says that if you set normal breakpoints and click on the workingMemory variable that the drools 'Working memory' or 'Agenda' view should then populate, however I always see 'The selected working memory is empty', even though I know that it isn't. I've stepped through the code from start to finish clicking on all possible WorkingMemory variables but i still see 'the selected working memory is empty' error.
Has anyone been able to successfully debug drools when deployed using camel? If so, what steps did you take?
Cheers.
I use the KnowledgeRuntimeLogger to help debug my camel/drools app. It creates a log file that I view in the Audit view (you can drag it into the view in eclipse).
KnowledgeRuntimeLogger flogger = KnowledgeRuntimeLoggerFactory.newThreadedFileLogger(ksession,"c:/temp/wmlog");
If you need to do this in spring, you can create it as a bean with the two constructor arguments (or create you own little bean that creates the logger).
hth

Resources