Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

The intercept feature in Camel supports intercepting Exchanges while they are on route.

Camel supports two three kinds of interceptors:

  • intercept that intercepts each and every processing step while routing an Exchange in the route.
  • interceptFrom that intercepts incoming Exchange in the route.
  • interceptSendToEndpoint new in Camel 2.0 that intercepts when an Exchange is about to be sent to the given Endpoint.

TODO: Is being reworked in Camel 2.0
Both of these These interceptors supports the following features:

  • Predicate using when to only trigger the interceptor in certain conditions
  • proceed when used with interceptFrom to continue routing from the point of interception when the interceptor is finished. proceed is default and can be omitted.
  • stop when used with interceptFrom will stops routing the Exchange completely. Camel will by default not stop.
  • skip when used with interceptSendToEndpoint will skip sending the Exchange to the original intended endpoint. Camel will by default not skip.

Intercept

Intercept is like a regular interceptor that is applied each each processing step the Exchange undergo while its being routed. You can think of it as a AOP before that is applied at each DSL keyword you have defined in your route.

The classic Hello World example would be:

Code Block

intercept().to("log:hello");

from("jms:queue:order").to("bean:validateOrder").to("bean:processOrder");

What happens is that the Exchange is intercepted before each processing step, that means that it will be intercepted before

  • .to("bean:validateOrder")
  • .to("bean:processOrder")
    So in this sample we intercept the Exchange twice.

TODO: Add support for when predicate
TODO: Add support for stop predicate (proceed is default)

Using from Spring DSL

The same hello world sample in Spring DSL would be:

Code Block
xml
xml

<camelContext ...>
    <intercept>
        <to uri="log:hello"/>
    </intercept>

    <route>
        <from uri="jms:queue:order"/>
        <to uri="bean:validateOrder"/>
        <to uri="bean:handleOrder"/>
    </route>
</camelContext>

InterceptFrom

InterceptFrom is for intercepting any incoming Exchange, in any route (it intercepts all the from DSLs). This allows you to do some custom behavior for received Exchanges. You can provide a specific uri for a given Endpoint then it only applies for that particular route.

...

Wiki Markup
{snippet:id=e1|lang=java|url=camel/trunk/camel-core/src/test/java/org/apache/camel/processor/intercept/InterceptFromUriSimpleLogTest.java}

Using from Spring DSL

Intercept is of course also available using Spring DSL as shown in the sample below:

...

Wiki Markup
{snippet:id=e3|lang=java|url=camel/trunk/camel-core/src/test/java/org/apache/camel/processor/intercept/InterceptSendToEndpointTest.java}

Using from Spring DSL

Intercept endpoint is of course also available using Spring DSL.

...