...
Camel supports two kinds of interceptors
intercept
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.
...
- Predicate using
when
to only trigger the interceptor in certain conditions proceed
when used withinterceptFrom
to continue routing from the point of interception when the interceptor is finished.proceed
is default and can be omitted.stop
when used withintercept
interceptFrom
will stops routing the Exchange completely. Camel will by default not stop.skip
when used withinterceptSendToEndpoint
will skip sending the Exchange to the original intended endpoint. Camel will by default not skip.
...
InterceptFrom
Intercept InterceptFrom 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
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
.
Wiki Markup |
---|
{snippet:id=e1|lang=java|url=camel/trunk/camel-core/src/test/java/org/apache/camel/processor/intercept/InterceptorSimpleLogTestInterceptFromSimpleLogTest.java} |
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:
Wiki Markup |
---|
{snippet:id=e1|lang=java|url=camel/trunk/camel-core/src/test/java/org/apache/camel/processor/intercept/InterceptorSimplePredicateTestInterceptFromSimplePredicateTest.java} |
And if we want to filter out certain messages we can use the stop()
to instruct Camel to stop continue routing the Exchange:
Wiki Markup |
---|
{snippet:id=e1|lang=java|url=camel/trunk/camel-core/src/test/java/org/apache/camel/processor/intercept/InterceptorSimplePredicateWithStopTestInterceptFromSimplePredicateWithStopTest.java} |
Using from Spring DSL
...
Wiki Markup |
---|
{snippet:id=example|lang=xml|url=camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/processor/SpringInterceptorTestSpringInterceptFromTest.xml} |
InterceptSendToEndpoint
...
Wiki Markup |
---|
{snippet:id=e1|lang=java|url=camel/trunk/camel-core/src/test/java/org/apache/camel/processor/InterceptEndpointTestintercept/InterceptSendToEndpointTest.java} |
And this time we add the Predicate so its only when the message body is Hello World
we intercept.
Wiki Markup |
---|
{snippet:id=e2|lang=java|url=camel/trunk/camel-core/src/test/java/org/apache/camel/processor/InterceptEndpointTestintercept/InterceptSendToEndpointTest.java} |
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.
Wiki Markup |
---|
{snippet:id=e3|lang=java|url=camel/trunk/camel-core/src/test/java/org/apache/camel/processor/intercept/InterceptEndpointTestInterceptSendToEndpointTest.java} |
Using from Spring DSL
...
Wiki Markup |
---|
{snippet:id=e1|lang=xml|url=camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/processor/interceptendpointinterceptSendToEndpoint.xml} |
And the 2nd. Notice how we can leverage the Simple language for the Predicate:
Wiki Markup |
---|
{snippet:id=e1|lang=xml|url=camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/processor/interceptendpointwheninterceptSendToEndpointWhen.xml} |
And the 3rd with the skip
, notice skip is set with the skipSendToOriginalEndpoint
attribute on the interceptSendToEndpoint tag:
Wiki Markup |
---|
{snippet:id=e1|lang=xml|url=camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/processor/interceptendpointskipinterceptSendToEndpointSkip.xml} |