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 <claus.ib...@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) {