Debugging Camel Headers - apache-camel

I need to work on some Camel routes which contain lot of CBR based on the headers:
<simple>${header.CamelFileName} regex '^.*xml$'</simple>
It would be very helpful to debug the content of the Headers of the Routes: do you recommend any component/processor ?
Thanks!

You can use Log component for that task (http://camel.apache.org/log.html)
<to uri="log:like-to-see-all?level=INFO&showAll=true&multiline=true"/>
This code will help you to see all message headers

I would recommend the DSL Log : http://camel.apache.org/logeip.html which is more confortable and comprehensible to use instead of a Log component (http://camel.apache.org/log.html) to debug headers.
<log message="CamelFileName : ${header.CamelFileName}; you can use simple langage" loggingLevel="FATAL" logName="com.mycompany.MyCoolRoute"/>
From the doc :
Difference between log in the DSL and [Log] component
The log DSL is
much lighter and meant for logging human logs such as Starting to do
... etc. It can only log a message based on the Simple language. On
the other hand Log component is a full fledged component which
involves using endpoints and etc. The Log component is meant for
logging the Message itself and you have many URI options to control
what you would like to be logged.
Hope

Related

Getting Filename without extension : camel

I have header with filename with extension like this
<setHeader>
<simple>Test.txt</simple>
</setHeader>
I need to fetch only filename ie. Test alone in another header. Can anyone help me to achieve this.
Thanks.
You have multiple options. The examples take the full filename from a header and write the filename without extension into another header.
You write a Java Bean to use full Java power (for example Apache Commons FilenameUtils.getBaseName) and call it from your route. See this Camel documentation how to inject header values into your bean methods. In the route you call the bean like this
.setHeader("filename", method(beanReference, "methodName"))
Or you add camel-groovy to your dependencies to get more scripting power than with Camel Simple. Then you can do it directly in the Camel route
setHeader("filename").groovy("request.headers.get('fullFilename')
.take(request.headers.get('fullFilename').lastIndexOf('.'))")

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.

Validate Camel route programmatically

I'm working on a logging solution where Camel routes are defined at runtime with a Java DSL String. I wonder if there's a way to check programmatically some errors such as components not found in the route. The only option I was able to find is catching the org.apache.camel.ResolveEndpointFailedException and dig into the error message. Is there a better way to validate the route?
Just to give an example, it would be good to ascertain if a route syntax is completely wrong or if just a component wasn't found so that I can output a message e.g. "install ftp component".
You can use Fabric8 Camel Maven Plugin (http://fabric8.io/guide/camelMavenPlugin.html) for validating Camel endpoints in the source code.
Look at this article by Claus Ibsen to get more information : https://blog.fabric8.io/cheers-fabric8-camel-maven-plugin-to-validate-camel-endpoints-from-source-code-8768aff76b41#.wcji8hfdg

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.

Resources