3

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.

Steve Storck
  • 793
  • 6
  • 25

2 Answers2

2

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

Luca Burgazzoli
  • 1,216
  • 7
  • 9
2

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).

burki
  • 6,741
  • 1
  • 15
  • 31
  • This solution intrigues me, but I would only want the intercept to happen for designated routes, and only upon start of a route and then again at the completion of the route (regardless of normal completion or exception). I could not discern if this was possible from that documentation. – Steve Storck May 14 '18 at 10:19
  • I have extended my answer a bit. Start of a route: `interceptFrom`; route completion: `onCompletion`. Only certain routes: use a Predicate to define the routes the interceptor is applied to. – burki May 14 '18 at 11:12