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.2.11. OnCompletion
Overview
Copy linkLink copied to clipboard!
					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 theOnCompletioncommand can be global or per route. A route scope overrides global scope.
- OnCompletioncan be configured to be triggered on success for failure.
- TheonWhenpredicate 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
Copy linkLink copied to clipboard!
					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
Copy linkLink copied to clipboard!
					To define 
onCompletion for more than just one route:
				Using onWhen
Copy linkLink copied to clipboard!
					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
Copy linkLink copied to clipboard!
					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 
Copy linkLink copied to clipboard!
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 allowsonCompletionto 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: