Validate Camel route programmatically - apache-camel

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

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('.'))")

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.

Sharing state in camel?

It appears I am running into issues sharing information between routes.
What is the camel pattern for passing around information ?
I looked at exchange properties but that does not stick around between routes I think...
eg:
one file has one has some configutations
i have a route to read this file
and several other routes that will act on based on the configs,
how do I accomplish this ?
I thought of puttin the values in a singleton bean, but that seems kind of ugly...
Exchange properties are preserved across routes inside camel (but there are some limitations and special cases when using splitter/aggregator etc.)
Assign ID's to all sub routes which will act on based on the config. Then get the suitable Route or RouteDefinition from camel context and check whether you can advice or share information to the route according.
ModelCamelContext modelContext;
modelContext.getRouteDefinition(String routeId) or modelContext.getRoute(String routeId)

Debugging Camel Headers

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

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

Resources