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 12666b7136d4 CAMEL-26977: camel-file - idempotent=true or noop=true
with an idempotent read lock consumes the file (#30006)
12666b7136d4 is described below
commit 12666b7136d4cbfb32047a4a4198db35e4621c16
Author: Federico Mariani <[email protected]>
AuthorDate: Sun Nov 15 15:34:46 2026 +0100
CAMEL-26977: camel-file - idempotent=true or noop=true with an idempotent
read lock consumes the file (#30006)
* CAMEL-26977: camel-file - idempotent=true or noop=true with an idempo…
…tent read lock consumes the file
The idempotent read lock adds the key to the same idempotent repository, so
the consumer no longer adds it eagerly
(the read lock was refused on every poll since 4.7).
Co-Authored-By: Claude <[email protected]>
---
.../camel/component/file/GenericFileEndpoint.java | 11 +++
...leIdempotentReadLockIdempotentConsumerTest.java | 100 +++++++++++++++++++++
.../ROOT/pages/camel-4x-upgrade-guide-4_24.adoc | 7 ++
3 files changed, 118 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 4479223677c2..8475ddcc6070 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
@@ -977,9 +977,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();
+ }
+}
diff --git
a/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_24.adoc
b/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_24.adoc
index 7987a1229abd..d07f463c80fc 100644
--- a/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_24.adoc
+++ b/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_24.adoc
@@ -386,6 +386,13 @@ event. Without `list` the informer cannot start: the
Kubernetes client logs an E
with a delay that doubles up to 30 seconds. Creating or deleting a secret or
config map still does not reload the
context.
+=== camel-file - idempotent consumer with an idempotent read lock
+
+With `idempotent=true` (or `noop=true`) and `readLock=idempotent`,
`idempotent-changed` or `idempotent-rename`, the
+file consumer no longer adds the key to the idempotent repository eagerly, and
`idempotentEager` does not apply: the
+read lock adds it when it acquires the lock. Since Camel 4.7 the read lock was
refused, and such a consumer never
+consumed a file.
+
=== Java
*Breaking change:* Camel 4.24 requires *Java 21* as the minimum runtime
version.