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

davsclaus pushed a commit to branch camel-4.18.x
in repository https://gitbox.apache.org/repos/asf/camel.git


The following commit(s) were added to refs/heads/camel-4.18.x by this push:
     new a9c901f8ee5b [backport camel-4.18.x] CAMEL-26977: camel-file - 
idempotent=true or noop=true with an idempotent read lock consumes the file 
(#30080)
a9c901f8ee5b is described below

commit a9c901f8ee5b30b12dfdae377a40ded75199cd55
Author: Federico Mariani <[email protected]>
AuthorDate: Sun Nov 15 19:02:48 2026 +0100

    [backport camel-4.18.x] CAMEL-26977: camel-file - idempotent=true or 
noop=true with an idempotent read lock consumes the file (#30080)
    
    Co-authored-by: Claude <[email protected]>
---
 .../camel/component/file/GenericFileEndpoint.java  |  11 +++
 ...leIdempotentReadLockIdempotentConsumerTest.java | 100 +++++++++++++++++++++
 2 files changed, 111 insertions(+)

diff --git 
a/components/camel-file/src/main/java/org/apache/camel/component/file/GenericFileEndpoint.java
 
b/components/camel-file/src/main/java/org/apache/camel/component/file/GenericFileEndpoint.java
index 4ebb699e4da4..307c8838d0bc 100644
--- 
a/components/camel-file/src/main/java/org/apache/camel/component/file/GenericFileEndpoint.java
+++ 
b/components/camel-file/src/main/java/org/apache/camel/component/file/GenericFileEndpoint.java
@@ -938,9 +938,20 @@ public abstract class GenericFileEndpoint<T> extends 
ScheduledPollEndpoint imple
     }
 
     public boolean isIdempotentEager() {
+        // an idempotent read lock adds the key to the same idempotent 
repository when it acquires the lock, so the
+        // consumer only checks the repository: adding the key eagerly would 
make the read lock refuse every file
+        if (isIdempotentReadLock()) {
+            return false;
+        }
         return idempotentEager != null ? idempotentEager : false;
     }
 
+    private boolean isIdempotentReadLock() {
+        return exclusiveReadLockStrategy == null
+                && ("idempotent".equals(readLock) || 
"idempotent-changed".equals(readLock)
+                        || "idempotent-rename".equals(readLock));
+    }
+
     public String getCharset() {
         return charset;
     }
diff --git 
a/core/camel-core/src/test/java/org/apache/camel/component/file/strategy/FileIdempotentReadLockIdempotentConsumerTest.java
 
b/core/camel-core/src/test/java/org/apache/camel/component/file/strategy/FileIdempotentReadLockIdempotentConsumerTest.java
new file mode 100644
index 000000000000..70e1740010d3
--- /dev/null
+++ 
b/core/camel-core/src/test/java/org/apache/camel/component/file/strategy/FileIdempotentReadLockIdempotentConsumerTest.java
@@ -0,0 +1,100 @@
+/*
+ * 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.component.file.strategy;
+
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.Exchange;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.apache.camel.spi.Registry;
+import 
org.apache.camel.support.processor.idempotent.MemoryIdempotentRepository;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.ValueSource;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+/**
+ * An idempotent consumer (idempotent=true, or noop=true) with an idempotent 
read lock: both use the same idempotent
+ * repository, and the file is consumed once.
+ */
+class FileIdempotentReadLockIdempotentConsumerTest extends ContextTestSupport {
+
+    private final MemoryIdempotentRepository myRepo = new 
MemoryIdempotentRepository();
+
+    @Override
+    public boolean isUseRouteBuilder() {
+        return false;
+    }
+
+    @Override
+    protected Registry createCamelRegistry() throws Exception {
+        Registry jndi = super.createCamelRegistry();
+        jndi.bind("myRepo", myRepo);
+        return jndi;
+    }
+
+    @ParameterizedTest
+    @ValueSource(strings = { "idempotent", "idempotent-changed", 
"idempotent-rename" })
+    void noopConsumesTheFileOnce(String readLock) throws Exception {
+        startRoute("noop=true&readLock=" + readLock);
+
+        MockEndpoint mock = getMockEndpoint("mock:result");
+        mock.expectedBodiesReceived("Hello World");
+        // the file stays, and the next polls skip it
+        mock.setAssertPeriod(500);
+
+        template.sendBodyAndHeader(fileUri(), "Hello World", 
Exchange.FILE_NAME, "hello.txt");
+
+        mock.assertIsSatisfied();
+        
assertThat(myRepo.contains(testFile("hello.txt").toAbsolutePath().toString())).isTrue();
+    }
+
+    @Test
+    void idempotentConsumesTheFileOnce() throws Exception {
+        startRoute("idempotent=true&readLock=idempotent");
+
+        MockEndpoint mock = getMockEndpoint("mock:result");
+        mock.expectedBodiesReceived("Hello World");
+
+        template.sendBodyAndHeader(fileUri(), "Hello World", 
Exchange.FILE_NAME, "hello.txt");
+
+        mock.assertIsSatisfied();
+
+        // a file with the same name is skipped as it was consumed before
+        mock.reset();
+        mock.expectedMessageCount(0);
+        mock.setAssertPeriod(500);
+
+        template.sendBodyAndHeader(fileUri(), "Hello Again", 
Exchange.FILE_NAME, "hello.txt");
+
+        mock.assertIsSatisfied();
+        assertThat(testFile("hello.txt")).exists();
+    }
+
+    private void startRoute(String options) throws Exception {
+        context.addRoutes(new RouteBuilder() {
+            @Override
+            public void configure() {
+                
from(fileUri("?initialDelay=0&delay=10&readLockCheckInterval=50&idempotentRepository=#myRepo&"
+                             + options))
+                        .to("mock:result");
+            }
+        });
+        context.start();
+    }
+}

Reply via email to