How to capture error at compile level in Apache Camel - 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.

Related

How can I include common behavior in several Apache Camel routes?

I am using Camel 2.19.2 in a Spring Boot 1.5.8 application. If I want, for example, to have several of my routes to be "status aware", how might I achieve this? What I mean by "status aware" is that the route will start, notify a component that the workflow has begun, then it will conduct route-specific logic, and when that is complete, it will notify a component that the workflow has completed. I want this to happen automatically, if possible, and without having to call the specific logic in each of the route builders that I want to use this capability.
Here is a code example like what I mean:
public class FooRouteBuilder extends StatusAwareRouteBuilder {
#Override
public void configure() {
// Here I want to have this route know how to notify something
// that this processing has begun, but I do not want to have
// to explicitly call a processor to make it happen, but it
// should know what to do by virtue of extending a custom
// route builder, if appropriate, or by some other/better
// mechanism
// Now conduct any route-specific logic
from("vm:myAction")
.process("myProcessor");
// Now handle the status notification that this is finished...
// Here I want to have this route know how to notify something
// that this processing has finished
}
}
Conceptually, this is almost like AOP, so I would like to be able to define this behavior in one place and include it in some number of routes that need to use this behavior. Is there a way that I can accomplish this? I saw that there is adviceWith for testing, but I need this for regular operation. Thanks in advance.
I think that RoutePolicy and RoutePolicyFactory can be the answer, i.e. you can have a callback invoked when the route or exchnage start/stop.
For more info see http://camel.apache.org/routepolicy.html
Perhaps Camel interceptors can help you. These are typically small generic routes that are applied to all or most of your routes.
For example to do a security check in every route. With an interceptor you write it once and it is applied to all routes, even new ones that are added.
There are three flavours.
intercept intercepts each and every processing step while routing
an Exchange in the route.
interceptFrom intercepts every incoming
Exchange in the route (start of processing)
interceptSendToEndpoint intercepts when an
Exchange is about to be sent to an endpoint.
The interceptors can be configured to "fire" only for specific endpoint types or by Camel predicate on other specific conditions.
There is also the onCompletion feature to do a similar thing at route completion. Either on successful completion on failure completion or both of them (default).

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

Stub camel route bean reference for integration test

I have a camel route which has a bean pointing to a amazon wrapper class. That class is making an amazon call.
Ideally I would like to stub out the amazon call for the integration test.
However all I can think of is to put a split on the route to check for a particular flag. If the flag is positive then the code calls thr stub and if its negative then it calls actual amazon.
I really do not like this option as it is mixing production code with test code. Or is it ok in this scenario.
You can use advices for this scenario..
If you need help with it, please Post an example Route that demonstrates your Problem.

Disable Dynamic Routing capability in Camel

With Camel, it is possible to add routes dynamically to the context. And it appears the context is always passed as part of the exchange.
Is there a way to prevent applications from adding routes at runtime? I looked at Shiro security but did not seem to find something along those lines.
the only thing I can think of is to wrap interactions with these applications using POJO Bean Binding which only passes the body of the Exhchange around and limits access to the Exchange directly...
see http://camel.apache.org/bean-binding.html
Maybe you can extend DefaultContext and add security rule in that.

Resources