From d342d2bffa5a895d4de15a163492ed6358a2a66d Mon Sep 17 00:00:00 2001 From: Nicolas Filotto Date: Fri, 11 Oct 2024 16:00:46 +0200 Subject: [PATCH] CAMEL-21343: camel-seda - Allow to mimic the behavior of camel-vm --- .../camel/component/seda/SedaConsumer.java | 48 +++++++++++++------ 1 file changed, 33 insertions(+), 15 deletions(-) diff --git a/components/camel-seda/src/main/java/org/apache/camel/component/seda/SedaConsumer.java b/components/camel-seda/src/main/java/org/apache/camel/component/seda/SedaConsumer.java index 5fdb91887104f..89b0e0227b9aa 100644 --- a/components/camel-seda/src/main/java/org/apache/camel/component/seda/SedaConsumer.java +++ b/components/camel-seda/src/main/java/org/apache/camel/component/seda/SedaConsumer.java @@ -26,6 +26,7 @@ import org.apache.camel.AsyncCallback; import org.apache.camel.AsyncProcessor; import org.apache.camel.Exchange; +import org.apache.camel.ExchangeExtension; import org.apache.camel.Processor; import org.apache.camel.ShutdownRunningTask; import org.apache.camel.Suspendable; @@ -181,19 +182,13 @@ protected void doRun() { } if (exchange != null) { try { - final Exchange target = exchange; + final Exchange original = exchange; // prepare the exchange before sending to consumer - prepareExchange(target); + final Exchange prepared = prepareExchange(exchange); // callback to be executed when sending to consumer and processing is done - AsyncCallback callback = doneSync -> { - // log exception if an exception occurred and was not handled - if (target.getException() != null) { - getExceptionHandler().handleException("Error processing exchange", target, - target.getException()); - } - }; + AsyncCallback callback = doneSync -> onProcessingDone(original, prepared); // process the exchange - sendToConsumers(target, callback); + sendToConsumers(prepared, callback); } catch (Exception e) { getExceptionHandler().handleException("Error processing exchange", exchange, e); } @@ -215,15 +210,38 @@ protected void doRun() { } } + /** + * Strategy to invoke when the exchange is done being processed. + *

+ * This method is meant to be overridden by subclasses to be able to mimic the behavior of the legacy component + * camel-vm, that is why the parameter {@code prepared} is not used default. + * + * @param original the exchange before being processed + * @param prepared the exchange processed + */ + protected void onProcessingDone(Exchange original, Exchange prepared) { + // log exception if an exception occurred and was not handled + if (original.getException() != null) { + getExceptionHandler().handleException("Error processing exchange", original, + original.getException()); + } + } + /** * Strategy to prepare exchange for being processed by this consumer + *

+ * This method is meant to be overridden by subclasses to be able to mimic the behavior of the legacy component + * camel-vm, that is why the prepared exchange is returned. * - * @param exchange the exchange + * @param exchange the exchange + * @return the exchange to process by this consumer */ - protected void prepareExchange(Exchange exchange) { - // this consumer grabbed the exchange so mark its from this route/endpoint - exchange.getExchangeExtension().setFromEndpoint(getEndpoint()); - exchange.getExchangeExtension().setFromRouteId(getRouteId()); + protected Exchange prepareExchange(Exchange exchange) { + // this consumer grabbed the exchange so mark it's from this route/endpoint + ExchangeExtension exchangeExtension = exchange.getExchangeExtension(); + exchangeExtension.setFromEndpoint(getEndpoint()); + exchangeExtension.setFromRouteId(getRouteId()); + return exchange; } /**