...
You can get hold of the org.apache.camel.spi.UnitOfWork
from org.apache.camel.Exchange
with the method getUnitOfWork()
.
...
From Camel 2.14: onCompletion
has been modified to support running the completion task in either synchronous or asynchronous mode (using a thread pool) and also whether to run before or after the route consumer is done. The reason is to give more flexibility. For example to specify to run synchronous and before the route consumer is done, which allows to modify the exchange before the consumer writes back any response to the callee. You can use this to for example add customer headers, or send to a log to log the response message, etc.
...
The onCompletion
has changed defaults and behavior from Camel 2.14: it now runs
- Runs synchronously without any thread pool
In Camel 2.13 the defaults were
- Runs asynchronous using a thread pool
...
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 e.g., send logs to a central log database, send an email, send alerts 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.
...
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.
...
...
You can only have one onCompletion
clause per route. At context scoped level it's possible to have many. And notice that when you use a route scoped onCompletion
then any context scoped are disabled for that given route.
Wiki Markup 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 onFailureOnly
as shown below: Wiki Markup onCompletion
Exchange as Camel will add the property Exchange.ON_COMPLETION
with a boolean value of true
when it spin offs the onCompletion
Exchange.
...
The onCompletion
is defined like this with Spring DSL: Wiki Markup onCompleteOnly
and onFailureOnly
is defined as a boolean attribute on the <onCompletion>
tag so the failure example would be: Wiki Markup
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
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
...
...
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: Wiki Markup
Using onCompletion
With or Without a Threadpool
...
onCompletion
will from Camel 2.14: not use thread pool by default. To use thread pool then either set a executorService
or set parallelProcessing
to true
.
For example in Java DSL:
...
And in XML DSL:
...
You can also refer to a specific thread pool to be used, using the executorServiceRef
option:
...
...
Using onCompletion
to Run Before Route Consumer Sends Back Response to Callee
...
For example to always add a "created by" header you use modeBeforeConsumer()
as shown below:
...
...
And in XML DSL you set the mode attribute to BeforeConsumer
:
...
...
See Also
- Unit of Work