Fuse 6 is no longer supported
As of February 2025, Red Hat Fuse 6 is no longer supported. If you are using Fuse 6, please upgrade to Red Hat build of Apache Camel.Questo contenuto non è disponibile nella lingua selezionata.
2.11. OnCompletion
Overview Copia collegamentoCollegamento copiato negli appunti!
Copia collegamentoCollegamento copiato negli appunti!
The OnCompletion DSL name is used to define an action that is to take place when a
Unit of Work
is completed. A Unit of Work
is a Camel concept that encompasses an entire exchange. See Section 43.1, “Exchanges”. The onCompletion
command has the following features:
- The scope of the
OnCompletion
command can be global or per route. A route scope overrides global scope. OnCompletion
can be configured to be triggered on success for failure.- The
onWhen
predicate can be used to only trigger theonCompletion
in certain situations. - You can define whether or not to use a thread pool, though the default is no thread pool.
Route Only Scope for onCompletion Copia collegamentoCollegamento copiato negli appunti!
Copia collegamentoCollegamento copiato negli appunti!
When an
onCompletion
DSL is specified on an exchange, Camel spins off a new thread. This allows the original thread to continue without interference from the onCompletion
task. A route will only support one onCompletion
. In the following example, the onCompletion
is triggered whether the exchange completes with success or failure. This is the default action.
For XML the format is as follows:
To trigger the
onCompletion
on failure, the onFailureOnly
parameter can be used. Similarly, to trigger the onCompletion
on success, use the onCompleteOnly
parameter.
For XML,
onFailureOnly
and onCompleteOnly
are expressed as booleans on the onCompletion
tag:
Global Scope for onCompletion Copia collegamentoCollegamento copiato negli appunti!
Copia collegamentoCollegamento copiato negli appunti!
To define
onCompletion
for more than just one route:
Using onWhen Copia collegamentoCollegamento copiato negli appunti!
Copia collegamentoCollegamento copiato negli appunti!
To trigger the
onCompletion
under certain circumstances, use the onWhen
predicate. The following example will trigger the onCompletion
when the body of the message contains the word Hello
:
Using onCompletion with or without a thread pool Copia collegamentoCollegamento copiato negli appunti!
Copia collegamentoCollegamento copiato negli appunti!
As of Camel 2.14,
onCompletion
will not use a thread pool by default. To force the use of a thread pool, either set an executorService
or set parallelProcessing
to true. For example, in Java DSL, use the following format:
onCompletion().parallelProcessing() .to("mock:before") .delay(1000) .setBody(simple("OnComplete:${body}"));
onCompletion().parallelProcessing()
.to("mock:before")
.delay(1000)
.setBody(simple("OnComplete:${body}"));
For XML the format is:
Use the
executorServiceRef
option to refer to a specific thread pool:
Run onCompletion before Consumer Sends Response Copia collegamentoCollegamento copiato negli appunti!
Copia collegamentoCollegamento copiato negli appunti!
onCompletion
can be run in two modes:
- AfterConsumer - The default mode which runs after the consumer is finished
- BeforeConsumer - Runs before the consumer writes a response back to the callee. This allows
onCompletion
to modify the Exchange, such as adding special headers, or to log the Exchange as a response logger.
For example, to add a
created by
header to the response, use modeBeforeConsumer()
as shown below:
.onCompletion().modeBeforeConsumer() .setHeader("createdBy", constant("Someone")) .end()
.onCompletion().modeBeforeConsumer()
.setHeader("createdBy", constant("Someone"))
.end()
For XML, set the mode attribute to
BeforeConsumer
: