...
In Camel 2.0 we have added DSL for these callbacks using the new onCompletion DSL name.
onCompletion supports the following features:
- scope: global and/or per route (route scope override all global scope)
- multiple global scope
- triggered either always, only if completed with success, or only if failed
onWhen
predicate to only trigger in certain situations
onCompletion with route scope
The onCompletion DSL allows you to add custom routes/processors when the original Exchange is complete. Camel spin off a copy of the Exchange and routes it in a separate thread, kinda like a Wire Tap. This allows the original thread to continue while the onCompletion route is running concurrently. We decided for this model as we did not want the onCompletion route to interfere with the original route.
...
Wiki Markup |
---|
{snippet:id=e1|lang=xml|url=camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/processor/SpringOnCompletionOnFailureOnlyTest.xml} |
onCompletion with global scope
This works just like the route scope except from the fact that they are defined globally. An example below:
Wiki Markup |
---|
{snippet:id=e1|lang=java|url=camel/trunk/camel-core/src/test/java/org/apache/camel/processor/OnCompletionGlobalTest.java} |
Using onCompletion from Spring DSL
This works just like the route scope except from the fact that they are defined globally. An example below:
Wiki Markup |
---|
{snippet:id=e1|lang=xml|url=camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/processor/SpringOnCompletionGlobalTest.xml} |
Info | ||
---|---|---|
| ||
If a onCompletion is defined in a route, it overrides all global scoped and thus its only the route scoped that are used. The global is newer used. |
Using onCompletion with onWhen predicate
As other DSL in Camel you can attach a Predicate to the onCompletion so it only triggers in certain conditions, when the predicate matches.
For example to only trigger if the message body contains the word Hello
we can do like:
Wiki Markup |
---|
{snippet:id=e1|lang=java|url=camel/trunk/camel-core/src/test/java/org/apache/camel/processor/OnCompletionWhenTest.java} |
See Also
- Unit of Work