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 247712e52cfd CAMEL-27112: FileIdempotentRepository and
FileStateRepository - accept the file name as a plain String (#30282)
247712e52cfd is described below
commit 247712e52cfdfc5a2dd47868a27d771bb2b7df3c
Author: Claus Ibsen <[email protected]>
AuthorDate: Tue Nov 17 17:43:56 2026 +0100
CAMEL-27112: FileIdempotentRepository and FileStateRepository - accept the
file name as a plain String (#30282)
FileIdempotentRepository and FileStateRepository took their file only as a
java.io.File, so declaring one as a bean in YAML or properties with a file name
failed with NoTypeConversionAvailableException (String to java.io.File).
- New fileName option (setFileName(String) / getFileName()) on both
repositories, plus String variants of the static factory methods. fileStore
(java.io.File) is unchanged; either option must be set. In the bean catalog,
fileStore is no longer required.
- No general String to File type converter, as the ticket asks. An
overloaded setFileStore(String) would not work with the generated configurer,
and would break the #class:java.io.File(...) workaround.
- Docs: the file component's file-based idempotent repository section
(package, 32mb limit, Java and YAML examples), the Idempotent Consumer EIP
page, the enrich-and-dedupe CLI example README and a YAML validator hint now
use fileName.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
---
.../catalog/beans/FileIdempotentRepository.json | 2 +-
.../apache/camel/catalog/docs/file-component.adoc | 56 +++++++++++++---
.../camel/catalog/docs/idempotentConsumer-eip.adoc | 4 +-
.../camel-file/src/main/docs/file-component.adoc | 56 +++++++++++++---
.../modules/eips/pages/idempotentConsumer-eip.adoc | 4 +-
.../processor/state/FileStateRepositoryTest.java | 54 +++++++++++++++
.../FileIdempotentRepositoryConfigurerTest.java | 53 +++++++++++++++
.../FileIdempotentRepositoryConfigurer.java | 6 ++
.../camel/bean/FileIdempotentRepository.json | 2 +-
.../idempotent/FileIdempotentRepository.java | 77 +++++++++++++++++++++-
.../processor/state/FileStateRepository.java | 58 +++++++++++++++-
.../examples/route/enrich-and-dedupe/README.md | 2 +-
.../camel/dsl/yaml/validator/SchemaHints.java | 2 +-
.../java/org/apache/camel/dsl/yaml/BeansTest.java | 19 ++++++
14 files changed, 368 insertions(+), 27 deletions(-)
diff --git
a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/beans/FileIdempotentRepository.json
b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/beans/FileIdempotentRepository.json
index 5191d466f59c..919b708c3ae3 100644
---
a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/beans/FileIdempotentRepository.json
+++
b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/beans/FileIdempotentRepository.json
@@ -10,7 +10,7 @@
"groupId": "org.apache.camel",
"artifactId": "camel-support",
"version": "4.24.0-SNAPSHOT",
- "properties": { "cacheSize": { "index": 0, "kind": "property",
"displayName": "Cache Size", "required": false, "type": "integer", "javaType":
"int", "deprecated": false, "autowired": false, "secret": false,
"defaultValue": 1000, "description": "The maximum size of the 1st-level
in-memory cache" }, "fileStore": { "index": 1, "kind": "property",
"displayName": "File Store", "required": true, "type": "string", "javaType":
"java.io.File", "deprecated": false, "autowired": false, "secret" [...]
+ "properties": { "cacheSize": { "index": 0, "kind": "property",
"displayName": "Cache Size", "required": false, "type": "integer", "javaType":
"int", "deprecated": false, "autowired": false, "secret": false,
"defaultValue": 1000, "description": "The maximum size of the 1st-level
in-memory cache" }, "fileName": { "index": 1, "kind": "property",
"displayName": "File Name", "required": false, "type": "string", "javaType":
"java.lang.String", "deprecated": false, "autowired": false, "secr [...]
}
}
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 8a38c1d24a7c..b7e16443dee6 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
@@ -1228,7 +1228,7 @@ DEBUG FileConsumer is idempotent and the file has been
consumed before. Will ski
==== Using a file-based idempotent repository
In this section we will use the file-based idempotent repository
-`org.apache.camel.processor.idempotent.FileIdempotentRepository` instead
+`org.apache.camel.support.processor.idempotent.FileIdempotentRepository`
instead
of the in-memory one that is used as default.
This repository uses a first level cache to avoid reading the file
repository. It will only use the file repository to store the content of
@@ -1236,14 +1236,52 @@ the first level cache. Thereby, the repository can
survive server restarts.
It will load the content of the file into the first level cache upon
startup. The file structure is basic as it stores the key in
separate lines in the file. By default, the file store has a size limit
-of 1mb. When the file grows larger, Camel will truncate the file store,
-rebuilding the content by flushing the first level cache into a fresh
-empty file.
-
-We configure our repository using Spring XML creating our file
-idempotent repository and define our file consumer to use our repository
-with the `idempotentRepository` using `#` sign to indicate
-Registry lookup:
+of 32mb. When the file grows larger, Camel drops the oldest entries from
+the file store (1000 by default) to make room for new ones.
+
+We declare the repository as a bean with the name of its file (`fileName`),
and define our file consumer to use
+the repository with the `idempotentRepository` option, using `#bean:` to look
it up in the Registry. After a restart,
+the files that were read before are skipped, and only the new files are read:
+
+[tabs]
+====
+Java::
++
+[source,java]
+----
+@BindToRegistry("readFiles")
+public IdempotentRepository readFiles() {
+ return
FileIdempotentRepository.fileIdempotentRepository("/var/data/read-files.dat");
+}
+
+@Override
+public void configure() {
+ from("file:/var/data/in?noop=true&idempotentRepository=#bean:readFiles")
+ .log("${header.CamelFileName}");
+}
+----
+
+YAML::
++
+[source,yaml]
+----
+- beans:
+ - name: readFiles
+ type:
"#class:org.apache.camel.support.processor.idempotent.FileIdempotentRepository"
+ properties:
+ fileName: /var/data/read-files.dat
+- route:
+ from:
+ uri: file:/var/data/in
+ parameters:
+ noop: true
+ idempotentRepository: "#bean:readFiles"
+ steps:
+ - log: "${header.CamelFileName}"
+----
+====
+
+The `fileName` property of the repository takes the file name as plain text.
Use `fileStore` instead when you have a `java.io.File`.
==== Using a JPA based idempotent repository
diff --git
a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/idempotentConsumer-eip.adoc
b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/idempotentConsumer-eip.adoc
index 8510903d15f4..da7c8cb5f4ed 100644
---
a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/idempotentConsumer-eip.adoc
+++
b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/idempotentConsumer-eip.adoc
@@ -118,7 +118,9 @@ YAML::
====
The memory repository forgets the ids when the application stops. To keep
them, use `FileIdempotentRepository` or one
-of the repositories listed above.
+of the repositories listed above. `FileIdempotentRepository` keeps the ids in
a file, which you name with its `fileName`
+property, such as when you declare the repository as a bean in YAML. The
+xref:ROOT:file-component.adoc#_using_a_file_based_idempotent_repository[File]
component has an example.
=== Handling the duplicates in the route
diff --git a/components/camel-file/src/main/docs/file-component.adoc
b/components/camel-file/src/main/docs/file-component.adoc
index 8a38c1d24a7c..b7e16443dee6 100644
--- a/components/camel-file/src/main/docs/file-component.adoc
+++ b/components/camel-file/src/main/docs/file-component.adoc
@@ -1228,7 +1228,7 @@ DEBUG FileConsumer is idempotent and the file has been
consumed before. Will ski
==== Using a file-based idempotent repository
In this section we will use the file-based idempotent repository
-`org.apache.camel.processor.idempotent.FileIdempotentRepository` instead
+`org.apache.camel.support.processor.idempotent.FileIdempotentRepository`
instead
of the in-memory one that is used as default.
This repository uses a first level cache to avoid reading the file
repository. It will only use the file repository to store the content of
@@ -1236,14 +1236,52 @@ the first level cache. Thereby, the repository can
survive server restarts.
It will load the content of the file into the first level cache upon
startup. The file structure is basic as it stores the key in
separate lines in the file. By default, the file store has a size limit
-of 1mb. When the file grows larger, Camel will truncate the file store,
-rebuilding the content by flushing the first level cache into a fresh
-empty file.
-
-We configure our repository using Spring XML creating our file
-idempotent repository and define our file consumer to use our repository
-with the `idempotentRepository` using `#` sign to indicate
-Registry lookup:
+of 32mb. When the file grows larger, Camel drops the oldest entries from
+the file store (1000 by default) to make room for new ones.
+
+We declare the repository as a bean with the name of its file (`fileName`),
and define our file consumer to use
+the repository with the `idempotentRepository` option, using `#bean:` to look
it up in the Registry. After a restart,
+the files that were read before are skipped, and only the new files are read:
+
+[tabs]
+====
+Java::
++
+[source,java]
+----
+@BindToRegistry("readFiles")
+public IdempotentRepository readFiles() {
+ return
FileIdempotentRepository.fileIdempotentRepository("/var/data/read-files.dat");
+}
+
+@Override
+public void configure() {
+ from("file:/var/data/in?noop=true&idempotentRepository=#bean:readFiles")
+ .log("${header.CamelFileName}");
+}
+----
+
+YAML::
++
+[source,yaml]
+----
+- beans:
+ - name: readFiles
+ type:
"#class:org.apache.camel.support.processor.idempotent.FileIdempotentRepository"
+ properties:
+ fileName: /var/data/read-files.dat
+- route:
+ from:
+ uri: file:/var/data/in
+ parameters:
+ noop: true
+ idempotentRepository: "#bean:readFiles"
+ steps:
+ - log: "${header.CamelFileName}"
+----
+====
+
+The `fileName` property of the repository takes the file name as plain text.
Use `fileStore` instead when you have a `java.io.File`.
==== Using a JPA based idempotent repository
diff --git
a/core/camel-core-engine/src/main/docs/modules/eips/pages/idempotentConsumer-eip.adoc
b/core/camel-core-engine/src/main/docs/modules/eips/pages/idempotentConsumer-eip.adoc
index 8510903d15f4..da7c8cb5f4ed 100644
---
a/core/camel-core-engine/src/main/docs/modules/eips/pages/idempotentConsumer-eip.adoc
+++
b/core/camel-core-engine/src/main/docs/modules/eips/pages/idempotentConsumer-eip.adoc
@@ -118,7 +118,9 @@ YAML::
====
The memory repository forgets the ids when the application stops. To keep
them, use `FileIdempotentRepository` or one
-of the repositories listed above.
+of the repositories listed above. `FileIdempotentRepository` keeps the ids in
a file, which you name with its `fileName`
+property, such as when you declare the repository as a bean in YAML. The
+xref:ROOT:file-component.adoc#_using_a_file_based_idempotent_repository[File]
component has an example.
=== Handling the duplicates in the route
diff --git
a/core/camel-core/src/test/java/org/apache/camel/support/processor/state/FileStateRepositoryTest.java
b/core/camel-core/src/test/java/org/apache/camel/support/processor/state/FileStateRepositoryTest.java
index 896787ae36af..6cb9451e0bb4 100644
---
a/core/camel-core/src/test/java/org/apache/camel/support/processor/state/FileStateRepositoryTest.java
+++
b/core/camel-core/src/test/java/org/apache/camel/support/processor/state/FileStateRepositoryTest.java
@@ -33,7 +33,10 @@ import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference;
+import org.apache.camel.CamelContext;
import org.apache.camel.TestSupport;
+import org.apache.camel.impl.DefaultCamelContext;
+import org.apache.camel.support.PropertyBindingSupport;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.DisabledOnOs;
@@ -57,6 +60,57 @@ public class FileStateRepositoryTest extends TestSupport {
repositoryStore = testFile("file-state-repository.dat").toFile();
}
+ @Test
+ public void shouldBindFileNameAsString() throws Exception {
+ // Given a FileStateRepository declared as a bean, with the file name
as a plain String
+ FileStateRepository repository = new FileStateRepository();
+ try (CamelContext context = new DefaultCamelContext()) {
+ boolean hit = PropertyBindingSupport.build()
+ .withCamelContext(context)
+ .withTarget(repository)
+ .withProperty("fileName", repositoryStore.getPath())
+ .bind();
+ assertTrue(hit);
+ }
+ assertEquals(repositoryStore, repository.getFileStore());
+ assertEquals(repositoryStore.getPath(), repository.getFileName());
+
+ // When the state is stored
+ repository.start();
+ repository.setState("key", "value");
+ repository.stop();
+
+ // Then a new repository with the same file name reads it again
+ FileStateRepository other =
fileStateRepository(repositoryStore.getPath());
+ other.start();
+ assertEquals("value", other.getState("key"));
+ other.stop();
+ }
+
+ @Test
+ public void shouldBindFileStoreAsFile() throws Exception {
+ // Given a FileStateRepository declared as a bean, with the file store
as a java.io.File
+ FileStateRepository repository = new FileStateRepository();
+ try (CamelContext context = new DefaultCamelContext()) {
+ boolean hit = PropertyBindingSupport.build()
+ .withCamelContext(context)
+ .withTarget(repository)
+ .withProperty("fileStore", "#class:java.io.File('" +
repositoryStore.getPath() + "')")
+ .bind();
+ assertTrue(hit);
+ }
+ assertEquals(repositoryStore, repository.getFileStore());
+ assertEquals(repositoryStore.getPath(), repository.getFileName());
+ }
+
+ @Test
+ public void shouldFailToStartWithoutFile() {
+ FileStateRepository repository = new FileStateRepository();
+
+ Exception e = assertThrows(IllegalArgumentException.class,
repository::start);
+ assertTrue(e.getMessage().contains("fileStore or fileName"),
e.getMessage());
+ }
+
@Test
public void shouldPreventUsingDelimiterInKey() {
// Given a FileStateRepository
diff --git
a/core/camel-main/src/test/java/org/apache/camel/main/FileIdempotentRepositoryConfigurerTest.java
b/core/camel-main/src/test/java/org/apache/camel/main/FileIdempotentRepositoryConfigurerTest.java
index 7af0797f3751..86e05055b78e 100644
---
a/core/camel-main/src/test/java/org/apache/camel/main/FileIdempotentRepositoryConfigurerTest.java
+++
b/core/camel-main/src/test/java/org/apache/camel/main/FileIdempotentRepositoryConfigurerTest.java
@@ -16,6 +16,8 @@
*/
package org.apache.camel.main;
+import java.io.File;
+
import org.apache.camel.CamelContext;
import org.apache.camel.impl.DefaultCamelContext;
import org.apache.camel.spi.BeanIntrospection;
@@ -60,4 +62,55 @@ public class FileIdempotentRepositoryConfigurerTest {
context.stop();
}
+ @Test
+ public void testFileName() {
+ CamelContext context = new DefaultCamelContext();
+
+ BeanIntrospection bi = PluginHelper.getBeanIntrospection(context);
+ bi.setExtendedStatistics(true);
+
+ context.start();
+
+ FileIdempotentRepository target = new FileIdempotentRepository();
+
+ // the file name is a plain String, as when the repository is a bean
in YAML or properties
+ boolean hit = PropertyBindingSupport.build()
+ .withCamelContext(context)
+ .withTarget(target)
+ .withReflection(false)
+ .withProperty("fileName", "target/data/read-files.dat")
+ .withRemoveParameters(true).bind();
+
+ assertTrue(hit);
+
+ assertEquals("target/data/read-files.dat", target.getFileName());
+ assertEquals(new File("target/data/read-files.dat"),
target.getFileStore());
+
+ // will auto detect generated configurer so no reflection in use
+ assertEquals(0, bi.getInvokedCounter());
+
+ context.stop();
+ }
+
+ @Test
+ public void testFileStore() {
+ CamelContext context = new DefaultCamelContext();
+ context.start();
+
+ FileIdempotentRepository target = new FileIdempotentRepository();
+
+ boolean hit = PropertyBindingSupport.build()
+ .withCamelContext(context)
+ .withTarget(target)
+ .withProperty("fileStore",
"#class:java.io.File('target/data/read-files.dat')")
+ .withRemoveParameters(true).bind();
+
+ assertTrue(hit);
+
+ assertEquals(new File("target/data/read-files.dat"),
target.getFileStore());
+ assertEquals(new File("target/data/read-files.dat").getPath(),
target.getFileName());
+
+ context.stop();
+ }
+
}
diff --git
a/core/camel-support/src/generated/java/org/apache/camel/support/processor/idempotent/FileIdempotentRepositoryConfigurer.java
b/core/camel-support/src/generated/java/org/apache/camel/support/processor/idempotent/FileIdempotentRepositoryConfigurer.java
index 149b8d7b1cf0..1bcf81e4bf17 100644
---
a/core/camel-support/src/generated/java/org/apache/camel/support/processor/idempotent/FileIdempotentRepositoryConfigurer.java
+++
b/core/camel-support/src/generated/java/org/apache/camel/support/processor/idempotent/FileIdempotentRepositoryConfigurer.java
@@ -28,6 +28,8 @@ public class FileIdempotentRepositoryConfigurer extends
org.apache.camel.support
case "cacheSize": target.setCacheSize(property(camelContext,
int.class, value)); return true;
case "dropoldestfilestore":
case "dropOldestFileStore":
target.setDropOldestFileStore(property(camelContext, long.class, value));
return true;
+ case "filename":
+ case "fileName": target.setFileName(property(camelContext,
java.lang.String.class, value)); return true;
case "filestore":
case "fileStore": target.setFileStore(property(camelContext,
java.io.File.class, value)); return true;
case "maxfilestoresize":
@@ -43,6 +45,8 @@ public class FileIdempotentRepositoryConfigurer extends
org.apache.camel.support
case "cacheSize": return int.class;
case "dropoldestfilestore":
case "dropOldestFileStore": return long.class;
+ case "filename":
+ case "fileName": return java.lang.String.class;
case "filestore":
case "fileStore": return java.io.File.class;
case "maxfilestoresize":
@@ -59,6 +63,8 @@ public class FileIdempotentRepositoryConfigurer extends
org.apache.camel.support
case "cacheSize": return target.getCacheSize();
case "dropoldestfilestore":
case "dropOldestFileStore": return target.getDropOldestFileStore();
+ case "filename":
+ case "fileName": return target.getFileName();
case "filestore":
case "fileStore": return target.getFileStore();
case "maxfilestoresize":
diff --git
a/core/camel-support/src/generated/resources/META-INF/services/org/apache/camel/bean/FileIdempotentRepository.json
b/core/camel-support/src/generated/resources/META-INF/services/org/apache/camel/bean/FileIdempotentRepository.json
index 5191d466f59c..919b708c3ae3 100644
---
a/core/camel-support/src/generated/resources/META-INF/services/org/apache/camel/bean/FileIdempotentRepository.json
+++
b/core/camel-support/src/generated/resources/META-INF/services/org/apache/camel/bean/FileIdempotentRepository.json
@@ -10,7 +10,7 @@
"groupId": "org.apache.camel",
"artifactId": "camel-support",
"version": "4.24.0-SNAPSHOT",
- "properties": { "cacheSize": { "index": 0, "kind": "property",
"displayName": "Cache Size", "required": false, "type": "integer", "javaType":
"int", "deprecated": false, "autowired": false, "secret": false,
"defaultValue": 1000, "description": "The maximum size of the 1st-level
in-memory cache" }, "fileStore": { "index": 1, "kind": "property",
"displayName": "File Store", "required": true, "type": "string", "javaType":
"java.io.File", "deprecated": false, "autowired": false, "secret" [...]
+ "properties": { "cacheSize": { "index": 0, "kind": "property",
"displayName": "Cache Size", "required": false, "type": "integer", "javaType":
"int", "deprecated": false, "autowired": false, "secret": false,
"defaultValue": 1000, "description": "The maximum size of the 1st-level
in-memory cache" }, "fileName": { "index": 1, "kind": "property",
"displayName": "File Name", "required": false, "type": "string", "javaType":
"java.lang.String", "deprecated": false, "autowired": false, "secr [...]
}
}
diff --git
a/core/camel-support/src/main/java/org/apache/camel/support/processor/idempotent/FileIdempotentRepository.java
b/core/camel-support/src/main/java/org/apache/camel/support/processor/idempotent/FileIdempotentRepository.java
index 6ab8ca839c01..36ad3d142f69 100644
---
a/core/camel-support/src/main/java/org/apache/camel/support/processor/idempotent/FileIdempotentRepository.java
+++
b/core/camel-support/src/main/java/org/apache/camel/support/processor/idempotent/FileIdempotentRepository.java
@@ -71,7 +71,10 @@ public class FileIdempotentRepository extends ServiceSupport
implements Idempote
@Metadata(description = "The maximum size of the 1st-level in-memory
cache", defaultValue = "1000")
private int cacheSize;
- @Metadata(description = "File name of the repository (incl directory)",
required = true)
+ @Metadata(description = "File name of the repository (incl directory).
Either fileName or fileStore must be set.")
+ private String fileName;
+ @Metadata(description = "File of the repository (incl directory) as a
java.io.File. Either fileName or fileStore must be"
+ + " set.")
private File fileStore;
@Metadata(description = "The maximum file size for the file store in
bytes. The default value is 32mb",
defaultValue = "" + 32 * 1024 * 1000L)
@@ -86,6 +89,7 @@ public class FileIdempotentRepository extends ServiceSupport
implements Idempote
public FileIdempotentRepository(File fileStore, Map<String, Object> cache)
{
this.fileStore = fileStore;
+ this.fileName = fileStore != null ? fileStore.getPath() : null;
this.cache = cache;
}
@@ -140,6 +144,49 @@ public class FileIdempotentRepository extends
ServiceSupport implements Idempote
return new FileIdempotentRepository(store, cache);
}
+ /**
+ * Creates a new file based repository using a {@link LRUCache} as 1st
level cache with a default of 1000 entries in
+ * the cache.
+ *
+ * @param fileName the file name of the file store (incl directory)
+ */
+ public static IdempotentRepository fileIdempotentRepository(String
fileName) {
+ return fileIdempotentRepository(new File(fileName));
+ }
+
+ /**
+ * Creates a new file based repository using a {@link LRUCache} as 1st
level cache.
+ *
+ * @param fileName the file name of the file store (incl directory)
+ * @param cacheSize the cache size
+ */
+ public static IdempotentRepository fileIdempotentRepository(String
fileName, int cacheSize) {
+ return fileIdempotentRepository(new File(fileName), cacheSize);
+ }
+
+ /**
+ * Creates a new file based repository using a {@link LRUCache} as 1st
level cache.
+ *
+ * @param fileName the file name of the file store (incl directory)
+ * @param cacheSize the cache size
+ * @param maxFileStoreSize the max size in bytes for the filestore file
+ */
+ public static IdempotentRepository fileIdempotentRepository(String
fileName, int cacheSize, long maxFileStoreSize) {
+ return fileIdempotentRepository(new File(fileName), cacheSize,
maxFileStoreSize);
+ }
+
+ /**
+ * Creates a new file based repository using the given {@link
java.util.Map} as 1st level cache.
+ * <p>
+ * Care should be taken to use a suitable underlying {@link java.util.Map}
to avoid this class being a memory leak.
+ *
+ * @param fileName the file name of the file store (incl directory)
+ * @param cache the cache to use as 1st level cache
+ */
+ public static IdempotentRepository fileIdempotentRepository(String
fileName, Map<String, Object> cache) {
+ return fileIdempotentRepository(new File(fileName), cache);
+ }
+
@Override
@ManagedOperation(description = "Adds the key to the store")
public boolean add(String key) {
@@ -229,8 +276,34 @@ public class FileIdempotentRepository extends
ServiceSupport implements Idempote
return fileStore;
}
+ /**
+ * Sets the file of the repository (incl directory). This also sets the
file name to the path of the file.
+ *
+ * @param fileStore the file store, or {@code null} to clear the file
store and the file name
+ */
public void setFileStore(File fileStore) {
this.fileStore = fileStore;
+ this.fileName = fileStore != null ? fileStore.getPath() : null;
+ }
+
+ /**
+ * Gets the file name of the repository (incl directory).
+ *
+ * @return the file name, or {@code null} if no file store or file name is
set
+ */
+ public String getFileName() {
+ return fileName;
+ }
+
+ /**
+ * Sets the file name of the repository (incl directory), such as when the
repository is declared as a bean in YAML
+ * or properties. This also sets the file store to this file.
+ *
+ * @param fileName the file name, or {@code null} to clear the file name
and the file store
+ */
+ public void setFileName(String fileName) {
+ this.fileName = fileName;
+ this.fileStore = fileName != null ? new File(fileName) : null;
}
@ManagedAttribute(description = "The file path for the store")
@@ -537,7 +610,7 @@ public class FileIdempotentRepository extends
ServiceSupport implements Idempote
@Override
protected void doStart() throws Exception {
- ObjectHelper.notNull(fileStore, "fileStore", this);
+ ObjectHelper.notNull(fileStore, "fileStore or fileName", this);
if (this.cache == null) {
// default use a 1st level cache
diff --git
a/core/camel-support/src/main/java/org/apache/camel/support/processor/state/FileStateRepository.java
b/core/camel-support/src/main/java/org/apache/camel/support/processor/state/FileStateRepository.java
index 8259887496fd..16be36e623c5 100644
---
a/core/camel-support/src/main/java/org/apache/camel/support/processor/state/FileStateRepository.java
+++
b/core/camel-support/src/main/java/org/apache/camel/support/processor/state/FileStateRepository.java
@@ -106,6 +106,37 @@ public class FileStateRepository extends ServiceSupport
implements StateReposito
return new FileStateRepository(store, cache);
}
+ /**
+ * Creates a new file based repository using a {@link HashMap} as 1st
level cache.
+ *
+ * @param fileName the file name of the file store (incl directory)
+ */
+ public static FileStateRepository fileStateRepository(String fileName) {
+ return fileStateRepository(new File(fileName));
+ }
+
+ /**
+ * Creates a new file based repository using a {@link HashMap} as 1st
level cache.
+ *
+ * @param fileName the file name of the file store (incl directory)
+ * @param maxFileStoreSize the max size in bytes for the fileStore file
+ */
+ public static FileStateRepository fileStateRepository(String fileName,
long maxFileStoreSize) {
+ return fileStateRepository(new File(fileName), maxFileStoreSize);
+ }
+
+ /**
+ * Creates a new file based repository using the given {@link
java.util.Map} as 1st level cache.
+ * <p>
+ * Care should be taken to use a suitable underlying {@link java.util.Map}
to avoid this class being a memory leak.
+ *
+ * @param fileName the file name of the file store (incl directory)
+ * @param cache the cache to use as 1st level cache
+ */
+ public static FileStateRepository fileStateRepository(String fileName,
Map<String, String> cache) {
+ return fileStateRepository(new File(fileName), cache);
+ }
+
@Override
@ManagedOperation(description = "Adds the value of the given key to the
store")
public void setState(String key, String value) {
@@ -295,7 +326,7 @@ public class FileStateRepository extends ServiceSupport
implements StateReposito
@Override
protected void doStart() throws Exception {
- ObjectHelper.notNull(fileStore, "fileStore", this);
+ ObjectHelper.notNull(fileStore, "fileStore or fileName", this);
// init store if not loaded before
if (init.compareAndSet(false, true)) {
@@ -321,10 +352,35 @@ public class FileStateRepository extends ServiceSupport
implements StateReposito
return fileStore;
}
+ /**
+ * Sets the file of the repository (incl directory).
+ *
+ * @param fileStore the file store
+ */
public void setFileStore(File fileStore) {
this.fileStore = fileStore;
}
+ /**
+ * Gets the file name of the repository (incl directory). It is the path
of the file store, as this repository has
+ * no bean metadata that needs a field of its own (unlike {@code
FileIdempotentRepository}).
+ *
+ * @return the file name, or {@code null} if no file store is set
+ */
+ public String getFileName() {
+ return fileStore != null ? fileStore.getPath() : null;
+ }
+
+ /**
+ * Sets the file name of the repository (incl directory), such as when the
repository is declared as a bean in YAML
+ * or properties. This sets the file store to this file.
+ *
+ * @param fileName the file name, or {@code null} to clear the file store
+ */
+ public void setFileName(String fileName) {
+ this.fileStore = fileName != null ? new File(fileName) : null;
+ }
+
@ManagedAttribute(description = "The file path for the store")
public String getFilePath() {
return fileStore.getPath();
diff --git
a/dsl/camel-jbang/camel-jbang-core/src/main/resources/examples/route/enrich-and-dedupe/README.md
b/dsl/camel-jbang/camel-jbang-core/src/main/resources/examples/route/enrich-and-dedupe/README.md
index b684f52b1463..65ab27ed28ac 100644
---
a/dsl/camel-jbang/camel-jbang-core/src/main/resources/examples/route/enrich-and-dedupe/README.md
+++
b/dsl/camel-jbang/camel-jbang-core/src/main/resources/examples/route/enrich-and-dedupe/README.md
@@ -60,7 +60,7 @@ Ask your assistant, or type it yourself, one step at a time,
and run after each:
- `skipDuplicate: true`, the default: the duplicate is dropped without a word.
- Replace the in-memory repository with a file one,
- `org.apache.camel.support.processor.idempotent.FileIdempotentRepository`
with a `fileStore` property: the ids
+ `org.apache.camel.support.processor.idempotent.FileIdempotentRepository`
with a `fileName` property: the ids
then survive a restart, and on the next run every order is a duplicate.
- Add an order for a customer without a file: `pollEnrich` waits for its
`timeout` and the variable stays empty.
diff --git
a/dsl/camel-yaml-dsl/camel-yaml-dsl-validator/src/main/java/org/apache/camel/dsl/yaml/validator/SchemaHints.java
b/dsl/camel-yaml-dsl/camel-yaml-dsl-validator/src/main/java/org/apache/camel/dsl/yaml/validator/SchemaHints.java
index 080e10afbda4..6e68d58a70d6 100644
---
a/dsl/camel-yaml-dsl/camel-yaml-dsl-validator/src/main/java/org/apache/camel/dsl/yaml/validator/SchemaHints.java
+++
b/dsl/camel-yaml-dsl/camel-yaml-dsl-validator/src/main/java/org/apache/camel/dsl/yaml/validator/SchemaHints.java
@@ -879,7 +879,7 @@ final class SchemaHints {
// - name: repo / type: ... / parameters: {...}: the options of a
bean are its properties (CAMEL-26960)
unknownProperty(".*/beans/\\d+",
m -> hasName(m) && Set.of("parameters", "params",
"options", "args").contains(m.unknown()),
- m -> "what a bean is set up with goes under properties:
(its setters, properties: {fileStore: x})"
+ m -> "what a bean is set up with goes under properties:
(its setters, properties: {fileName: x})"
+ " or constructors: (the arguments of its
constructor, constructors: {0: x})"),
// - myBean: {type: ...} instead of - name: myBean / type: ...;
not for an item that has its name, where the
// key is one a bean does not have
diff --git
a/dsl/camel-yaml-dsl/camel-yaml-dsl/src/test/java/org/apache/camel/dsl/yaml/BeansTest.java
b/dsl/camel-yaml-dsl/camel-yaml-dsl/src/test/java/org/apache/camel/dsl/yaml/BeansTest.java
index d99692b595d0..5ea97dab7543 100644
---
a/dsl/camel-yaml-dsl/camel-yaml-dsl/src/test/java/org/apache/camel/dsl/yaml/BeansTest.java
+++
b/dsl/camel-yaml-dsl/camel-yaml-dsl/src/test/java/org/apache/camel/dsl/yaml/BeansTest.java
@@ -16,6 +16,7 @@
*/
package org.apache.camel.dsl.yaml;
+import java.io.File;
import java.util.Properties;
import org.apache.camel.dsl.yaml.support.YamlTestSupport;
@@ -26,6 +27,7 @@ import org.apache.camel.dsl.yaml.support.model.MyCtrBean;
import org.apache.camel.dsl.yaml.support.model.MyDestroyBean;
import org.apache.camel.dsl.yaml.support.model.MyFacBean;
import org.apache.camel.dsl.yaml.support.model.MyFacHelper;
+import org.apache.camel.support.processor.idempotent.FileIdempotentRepository;
import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.assertThat;
@@ -69,6 +71,23 @@ class BeansTest extends YamlTestSupport {
assertThat(props.getNested().getBar()).isEqualTo("nf2_p");
}
+ @Test
+ void beansWithFileName() throws Exception {
+ // CAMEL-27112: the file of a file idempotent repository is given as a
plain file name
+ loadRoutes("""
+ - beans:
+ - name: readFiles
+ type: "#class:%s"
+ properties:
+ fileName: "target/data/read-files.dat"
+ """.formatted(FileIdempotentRepository.class.getName()));
+
+ FileIdempotentRepository repo =
context.getRegistry().lookupByNameAndType("readFiles",
FileIdempotentRepository.class);
+ assertThat(repo).isNotNull();
+ assertThat(repo.getFileName()).isEqualTo("target/data/read-files.dat");
+ assertThat(repo.getFileStore()).isEqualTo(new
File("target/data/read-files.dat"));
+ }
+
@Test
void beansWithPlaceholders() throws Exception {
Properties initial = new Properties();