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.Ce contenu n'est pas disponible dans la langue sélectionnée.
2.11. OnCompletion
Overview Copier lienLien copié sur presse-papiers!
Copier lienLien copié sur presse-papiers!
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
OnCompletioncommand can be global or per route. A route scope overrides global scope. OnCompletioncan be configured to be triggered on success for failure.- The
onWhenpredicate can be used to only trigger theonCompletionin 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 Copier lienLien copié sur presse-papiers!
Copier lienLien copié sur presse-papiers!
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 Copier lienLien copié sur presse-papiers!
Copier lienLien copié sur presse-papiers!
To define
onCompletion for more than just one route:
Using onWhen Copier lienLien copié sur presse-papiers!
Copier lienLien copié sur presse-papiers!
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 Copier lienLien copié sur presse-papiers!
Copier lienLien copié sur presse-papiers!
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 Copier lienLien copié sur presse-papiers!
Copier lienLien copié sur presse-papiers!
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
onCompletionto 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: