Debugging drools when deployed in camel / fuseesb - apache-camel

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

Related

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.

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.

MVC 5 IsInRole Usage on Razor Views: Cannot connect to Database

I'm having issues using the new identity system in MVC 5, my goal is to make use of the User.IsinRole("RoleName") on Views. For example:
#if(User.IsInRole("Administrator"))
{
<li>#Html.ActionLink("Admin", "Index", "Admin")</li>
}
This is placed in the main layout page which is hit when the application launches. On doing this, i'm getting the following error:
"An exception of type 'System.Web.HttpException' occurred in System.Web.dll but was not handled in user code
Additional information: Unable to connect to SQL Server database."
I have searched high and low for a solution to this, the common solution is to either include "[InitializeSimpleMembership]" at the top of the controller or initialise the database connection manually in application start. (With WebSecurity.InitializeDatabaseConnection). Both of these methods do not seem to be recognised by MVC 5.
I have also tried working around this by creating a bunch of messy code any time i return a view to populate the ViewBag with an IsAdmin boolean by using the Aspnet.Identity.UserManager to determine roles. Whilst this works it's not the way i feel i should be doing things.
It might be worth noting but i don't experience these issues accessing User.IsInRole on the backend, this definitely seems to be an initialization problem.
I was having the same problem, however Stunt's answer wasn't working for me. Tried the answer to this question and that solved the issue.
For the lazy you can try adding this to your web.config file:
<system.webServer>
<modules>
<remove name="RoleManager" />
</modules>
</system.webServer>
I managed to get around the problem by removing the following line from my Web Config:
<roleManager enabled="true" />
This was found when looking after comparing line for line on the following example code:
https://github.com/rustd/AspnetIdentitySample/tree/master/AspnetIdentitySample
Create database on start up of the application. Add the following in the Global.ascx with your dbcontext.
using (FooContext db = new FooContext())
{
db.Database.CreateIfNotExists();
}

Resources