You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 10 Next »

Intercept

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

Camel supports two kinds of interceptors

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

Both of these interceptors supports

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

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.

So lets start with the logging example. We want to log all the incoming requests so we use interceptFrom 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.

Error formatting macro: snippet: java.lang.NullPointerException

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:

Error formatting macro: snippet: java.lang.NullPointerException

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

Error formatting macro: snippet: java.lang.NullPointerException

And if want to only apply a specific endpoint, as the seda:bar endpoint in the sample below, we can do it like this:

Error formatting macro: snippet: java.lang.NullPointerException

Using from Spring DSL

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

Error formatting macro: snippet: java.lang.NullPointerException

InterceptSendToEndpoint

Available as of Camel 2.0

Intercept send to 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:

Error formatting macro: snippet: java.lang.NullPointerException

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

Error formatting macro: snippet: java.lang.NullPointerException

And to skip sending to the mock:foo endpoint we use the *skip() DSL in the route at the end to instruct Camel to skip sending to the original intended endpoint.

Error formatting macro: snippet: java.lang.NullPointerException

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:

Error formatting macro: snippet: java.lang.NullPointerException

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

Error formatting macro: snippet: java.lang.NullPointerException

And the 3rd with the skip, notice skip is set with the skipSendToOriginalEndpoint attribute on the interceptSendToEndpoint tag:

Error formatting macro: snippet: java.lang.NullPointerException
  • No labels