Search code examples
apache-camelspring-camel

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.


Solution

  • 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