This is an automated email from the ASF dual-hosted git repository. davsclaus pushed a commit to branch main in repository https://gitbox.apache.org/repos/asf/camel.git
The following commit(s) were added to refs/heads/main by this push: new 6a7a216f1b9 CAMEL-20015: Move internal state in AbstractExchange to Extended exchange where we have such kind of state. (code cleanup) (#18183) 6a7a216f1b9 is described below commit 6a7a216f1b92b72ed8cedb0eda7e081f63757e49 Author: Claus Ibsen <claus.ib...@gmail.com> AuthorDate: Tue May 27 13:16:08 2025 +0200 CAMEL-20015: Move internal state in AbstractExchange to Extended exchange where we have such kind of state. (code cleanup) (#18183) --- .../java/org/apache/camel/ExchangeExtension.java | 12 +++++ .../org/apache/camel/support/AbstractExchange.java | 56 +++++++++------------- .../camel/support/ExtendedExchangeExtension.java | 12 +++++ 3 files changed, 46 insertions(+), 34 deletions(-) diff --git a/core/camel-api/src/main/java/org/apache/camel/ExchangeExtension.java b/core/camel-api/src/main/java/org/apache/camel/ExchangeExtension.java index ded6208da5b..38e82f38d04 100644 --- a/core/camel-api/src/main/java/org/apache/camel/ExchangeExtension.java +++ b/core/camel-api/src/main/java/org/apache/camel/ExchangeExtension.java @@ -274,4 +274,16 @@ public interface ExchangeExtension { */ Exchange createCopyWithProperties(CamelContext context); + /** + * Returns true if this exchange is an external initiated redelivered message (such as a JMS broker). + * <p/> + * <b>Important: </b> It is not always possible to determine if the message is a redelivery or not, and therefore + * <tt>false</tt> is returned. Such an example would be a JDBC message. However JMS brokers provides details if a + * message is redelivered. + * + * @param message the camel message + * @return <tt>true</tt> if redelivered, <tt>false</tt> if not or not able to determine + */ + boolean isExternalRedelivered(Message message); + } diff --git a/core/camel-support/src/main/java/org/apache/camel/support/AbstractExchange.java b/core/camel-support/src/main/java/org/apache/camel/support/AbstractExchange.java index 46d948f1b1e..fbfdbf5b46e 100644 --- a/core/camel-support/src/main/java/org/apache/camel/support/AbstractExchange.java +++ b/core/camel-support/src/main/java/org/apache/camel/support/AbstractExchange.java @@ -36,16 +36,14 @@ import org.apache.camel.MessageHistory; import org.apache.camel.SafeCopyProperty; import org.apache.camel.spi.UnitOfWork; import org.apache.camel.spi.VariableRepository; -import org.apache.camel.trait.message.MessageTrait; -import org.apache.camel.trait.message.RedeliveryTraitPayload; import org.apache.camel.util.ObjectHelper; /** * Base class for the two official and only implementations of {@link Exchange}, the {@link DefaultExchange} and * {@link DefaultPooledExchange}. * - * Camel end users should use {@link DefaultExchange} if creating an {@link Exchange} manually. However that is more - * seldom to use, as exchanges are created via {@link Endpoint}. + * Camel end users should use {@link DefaultExchange} if creating an {@link Exchange} manually. However, this is seldom + * used, because exchanges are created via {@link Endpoint}s. * * @see DefaultExchange */ @@ -65,7 +63,6 @@ abstract class AbstractExchange implements Exchange { protected Map<String, SafeCopyProperty> safeCopyProperties; protected ExchangeVariableRepository variableRepository; private final ExtendedExchangeExtension privateExtension; - private RedeliveryTraitPayload externalRedelivered = RedeliveryTraitPayload.UNDEFINED_REDELIVERY; protected AbstractExchange(CamelContext context, EnumMap<ExchangePropertyKey, Object> internalProperties, Map<String, Object> properties) { @@ -82,34 +79,29 @@ abstract class AbstractExchange implements Exchange { protected AbstractExchange(CamelContext context, ExchangePattern pattern) { this.context = context; this.pattern = pattern; - - internalProperties = new EnumMap<>(ExchangePropertyKey.class); - privateExtension = new ExtendedExchangeExtension(this); + this.internalProperties = new EnumMap<>(ExchangePropertyKey.class); + this.privateExtension = new ExtendedExchangeExtension(this); } protected AbstractExchange(Exchange parent) { this.context = parent.getContext(); this.pattern = parent.getPattern(); - - internalProperties = new EnumMap<>(ExchangePropertyKey.class); - - privateExtension = new ExtendedExchangeExtension(this); - privateExtension.setFromEndpoint(parent.getFromEndpoint()); - privateExtension.setFromRouteId(parent.getFromRouteId()); - privateExtension.setUnitOfWork(parent.getUnitOfWork()); + this.internalProperties = new EnumMap<>(ExchangePropertyKey.class); + this.privateExtension = new ExtendedExchangeExtension(this); + this.privateExtension.setFromEndpoint(parent.getFromEndpoint()); + this.privateExtension.setFromRouteId(parent.getFromRouteId()); + this.privateExtension.setUnitOfWork(parent.getUnitOfWork()); } @SuppressWarnings("CopyConstructorMissesField") protected AbstractExchange(AbstractExchange parent) { this.context = parent.getContext(); this.pattern = parent.getPattern(); - this.internalProperties = new EnumMap<>(parent.internalProperties); - - privateExtension = new ExtendedExchangeExtension(this); - privateExtension.setFromEndpoint(parent.getFromEndpoint()); - privateExtension.setFromRouteId(parent.getFromRouteId()); - privateExtension.setUnitOfWork(parent.getUnitOfWork()); + this.privateExtension = new ExtendedExchangeExtension(this); + this.privateExtension.setFromEndpoint(parent.getFromEndpoint()); + this.privateExtension.setFromRouteId(parent.getFromRouteId()); + this.privateExtension.setUnitOfWork(parent.getUnitOfWork()); setIn(parent.getIn().copy()); @@ -195,13 +187,13 @@ abstract class AbstractExchange implements Exchange { return ExchangeHelper.convertToType(this, type, value); } - // TODO: fix re-assignment of the value instance here. @SuppressWarnings("unchecked") - private <T> T evalPropertyValue(final Object defaultValue, final Class<T> type, Object value) { - if (value == null) { - value = defaultValue; + private <T> T evalPropertyValue(final Object defaultValue, final Class<T> type, final Object value) { + Object val = value; + if (val == null) { + val = defaultValue; } - if (value == null) { + if (val == null) { // let's avoid NullPointerException when converting to boolean for null values if (boolean.class == type) { return (T) Boolean.FALSE; @@ -211,11 +203,11 @@ abstract class AbstractExchange implements Exchange { // eager same instance type test to avoid the overhead of invoking the type converter // if already is the same type - if (type.isInstance(value)) { - return (T) value; + if (type.isInstance(val)) { + return (T) val; } - return ExchangeHelper.convertToType(this, type, value); + return ExchangeHelper.convertToType(this, type, val); } @Override @@ -657,11 +649,7 @@ abstract class AbstractExchange implements Exchange { @Override public boolean isExternalRedelivered() { - if (externalRedelivered == RedeliveryTraitPayload.UNDEFINED_REDELIVERY) { - Message message = getIn(); - externalRedelivered = (RedeliveryTraitPayload) message.getPayloadForTrait(MessageTrait.REDELIVERY); - } - return externalRedelivered == RedeliveryTraitPayload.IS_REDELIVERY; + return privateExtension.isExternalRedelivered(getIn()); } @Override diff --git a/core/camel-support/src/main/java/org/apache/camel/support/ExtendedExchangeExtension.java b/core/camel-support/src/main/java/org/apache/camel/support/ExtendedExchangeExtension.java index bb41cac492f..df037c91afe 100644 --- a/core/camel-support/src/main/java/org/apache/camel/support/ExtendedExchangeExtension.java +++ b/core/camel-support/src/main/java/org/apache/camel/support/ExtendedExchangeExtension.java @@ -26,9 +26,12 @@ import org.apache.camel.CamelContext; import org.apache.camel.Endpoint; import org.apache.camel.Exchange; import org.apache.camel.ExchangeExtension; +import org.apache.camel.Message; import org.apache.camel.SafeCopyProperty; import org.apache.camel.spi.Synchronization; import org.apache.camel.spi.UnitOfWork; +import org.apache.camel.trait.message.MessageTrait; +import org.apache.camel.trait.message.RedeliveryTraitPayload; public class ExtendedExchangeExtension implements ExchangeExtension { private final AbstractExchange exchange; @@ -48,6 +51,7 @@ public class ExtendedExchangeExtension implements ExchangeExtension { private AsyncCallback defaultConsumerCallback; // optimize (do not reset) private UnitOfWork unitOfWork; private List<Synchronization> onCompletions; + private RedeliveryTraitPayload externalRedelivered = RedeliveryTraitPayload.UNDEFINED_REDELIVERY; ExtendedExchangeExtension(AbstractExchange exchange) { this.exchange = exchange; @@ -309,6 +313,14 @@ public class ExtendedExchangeExtension implements ExchangeExtension { this.failureHandled = failureHandled; } + @Override + public boolean isExternalRedelivered(Message message) { + if (externalRedelivered == RedeliveryTraitPayload.UNDEFINED_REDELIVERY) { + externalRedelivered = (RedeliveryTraitPayload) message.getPayloadForTrait(MessageTrait.REDELIVERY); + } + return externalRedelivered == RedeliveryTraitPayload.IS_REDELIVERY; + } + UnitOfWork getUnitOfWork() { return unitOfWork; }