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

davsclaus pushed a commit to branch backport/CAMEL-26945-inflight-elapsed-4.22.x
in repository https://gitbox.apache.org/repos/asf/camel.git

commit ddf50da11d652fefbf41c70d54de522412e5fb15
Author: Claus Ibsen <[email protected]>
AuthorDate: Fri Nov 13 22:07:46 2026 +0100

    CAMEL-26945: camel-core - the time an inflight exchange has been at its 
node is no longer garbage
    
    While an exchange is at a node, the inflight repository computes how long it
    has been there with MessageHistory.getElapsedSinceCreated(), whose default
    subtracted the creation time in wall clock millis from System.nanoTime(). 
The
    result was about the uptime of the machine in nanoseconds, shown as elapsed
    by the inflight dev console (camel get inflight) and the JMX browse of the
    inflight repository.
    
    DefaultMessageHistory, which the message history factories of Camel use,
    measures it on its monotonic clock, as it measures the time the node took.
    The default method subtracts the creation time from the current time in
    millis.
    
    Backport of #29887 to camel-4.22.x: the default method reads
    System.currentTimeMillis(), as this branch has no TimeSource; the inflight
    dev console has no descriptions of its fields here, so their swap is not
    part of it.
    
    (cherry picked from commit 8092d88aba4d9a478d335eca831a37de96625735)
    
    Co-Authored-By: Claude Opus 5.5 <[email protected]>
    Signed-off-by: Claus Ibsen <[email protected]>
---
 .../main/java/org/apache/camel/MessageHistory.java |  4 +-
 .../camel/impl/InflightRepositoryElapsedTest.java  | 98 ++++++++++++++++++++++
 .../camel/support/DefaultMessageHistory.java       |  6 ++
 3 files changed, 106 insertions(+), 2 deletions(-)

diff --git a/core/camel-api/src/main/java/org/apache/camel/MessageHistory.java 
b/core/camel-api/src/main/java/org/apache/camel/MessageHistory.java
index a5906fcfd36a..85d19814bd03 100644
--- a/core/camel-api/src/main/java/org/apache/camel/MessageHistory.java
+++ b/core/camel-api/src/main/java/org/apache/camel/MessageHistory.java
@@ -52,10 +52,10 @@ public interface MessageHistory {
     long getElapsed();
 
     /**
-     * The elapsed time since created.
+     * The elapsed time in millis since created, such as how long the node has 
been processing while it is not done.
      */
     default long getElapsedSinceCreated() {
-        return System.nanoTime() - getTime();
+        return System.currentTimeMillis() - getTime();
     }
 
     /**
diff --git 
a/core/camel-core/src/test/java/org/apache/camel/impl/InflightRepositoryElapsedTest.java
 
b/core/camel-core/src/test/java/org/apache/camel/impl/InflightRepositoryElapsedTest.java
new file mode 100644
index 000000000000..7759c0e7c312
--- /dev/null
+++ 
b/core/camel-core/src/test/java/org/apache/camel/impl/InflightRepositoryElapsedTest.java
@@ -0,0 +1,98 @@
+/*
+ * 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.impl;
+
+import java.lang.reflect.InvocationHandler;
+import java.lang.reflect.Proxy;
+import java.util.Collection;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.TimeUnit;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.MessageHistory;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.spi.InflightRepository;
+import org.junit.jupiter.api.Test;
+
+import static org.awaitility.Awaitility.await;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+/**
+ * CAMEL-26945: the time an inflight exchange has been at the node it is at, 
while it is still there.
+ */
+public class InflightRepositoryElapsedTest extends ContextTestSupport {
+
+    private final CountDownLatch done = new CountDownLatch(1);
+
+    @Override
+    protected CamelContext createCamelContext() throws Exception {
+        CamelContext context = super.createCamelContext();
+        context.setMessageHistory(true);
+        context.getInflightRepository().setInflightBrowseEnabled(true);
+        return context;
+    }
+
+    @Test
+    public void testElapsedAtTheNodeTheExchangeIsAt() {
+        template.asyncSendBody("direct:start", "Hello World");
+        try {
+            // the exchange waits at the node: the time it has been there grows
+            await().atMost(10, TimeUnit.SECONDS).until(() -> elapsed() >= 100);
+
+            InflightRepository.InflightExchange inflight = 
context.getInflightRepository().browse().iterator().next();
+            assertEquals("wait", inflight.getNodeId());
+            long elapsed = inflight.getElapsed();
+            long duration = inflight.getDuration();
+            // and it is no longer than the exchange is old
+            assertTrue(elapsed >= 100 && elapsed <= duration,
+                    "Elapsed " + elapsed + " ms at the node of an exchange 
that is " + duration + " ms old");
+        } finally {
+            done.countDown();
+        }
+    }
+
+    @Test
+    public void testElapsedSinceCreatedOfAMessageHistoryWithoutAClock() {
+        // a message history of another factory, which only has the time it 
was created
+        long created = System.currentTimeMillis() - 250;
+        MessageHistory history = (MessageHistory) 
Proxy.newProxyInstance(MessageHistory.class.getClassLoader(),
+                new Class<?>[] { MessageHistory.class },
+                (proxy, method, args) -> "getTime".equals(method.getName())
+                        ? created : InvocationHandler.invokeDefault(proxy, 
method, args));
+
+        long elapsed = history.getElapsedSinceCreated();
+        assertTrue(elapsed >= 250 && elapsed < 10000, "Elapsed " + elapsed + " 
ms since created 250 ms ago");
+    }
+
+    private long elapsed() {
+        Collection<InflightRepository.InflightExchange> list = 
context.getInflightRepository().browse();
+        return list.isEmpty() ? 0 : list.iterator().next().getElapsed();
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() {
+        return new RouteBuilder() {
+            @Override
+            public void configure() {
+                from("direct:start").routeId("slow")
+                        .process(e -> done.await(10, 
TimeUnit.SECONDS)).id("wait");
+            }
+        };
+    }
+}
diff --git 
a/core/camel-support/src/main/java/org/apache/camel/support/DefaultMessageHistory.java
 
b/core/camel-support/src/main/java/org/apache/camel/support/DefaultMessageHistory.java
index a2de8ce6c12c..e959d17d89f5 100644
--- 
a/core/camel-support/src/main/java/org/apache/camel/support/DefaultMessageHistory.java
+++ 
b/core/camel-support/src/main/java/org/apache/camel/support/DefaultMessageHistory.java
@@ -81,6 +81,12 @@ public class DefaultMessageHistory implements MessageHistory 
{
         }
     }
 
+    @Override
+    public long getElapsedSinceCreated() {
+        // the monotonic clock, as the node processing time is measured
+        return clock.elapsed();
+    }
+
     @Override
     public Message getMessage() {
         return message;

Reply via email to