OnCompletion
Camel has this concept of a Unit of Work that encompass the Exchange. The unit of work among others supports synchronization callbacks that are invoked when the Exchange is complete. The callback API is defined in org.apache.camel.spi.Synchronization.
![]() | Getting the UnitOfWork You can get hold of the org.apache.camel.spi.UnitOfWork from org.apache.camel.Exchange with the method getUnitOfWork(). |
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
![]() | On completion runs in separate thread The onCompletion runs in a separate thread in parallel with the original route. It is therefore not intended to influence the outcome of the original route. The idea for on completion is to spin off a new thread to eg send logs to a central log database, send an email, send alterts to a monitoring system, store a copy of the result message etc.
Therefore if you want to do some work that influence the original route, then do not use onCompletion for that. Notice: if you use the UnitOfWork API as mentioned in the top of this page, then you can register a Synchronization callback on the Exchange which is executed in the original route. That way allows you to do some custom code when the route is completed; this is how custom components can enlist on completion services which they need, eg the File component does that for work that moves/deletes the original file etc. |
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.
from("direct:start")
.onCompletion()
.to("log:sync")
.to("mock:sync")
.end()
.process(new MyProcessor())
.to("mock:result");
By default the onCompletion will be triggered when the Exchange is complete and regardless if the Exchange completed with success or with an failure (such as an Exception was thrown). You can limit the trigger to only occur onCompleteOnly or by onFauilureOnly as shown below:
from("direct:start")
.onCompletion().onFailureOnly()
.to("log:sync")
.to("mock:sync")
.end()
.process(new MyProcessor())
.to("mock:result");
You can identify if the Exchange is an onCompletion Exchange as Camel will add the property Exchange.ON_COMPLETION with a boolean value of true when it spin offs the onCompletion Exchange.
Using onCompletion from Spring DSL
The onCompletion is defined like this with Spring DSL:
<route>
<from uri="direct:start"/>
<onCompletion>
<to uri="log:sync"/>
<to uri="mock:sync"/>
</onCompletion>
<process ref="myProcessor"/>
<to uri="mock:result"/>
</route>
And the onCompleteOnly and onFailureOnly is defined as a boolean attribute on the <onCompletion> tag so the failure example would be:
<route>
<from uri="direct:start"/>
<onCompletion _onFailureOnly_="true">
<to uri="log:sync"/>
<to uri="mock:sync"/>
</onCompletion>
<process ref="myProcessor"/>
<to uri="mock:result"/>
</route>
onCompletion with global scope
This works just like the route scope except from the fact that they are defined globally. An example below:
onCompletion().to("log:global").to("mock:sync");
from("direct:start")
.process(new MyProcessor())
.to("mock:result");
Using onCompletion from Spring DSL
This works just like the route scope except from the fact that they are defined globally. An example below:
<!-- this is a global onCompletion route that is invoke when any exchange is complete
as a kind of after callback -->
<onCompletion>
<to uri="log:global"/>
<to uri="mock:sync"/>
</onCompletion>
<route>
<from uri="direct:start"/>
<process ref="myProcessor"/>
<to uri="mock:result"/>
</route>
![]() | Route scope override Global scope If an onCompletion is defined in a route, it overrides all global scoped and thus its only the route scoped that are used. The globally scoped ones are never 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:
from("direct:start")
.onCompletion().onWhen(body().contains("Hello"))
.to("log:sync")
.to("mock:sync")
.end()
.to("log:original")
.to("mock:result");
See Also