Intercept has been edited by Claus Ibsen (Apr 27, 2009).

(View changes)

Content:

Intercept

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

Camel supports two kinds of interceptors

  • intercept that intercepts incoming Exchange
  • interceptEndpoint new in Camel 2.0 that intercepts when an Exchange is about to be sent to the given Endpoint.

Both of these interceptors supports

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

Intercept

Intercept is for intercepting any incoming Exchange, that is it intercepts the from DSL. This allows you to do some custom behavior for received Exchanges.

So lets start with the logging example. We want to log all the incoming requests so we use intercept to route to the Log component. As proceed is default then the Exchange will continue its route, and thus it will continue to mock:first.

// intercept all incomming routes and log it
intercept().to("log:received");

// and here we have a couple of routes
from("direct:start").to("mock:first").to("seda:bar");

from("seda:bar").to("mock:result");

You can also attach a Predicate to only trigger if certain conditions is meet. For instance in the route below we intercept when a test message is send to us, so we can do some custom processing before we continue routing:

intercept()
    .when(header("usertype").isEqualTo("test"))
    .process(new MyTestServiceProcessor())
    .to("mock:intercepted");

// and here is our route
from("direct:start").to("seda:bar").to("mock:result");

And if we want to filter out certain messages we can use the stop() to instruct Camel to stop continue routing the Exchange:

intercept()
    .when(header("usertype").isEqualTo("test"))
    // here we use stop() to tell Camel to NOT continue routing the message.
    // this let us act as a filter, to drop certain messages.
    .stop();

// and here is our route
from("direct:start").to("seda:bar").to("mock:result");

Using from Spring DSL

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

<camelContext id="camel" xmlns="http://camel.apache.org/schema/spring">
  <intercept>
    <to uri="mock:middle1"/>
  </intercept>
  <route>
    <from uri="direct:start"/>
    <to uri="mock:end"/>
  </route>
</camelContext>

InterceptEndpoint

Available as of Camel 2.0

Intercept endpoint is triggered when an Exchange is being sent to the intercepted endpoint. This allows you to route the Exchange to a Detour or do some custom processing before the Exchange is sent to the original intended destination. You can also skip sending to the intended destination. By default Camel will send to the original intended destination after the intercepted route completes. And as the regular intercept you can also define an when Predicate so we only intercept if the Predicate evaluates to true. This allows you do do a bit of filtering, to only intercept when certain criteria is meet.

Let start with a simple example, where we want to intercept when an Exchange is being sent to mock:foo:

// we intercept by endpoint, that means that whenever an exchange is about to be sent to
// this endpoint, its intercepted and routed with this detour route beforehand
// afterwards its send to the original intended destination. So this is kinda AOP before.
// That means mock:foo will receive the message (Bye World).
interceptEndpoint("mock:foo").to("mock:detour").transform(constant("Bye World"));

from("direct:first")
    .to("mock:bar")
    .to("mock:foo")
    .to("mock:result");

And this time we add the Predicate so its only when the message body is Hello World we intercept.

// we can also attach a predicate to the endpoint interceptor. So in this example the exchange is
// only intercepted if the body is Hello World
interceptEndpoint("mock:foo").when(body().isEqualTo("Hello World")).to("mock:detour").transform(constant("Bye World"));

from("direct:second")
    .to("mock:bar")
    .to("mock:foo")
    .to("mock:result");

And to skip sending to the mock:foo endpoint we use the stop() DSL in the route at the end to instruct Camel to skip sending. The name *stop() is used as the interceptEndpoint builds on top of intercept and thus we inherit the DSL keywords.

// since we use the stop() at the end of the detour route we instruct Camel to skip
// sending the exchange to the original intended destination.
// That means that mock:foo will NOT receive the message, but the message
// is skipped and continued in the original route, so mock:result will receive
// the message.
interceptEndpoint("mock:foo").transform(constant("Bye World")).to("mock:detour").stop();

from("direct:third")
    .to("mock:bar")
    .to("mock:foo")
    .to("mock:result");

Using from Spring DSL

Intercept endpoint is of course also available using Spring DSL.

We start with the first example from above in Spring DSL:

<camelContext id="camel" xmlns="http://camel.apache.org/schema/spring">

    <!-- we intercept by endpoint, that means that whenever an exchange is about to be sent to
         this endpoint, its intercepted and routed with this detour route beforehand
         afterwards its send to the original intended destination. So this is kinda AOP before.
         That means mock:foo will receive the message (Bye World). -->
    <interceptEndpoint uri="mock:foo">
        <to uri="mock:detour"/>
        <transform>
            <constant>Bye World</constant>
        </transform>
    </interceptEndpoint>

    <route>
        <from uri="direct:first"/>
        <to uri="mock:bar"/>
        <to uri="mock:foo"/>
        <to uri="mock:result"/>
    </route>
</camelContext>

And the 2nd. Notice how we can leverage the Simple language for the Predicate:

<camelContext id="camel" xmlns="http://camel.apache.org/schema/spring">

    <interceptEndpoint uri="mock:foo">
        <when><simple>${body} == 'Hello World'</simple></when>
        <to uri="mock:detour"/>
        <transform>
            <constant>Bye World</constant>
        </transform>
    </interceptEndpoint>

    <route>
        <from uri="direct:second"/>
        <to uri="mock:bar"/>
        <to uri="mock:foo"/>
        <to uri="mock:result"/>
    </route>
</camelContext>

And the 3rd with the stop:

<camelContext id="camel" xmlns="http://camel.apache.org/schema/spring">

    <!-- since we use the stop() at the end of the detour route we instruct Camel to skip
         sending the exchange to the original intended destination.
         That means that mock:foo will NOT receive the message, but the message
         is skipped and continued in the original route, so mock:result will receive
         the message. -->
    <interceptEndpoint uri="mock:foo">
        <transform>
            <constant>Bye World</constant>
        </transform>
        <to uri="mock:detour"/>
        <stop/>
    </interceptEndpoint>

    <route>
        <from uri="direct:third"/>
        <to uri="mock:bar"/>
        <to uri="mock:foo"/>
        <to uri="mock:result"/>
    </route>
</camelContext>

Reply via email to