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 6344a971e880 CAMEL-27384: camel-file - with recursive=true, a poll
deletes the done file of each directory it consumed, not only the one of its
last file (#30702)
6344a971e880 is described below
commit 6344a971e880cd1a5bf9fc66e96c20df087f5d76
Author: allthingssecurity <[email protected]>
AuthorDate: Tue Nov 24 15:37:49 2026 +0530
CAMEL-27384: camel-file - with recursive=true, a poll deletes the done file
of each directory it consumed, not only the one of its last file (#30702)
With a done file with a fixed name, each directory has its own done file.
Only the exchange with BATCH_COMPLETE (the last file of the poll) deleted
its done file, so when a poll consumed files of several directories, the
done files of the other directories stayed, and the next files written
there were consumed without a new done file. The last file of each done
file in the poll now deletes it, unless the poll kept it for files it left.
BATCH_COMPLETE is unchanged. Without recursive=true the last file of the
poll is the only one, as before.
Found with a TLA+ model.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
---
.../apache/camel/catalog/docs/file-component.adoc | 3 +-
.../camel-file/src/main/docs/file-component.adoc | 3 +-
.../camel/component/file/GenericFileConsumer.java | 40 +++++++++++
.../component/file/GenericFileOnCompletion.java | 15 +++-
.../FileConsumerDoneFileNameRecursiveTest.java | 84 ++++++++++++++++++++++
.../ROOT/pages/camel-4x-upgrade-guide-4_24.adoc | 11 +++
6 files changed, 152 insertions(+), 4 deletions(-)
diff --git
a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/file-component.adoc
b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/file-component.adoc
index b7e16443dee6..a6a9da95f917 100644
---
a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/file-component.adoc
+++
b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/file-component.adoc
@@ -804,7 +804,8 @@ When a poll leaves files of the directory for the next poll
(beyond
`maxMessagesPerPoll`, or not started, for example when the read lock is not
acquired), the _done file_ is kept until they are consumed; meanwhile the
polls consume every file of the directory, as they do whenever the _done file_
-is there.
+is there. With `recursive=true` each directory has its own _done file_, which
+the last file of that directory in the poll deletes.
[NOTE]
====
diff --git a/components/camel-file/src/main/docs/file-component.adoc
b/components/camel-file/src/main/docs/file-component.adoc
index b7e16443dee6..a6a9da95f917 100644
--- a/components/camel-file/src/main/docs/file-component.adoc
+++ b/components/camel-file/src/main/docs/file-component.adoc
@@ -804,7 +804,8 @@ When a poll leaves files of the directory for the next poll
(beyond
`maxMessagesPerPoll`, or not started, for example when the read lock is not
acquired), the _done file_ is kept until they are consumed; meanwhile the
polls consume every file of the directory, as they do whenever the _done file_
-is there.
+is there. With `recursive=true` each directory has its own _done file_, which
+the last file of that directory in the poll deletes.
[NOTE]
====
diff --git
a/components/camel-file/src/main/java/org/apache/camel/component/file/GenericFileConsumer.java
b/components/camel-file/src/main/java/org/apache/camel/component/file/GenericFileConsumer.java
index 0f7aa946913a..3ded99ced1f5 100644
---
a/components/camel-file/src/main/java/org/apache/camel/component/file/GenericFileConsumer.java
+++
b/components/camel-file/src/main/java/org/apache/camel/component/file/GenericFileConsumer.java
@@ -18,8 +18,10 @@ package org.apache.camel.component.file;
import java.util.ArrayDeque;
import java.util.ArrayList;
+import java.util.BitSet;
import java.util.Collections;
import java.util.Deque;
+import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
@@ -71,6 +73,9 @@ public abstract class GenericFileConsumer<T> extends
ScheduledBatchPollingConsum
private final String[] includeExt;
private final String[] excludeExt;
private boolean retrieveFile = true;
+ // whether the exchange processBatch hands to processExchange is the last
of its done file in the batch (poll thread);
+ // set by processBatch just before it calls processExchange, and read there
+ private boolean lastOfDoneFile;
// files already reported as resolving outside the starting directory
(bounded)
private final Map<String, Boolean> skippedOutsideStartingDirectory =
LRUCacheFactory.newLRUCache(1000);
// the done files with a fixed name kept for the files a poll left (null
if the endpoint has no such done file)
@@ -286,6 +291,7 @@ public abstract class GenericFileConsumer<T> extends
ScheduledBatchPollingConsum
}
Queue<Object> notStarted = new ArrayDeque<>();
+ BitSet lastOfEachDoneFile = lastOfEachDoneFile(exchanges, total);
for (int index = 0; index < total && isBatchAllowed(); index++) {
// only loop if we are started (allowed to run)
@@ -299,6 +305,7 @@ public abstract class GenericFileConsumer<T> extends
ScheduledBatchPollingConsum
// update pending number of exchanges
pendingExchanges = total - index - 1;
+ lastOfDoneFile = lastOfEachDoneFile != null &&
lastOfEachDoneFile.get(index);
// the file as polled: the begin strategy may pre move it, and
then binds the pre moved file to the exchange
GenericFile<?> polledFile = getExchangeFileProperty(exchange);
@@ -335,6 +342,36 @@ public abstract class GenericFileConsumer<T> extends
ScheduledBatchPollingConsum
return answer;
}
+ /**
+ * With a done file with a fixed name, the batch index of the last file of
each done file among the files of the
+ * batch: that file deletes the done file once it completed. With {@code
recursive=true} the files of a poll come
+ * from several directories, each with its own done file, not only the
directory of the last file of the poll. Only
+ * the indexes are kept, not the exchanges, so an exchange that completed
is not kept reachable for the rest of the
+ * poll.
+ *
+ * @return the batch indexes, or <tt>null</tt> when the done files are not
kept (no done file with a fixed name)
+ */
+ private BitSet lastOfEachDoneFile(Queue<Object> exchanges, int total) {
+ if (getKeptDoneFiles() == null) {
+ return null;
+ }
+ Map<String, Integer> last = new HashMap<>();
+ int index = 0;
+ for (Object exchange : exchanges) {
+ if (index >= total) {
+ break;
+ }
+ GenericFile<?> file = getExchangeFileProperty((Exchange) exchange);
+ last.put(endpoint.createDoneFileName(file.getAbsoluteFilePath()),
index);
+ index++;
+ }
+ BitSet answer = new BitSet(total);
+ for (int i : last.values()) {
+ answer.set(i);
+ }
+ return answer;
+ }
+
/**
* Drain any in progress files as we are done with this batch
*
@@ -611,6 +648,9 @@ public abstract class GenericFileConsumer<T> extends
ScheduledBatchPollingConsum
GenericFileKeptDoneFiles kept = getKeptDoneFiles();
if (kept != null) {
onCompletion.setKeptDoneFiles(kept.batch());
+ // the value set by processBatch for this exchange: this
relies on processExchange being called from
+ // inside processBatch, on the poll thread, as the field is
not passed as a parameter
+ onCompletion.setLastOfDoneFile(lastOfDoneFile);
}
exchange.getExchangeExtension().addOnCompletion(onCompletion);
diff --git
a/components/camel-file/src/main/java/org/apache/camel/component/file/GenericFileOnCompletion.java
b/components/camel-file/src/main/java/org/apache/camel/component/file/GenericFileOnCompletion.java
index 9a8bd1531466..076603e7e6aa 100644
---
a/components/camel-file/src/main/java/org/apache/camel/component/file/GenericFileOnCompletion.java
+++
b/components/camel-file/src/main/java/org/apache/camel/component/file/GenericFileOnCompletion.java
@@ -41,6 +41,7 @@ public class GenericFileOnCompletion<T> implements
Synchronization {
private final GenericFile<T> file;
private final String absoluteFileName;
private GenericFileKeptDoneFiles.Batch keptDoneFiles;
+ private boolean lastOfDoneFile;
public GenericFileOnCompletion(GenericFileEndpoint<T> endpoint,
GenericFileOperations<T> operations,
GenericFileProcessStrategy processStrategy,
GenericFile<T> file,
@@ -74,6 +75,14 @@ public class GenericFileOnCompletion<T> implements
Synchronization {
this.keptDoneFiles = keptDoneFiles;
}
+ /**
+ * Whether the file is the last of its done file with a fixed name among
the files of its poll (with
+ * {@code recursive=true} one per directory), which then deletes the done
file, unless it is kept.
+ */
+ void setLastOfDoneFile(boolean lastOfDoneFile) {
+ this.lastOfDoneFile = lastOfDoneFile;
+ }
+
public ExceptionHandler getExceptionHandler() {
return exceptionHandler;
}
@@ -215,9 +224,11 @@ public class GenericFileOnCompletion<T> implements
Synchronization {
}
protected void handleDoneFile(Exchange exchange) {
- // must be last in batch to delete the done file name
+ // must be last in batch to delete the done file name, or with a done
file with a fixed name the last file of that
+ // done file in the batch (with recursive=true each directory has its
own done file)
// delete done file if used (and not noop=true)
- boolean complete =
exchange.getProperty(ExchangePropertyKey.BATCH_COMPLETE, false, Boolean.class);
+ boolean complete = keptDoneFiles != null
+ ? lastOfDoneFile :
exchange.getProperty(ExchangePropertyKey.BATCH_COMPLETE, false, Boolean.class);
if (endpoint.getDoneFileName() != null && !endpoint.isNoop()) {
// done file must be in same path as the original input file
String doneFileName =
endpoint.createDoneFileName(absoluteFileName);
diff --git
a/core/camel-core/src/test/java/org/apache/camel/component/file/FileConsumerDoneFileNameRecursiveTest.java
b/core/camel-core/src/test/java/org/apache/camel/component/file/FileConsumerDoneFileNameRecursiveTest.java
new file mode 100644
index 000000000000..fd71ad6211c2
--- /dev/null
+++
b/core/camel-core/src/test/java/org/apache/camel/component/file/FileConsumerDoneFileNameRecursiveTest.java
@@ -0,0 +1,84 @@
+/*
+ * 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;
+
+import java.nio.file.Files;
+import java.util.List;
+import java.util.concurrent.TimeUnit;
+
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.builder.RouteBuilder;
+import org.awaitility.Awaitility;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+/**
+ * With recursive=true and a done file with a fixed name, each directory has
its own done file: a poll that consumes the
+ * files of several directories deletes the done file of each of them, not
only the one of the directory of its last
+ * file.
+ */
+class FileConsumerDoneFileNameRecursiveTest extends ContextTestSupport {
+
+ private static final List<String> DIRECTORIES = List.of("d1", "d2", "d3");
+
+ @Test
+ void pollOfSeveralDirectoriesDeletesTheDoneFileOfEach() throws Exception {
+ for (String directory : DIRECTORIES) {
+ Files.createDirectories(testDirectory(directory));
+ Files.writeString(testFile(directory + "/" + directory + ".txt"),
directory);
+ Files.writeString(testFile(directory + "/ready"), "");
+ }
+ getMockEndpoint("mock:result").expectedBodiesReceivedInAnyOrder("d1",
"d2", "d3");
+
+ context.getRouteController().startRoute("files");
+
+ assertMockEndpointsSatisfied();
+ // the files are moved once their done file was handled
+ Awaitility.await().atMost(20, TimeUnit.SECONDS).until(() ->
DIRECTORIES.stream()
+ .noneMatch(directory -> Files.exists(testFile(directory + "/"
+ directory + ".txt"))));
+ for (String directory : DIRECTORIES) {
+ assertFalse(Files.exists(testFile(directory + "/ready")),
+ "The done file of " + directory + " must be deleted after
its last file");
+ }
+
+ // a file written into one of the directories waits for a new done
file (the mock is reset before the files are
+ // written, as a poll can deliver the file at once)
+ getMockEndpoint("mock:result").reset();
+ getMockEndpoint("mock:result").expectedBodiesReceived("other");
+ Files.writeString(testFile("d1/next.txt"), "next");
+ Files.writeString(testFile("d2/other.txt"), "other");
+ Files.writeString(testFile("d2/ready"), "");
+ assertMockEndpointsSatisfied();
+ Awaitility.await().atMost(20, TimeUnit.SECONDS).until(() ->
!Files.exists(testFile("d2/ready")));
+ assertTrue(Files.exists(testFile("d1/next.txt")), "d1/next.txt has no
done file and must not be consumed");
+ }
+
+ @Override
+ protected RouteBuilder createRouteBuilder() {
+ return new RouteBuilder() {
+ @Override
+ public void configure() {
+
from(fileUri("?doneFileName=ready&recursive=true&initialDelay=0&delay=10"))
+ .routeId("files").autoStartup(false)
+ .convertBodyTo(String.class)
+ .to("mock:result");
+ }
+ };
+ }
+}
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 11b40bceb3e5..5c325b92ad14 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
@@ -3647,6 +3647,17 @@ also applies to `pollEnrich` and
`ConsumerTemplate.receive`, which take one file
until the last file of the directory is received, where before the first
received file deleted it. This applies to
camel-ftp, camel-sftp, camel-smb and camel-azure-files as well.
+=== camel-file, camel-ftp, camel-sftp, camel-mina-sftp, camel-smb,
camel-azure-files - with recursive=true the done file of each directory is
deleted
+
+No option or default changes. With `recursive=true` and a done file with a
fixed name (such as `doneFileName=ready`),
+a poll that consumes files of several directories deletes the done file of
each of these directories after the last
+file of that directory, as documented. Before, it deleted only the done file
of the directory of the last file of the
+poll, and the done files of the other directories stayed, so that the next
files written there were consumed without
+a new done file. So a file of such a directory that failed in the poll before
the last file of that directory (and was
+not moved with `moveFailed`) is now consumed again only after a new done file
is written, as it already was in the
+directory of the last file of the poll. A done file is still kept while the
poll leaves files of its directory for the
+next poll, and a dynamic done file name (`${file:name}.done`) is deleted for
each file as before.
+
=== camel-file - poll and pollEnrich evaluate the fileName on the current
message
`poll` and `pollEnrich` with a file based component (file, ftp, ftps, sftp,
smb and azure-files) evaluate a dynamic