You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 4 Next »

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

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 WireTap. 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.

Error formatting macro: snippet: java.lang.NullPointerException

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:

Error formatting macro: snippet: java.lang.NullPointerException

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:

Error formatting macro: snippet: java.lang.NullPointerException

And the onCompleteOnly and onFailureOnly is defined as a boolean attribute on the <onCompletion> tag so the failure example would be:

Error formatting macro: snippet: java.lang.NullPointerException

See Also

  • Unit of Work
  • No labels