You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2021/11/13 12:01:22 UTC

[camel] branch main updated (cd3f56f -> a77c354)

This is an automated email from the ASF dual-hosted git repository.

davsclaus pushed a change to branch main
in repository https://gitbox.apache.org/repos/asf/camel.git.


    from cd3f56f  CAMEL-17190: camel-quickfix - Use ref counters to keep track on engine usage, and stop engine when no longer in use. And start if needed, such as manually stop a route and then later start it again.
     new 53e406e  Polished
     new 8e7f86b  CAMEL-17174: fixed issue where exchange UnitOfWork was not being cleared upon completion.
     new a77c354  CAMEL-17174: fixed issue where exchange UnitOfWork was not being cleared upon completion.

The 3 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 .../java/org/apache/camel/spi/ExchangeFactory.java |  2 +-
 .../camel/impl/engine/DefaultUnitOfWork.java       |  5 +-
 .../apache/camel/impl/engine/MDCUnitOfWork.java    |  1 +
 .../impl/engine/PrototypeExchangeFactory.java      |  2 +-
 .../camel/processor/UnitOfWorkHelperTest.java      | 95 ++++++++++++++++++++++
 .../org/apache/camel/support/UnitOfWorkHelper.java |  1 -
 6 files changed, 102 insertions(+), 4 deletions(-)
 create mode 100644 core/camel-core/src/test/java/org/apache/camel/processor/UnitOfWorkHelperTest.java

[camel] 02/03: CAMEL-17174: fixed issue where exchange UnitOfWork was not being cleared upon completion.

Posted by da...@apache.org.
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

commit 8e7f86b0c0e090d4d82ef93e93dcb524f447b0f5
Author: Marc Eiro <ma...@sap.com>
AuthorDate: Fri Nov 12 12:38:10 2021 +0100

    CAMEL-17174: fixed issue where exchange UnitOfWork was not being cleared upon completion.
---
 .../camel/processor/UnitOfWorkHelperTest.java      | 96 ++++++++++++++++++++++
 .../org/apache/camel/support/UnitOfWorkHelper.java |  4 +
 2 files changed, 100 insertions(+)

diff --git a/core/camel-core/src/test/java/org/apache/camel/processor/UnitOfWorkHelperTest.java b/core/camel-core/src/test/java/org/apache/camel/processor/UnitOfWorkHelperTest.java
new file mode 100644
index 0000000..23f36b9
--- /dev/null
+++ b/core/camel-core/src/test/java/org/apache/camel/processor/UnitOfWorkHelperTest.java
@@ -0,0 +1,96 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.camel.processor;
+
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.EndpointInject;
+import org.apache.camel.Exchange;
+import org.apache.camel.ExtendedExchange;
+import org.apache.camel.Message;
+import org.apache.camel.component.direct.DirectEndpoint;
+import org.apache.camel.component.file.FileComponent;
+import org.apache.camel.component.file.GenericFile;
+import org.apache.camel.component.file.GenericFileMessage;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.apache.camel.spi.CamelEvent;
+import org.apache.camel.spi.CamelEvent.ExchangeCreatedEvent;
+import org.apache.camel.support.DefaultExchange;
+import org.apache.camel.support.EventNotifierSupport;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+public class UnitOfWorkHelperTest extends ContextTestSupport {
+
+    private static final String FILE_CONTENT = "Lorem ipsum dolor sit amet";
+
+    @EndpointInject(value = "mock:result")
+    protected MockEndpoint resultEndpoint;
+    private DirectEndpoint fromEndpoint;
+
+    private CustomEventNotifier eventNotifier;
+    private int numberOfExchangeCreatedEvents;
+
+    @Test
+    void testUoWShouldBeClearedOnJobDone() throws Exception {
+        eventNotifier = new CustomEventNotifier();
+        context.getManagementStrategy().addEventNotifier(eventNotifier);
+        Exchange testExchange = createExchange("testFile");
+
+        template.send("direct:from", testExchange);
+        template.send("direct:from", testExchange);
+
+        assertEquals(2, numberOfExchangeCreatedEvents);
+    }
+
+    private Exchange createExchange(String fileName) {
+        Exchange testExchange = new DefaultExchange(context);
+
+        GenericFile<String> testFile = createFile(fileName);
+        Message testMessage = new GenericFileMessage<String>(testExchange, testFile);
+        testMessage.setBody(testFile);
+
+        testExchange.setIn(testMessage);
+        ExtendedExchange extExchange = testExchange.adapt(ExtendedExchange.class);
+        extExchange.setFromEndpoint(fromEndpoint);
+        testExchange.setProperty(FileComponent.FILE_EXCHANGE_FILE, testFile);
+
+        return testExchange;
+    }
+
+    private GenericFile<String> createFile(final String fileName) {
+        GenericFile<String> testFile = new GenericFile<String>();
+
+        testFile.setFile(FILE_CONTENT);
+        testFile.setAbsoluteFilePath(fileName);
+        testFile.setBody(FILE_CONTENT);
+
+        return testFile;
+    }
+
+    public class CustomEventNotifier extends EventNotifierSupport {
+
+        @Override
+        public void notify(final CamelEvent event) {
+            if (event instanceof ExchangeCreatedEvent) {
+                numberOfExchangeCreatedEvents += 1;
+            }
+        }
+    }
+
+}
diff --git a/core/camel-support/src/main/java/org/apache/camel/support/UnitOfWorkHelper.java b/core/camel-support/src/main/java/org/apache/camel/support/UnitOfWorkHelper.java
index 8dafdcc..e160d9a 100644
--- a/core/camel-support/src/main/java/org/apache/camel/support/UnitOfWorkHelper.java
+++ b/core/camel-support/src/main/java/org/apache/camel/support/UnitOfWorkHelper.java
@@ -21,6 +21,7 @@ import java.util.Collections;
 import java.util.List;
 
 import org.apache.camel.Exchange;
+import org.apache.camel.ExtendedExchange;
 import org.apache.camel.Route;
 import org.apache.camel.Service;
 import org.apache.camel.spi.Synchronization;
@@ -67,6 +68,9 @@ public final class UnitOfWorkHelper {
                     exchange, e);
         }
 
+        // MUST clear and set uow to null on exchange after done
+        ExtendedExchange ee = (ExtendedExchange) exchange;
+        ee.setUnitOfWork(null);
     }
 
     public static void doneSynchronizations(Exchange exchange, List<Synchronization> synchronizations, Logger log) {

[camel] 03/03: CAMEL-17174: fixed issue where exchange UnitOfWork was not being cleared upon completion.

Posted by da...@apache.org.
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

commit a77c354dee5299701843671bf8a13da86cd1928c
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Sat Nov 13 12:50:32 2021 +0100

    CAMEL-17174: fixed issue where exchange UnitOfWork was not being cleared upon completion.
---
 .../apache/camel/impl/engine/DefaultUnitOfWork.java   |  5 ++++-
 .../org/apache/camel/impl/engine/MDCUnitOfWork.java   |  1 +
 .../apache/camel/processor/UnitOfWorkHelperTest.java  | 19 +++++++++----------
 .../org/apache/camel/support/UnitOfWorkHelper.java    |  5 -----
 4 files changed, 14 insertions(+), 16 deletions(-)

diff --git a/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/DefaultUnitOfWork.java b/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/DefaultUnitOfWork.java
index a68d058a..80b08c3 100644
--- a/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/DefaultUnitOfWork.java
+++ b/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/DefaultUnitOfWork.java
@@ -270,7 +270,10 @@ public class DefaultUnitOfWork implements UnitOfWork {
     }
 
     protected void onDone() {
-        // optimized to do nothing
+        // MUST clear and set uow to null on exchange after done
+        // in case the same exchange is manually reused by Camel end users (should happen seldom)
+        ExtendedExchange ee = (ExtendedExchange) exchange;
+        ee.setUnitOfWork(null);
     }
 
     @Override
diff --git a/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/MDCUnitOfWork.java b/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/MDCUnitOfWork.java
index dd76a31..797e181 100644
--- a/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/MDCUnitOfWork.java
+++ b/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/MDCUnitOfWork.java
@@ -202,6 +202,7 @@ public class MDCUnitOfWork extends DefaultUnitOfWork implements Service {
 
     @Override
     protected void onDone() {
+        super.onDone();
         // clear MDC, so we do not leak as Camel is done using this UoW
         clear();
     }
diff --git a/core/camel-core/src/test/java/org/apache/camel/processor/UnitOfWorkHelperTest.java b/core/camel-core/src/test/java/org/apache/camel/processor/UnitOfWorkHelperTest.java
index 23f36b9..855d7b9 100644
--- a/core/camel-core/src/test/java/org/apache/camel/processor/UnitOfWorkHelperTest.java
+++ b/core/camel-core/src/test/java/org/apache/camel/processor/UnitOfWorkHelperTest.java
@@ -14,19 +14,17 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-
 package org.apache.camel.processor;
 
 import org.apache.camel.ContextTestSupport;
-import org.apache.camel.EndpointInject;
 import org.apache.camel.Exchange;
 import org.apache.camel.ExtendedExchange;
 import org.apache.camel.Message;
-import org.apache.camel.component.direct.DirectEndpoint;
 import org.apache.camel.component.file.FileComponent;
 import org.apache.camel.component.file.GenericFile;
 import org.apache.camel.component.file.GenericFileMessage;
 import org.apache.camel.component.mock.MockEndpoint;
+import org.apache.camel.component.seda.SedaEndpoint;
 import org.apache.camel.spi.CamelEvent;
 import org.apache.camel.spi.CamelEvent.ExchangeCreatedEvent;
 import org.apache.camel.support.DefaultExchange;
@@ -39,21 +37,22 @@ public class UnitOfWorkHelperTest extends ContextTestSupport {
 
     private static final String FILE_CONTENT = "Lorem ipsum dolor sit amet";
 
-    @EndpointInject(value = "mock:result")
-    protected MockEndpoint resultEndpoint;
-    private DirectEndpoint fromEndpoint;
-
+    private MockEndpoint resultEndpoint;
+    private SedaEndpoint fromEndpoint;
     private CustomEventNotifier eventNotifier;
     private int numberOfExchangeCreatedEvents;
 
     @Test
     void testUoWShouldBeClearedOnJobDone() throws Exception {
+        resultEndpoint = context.getEndpoint("mock:result", MockEndpoint.class);
+        fromEndpoint = context.getEndpoint("seda:from", SedaEndpoint.class);
+
         eventNotifier = new CustomEventNotifier();
         context.getManagementStrategy().addEventNotifier(eventNotifier);
         Exchange testExchange = createExchange("testFile");
 
-        template.send("direct:from", testExchange);
-        template.send("direct:from", testExchange);
+        template.send(fromEndpoint, testExchange);
+        template.send(fromEndpoint, testExchange);
 
         assertEquals(2, numberOfExchangeCreatedEvents);
     }
@@ -62,7 +61,7 @@ public class UnitOfWorkHelperTest extends ContextTestSupport {
         Exchange testExchange = new DefaultExchange(context);
 
         GenericFile<String> testFile = createFile(fileName);
-        Message testMessage = new GenericFileMessage<String>(testExchange, testFile);
+        Message testMessage = new GenericFileMessage<>(testExchange, testFile);
         testMessage.setBody(testFile);
 
         testExchange.setIn(testMessage);
diff --git a/core/camel-support/src/main/java/org/apache/camel/support/UnitOfWorkHelper.java b/core/camel-support/src/main/java/org/apache/camel/support/UnitOfWorkHelper.java
index e160d9a..7e61ddf 100644
--- a/core/camel-support/src/main/java/org/apache/camel/support/UnitOfWorkHelper.java
+++ b/core/camel-support/src/main/java/org/apache/camel/support/UnitOfWorkHelper.java
@@ -21,7 +21,6 @@ import java.util.Collections;
 import java.util.List;
 
 import org.apache.camel.Exchange;
-import org.apache.camel.ExtendedExchange;
 import org.apache.camel.Route;
 import org.apache.camel.Service;
 import org.apache.camel.spi.Synchronization;
@@ -67,10 +66,6 @@ public final class UnitOfWorkHelper {
             LOG.warn("Exception occurred during done UnitOfWork for Exchange: {}. This exception will be ignored.",
                     exchange, e);
         }
-
-        // MUST clear and set uow to null on exchange after done
-        ExtendedExchange ee = (ExtendedExchange) exchange;
-        ee.setUnitOfWork(null);
     }
 
     public static void doneSynchronizations(Exchange exchange, List<Synchronization> synchronizations, Logger log) {

[camel] 01/03: Polished

Posted by da...@apache.org.
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

commit 53e406ee244ed0f767235cfe08498ac9afccbf13
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Sat Nov 13 12:34:31 2021 +0100

    Polished
---
 core/camel-api/src/main/java/org/apache/camel/spi/ExchangeFactory.java  | 2 +-
 .../java/org/apache/camel/impl/engine/PrototypeExchangeFactory.java     | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)

diff --git a/core/camel-api/src/main/java/org/apache/camel/spi/ExchangeFactory.java b/core/camel-api/src/main/java/org/apache/camel/spi/ExchangeFactory.java
index 83a5e04..3ecaf4c 100644
--- a/core/camel-api/src/main/java/org/apache/camel/spi/ExchangeFactory.java
+++ b/core/camel-api/src/main/java/org/apache/camel/spi/ExchangeFactory.java
@@ -31,7 +31,7 @@ import org.apache.camel.NonManagedService;
  * contract as we only want to control the created {@link Exchange} that comes into Camel via {@link Consumer} or
  * {@link org.apache.camel.PollingConsumer}.
  * <p/>
- * The factory is pluggable which allows to use different strategies. The default factory will create a new
+ * The factory is pluggable which allows using different strategies. The default factory will create a new
  * {@link Exchange} instance, and the pooled factory will pool and reuse exchanges.
  *
  * @see ProcessorExchangeFactory
diff --git a/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/PrototypeExchangeFactory.java b/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/PrototypeExchangeFactory.java
index e37be97..63215d3 100644
--- a/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/PrototypeExchangeFactory.java
+++ b/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/PrototypeExchangeFactory.java
@@ -51,7 +51,7 @@ public class PrototypeExchangeFactory extends PooledObjectFactorySupport<Exchang
     protected void doBuild() throws Exception {
         super.doBuild();
         this.exchangeFactoryManager = camelContext.adapt(ExtendedCamelContext.class).getExchangeFactoryManager();
-        // force to create and load the class during build time so the JVM does not
+        // force creating and load the class during build time so the JVM does not
         // load the class on first exchange to be created
         DefaultExchange dummy = new DefaultExchange(camelContext);
         // force message init to load classes