I would like to access all the Synchronization’s that were added to an exchange during a camel route.
The reason for this is that when a particular type of exception happens, I want to route the message to an error handling component and let that error handling component execute the “onCompletion” of those synchronizations even though there was an exception.
the synchronizations are added by using
exchange.getUnitofWork().addSynchronization(new MySyncAdapter());
and I was trying to access them with
exchange.handoverCompletions();
However, I think I must be doing something wrong because no matter which component I try to get the completions from (my real component, or the error component), the list is null.
Edit:
According tot he answer below, this should work:
exchange.addOnCompletion(new MySyncAdapter());
List<Synchronization> syncs = exchange.handoverCompletions();
however, syncs is still null. Any suggestions?
The Exchange in this case does not have the Completions/Synchronizations, the UnitOfWork instance does so when you call
exchange.handoverCompletions()you will get a null. You also do not have access to the synchronizations in the UnitOfWork because it is being handled in a different thread. Any attempt to modify them, which is what handoverCompletion does, causes a concurrency exception.In reality you are trying to use something in a way it wasn’t intended. Exchange errors should be handled by invoking the Exception Clause DSL outlined here. It is designed to capture exchange errors in a fine grained manner by allowing a developer to define the Exception type and forward the Exchange to a route for further processing by your error handling component.
Best Regards,
Scott ES