You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2026/04/10 15:43:22 UTC

(camel) branch main updated: CAMEL-23267: Use simple LinkedHashMap in MemoryIdempotentRepository which is used as in progress repo for file based components (#22547)

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 b9779a2a4fec CAMEL-23267: Use simple LinkedHashMap in MemoryIdempotentRepository which is used as in progress repo for file based components (#22547)
b9779a2a4fec is described below

commit b9779a2a4fec7a200570cd4e1c5f7b01b5421624
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Fri Apr 10 17:43:16 2026 +0200

    CAMEL-23267: Use simple LinkedHashMap in MemoryIdempotentRepository which is used as in progress repo for file based components (#22547)
    
    * CAMEL-23267: Use a LinkedHashMap as inProgressRepository for file component
    
    * CAMEL-23267: Fill test assertion gap and set maximum cache size field
    
    * CAMEL-23267: Use simple LinkedHashMap as inProgressRepository in AWS2S3 and IBMCOS endpoints
    
    * CAMEL-23267: Use simple LinkedHashMap as inProgressRepository
    
    ---------
    
    Co-authored-by: tga <th...@clavisit.com>
    Co-authored-by: thomas-gantenbein-tga <32...@users.noreply.github.com>
---
 .../camel/component/file/GenericFileEndpoint.java  |  2 +-
 .../idempotent/MemoryIdempotentRepository.java     | 21 +++++++---
 .../support/MemoryIdempotentRepositoryTest.java    | 49 ++++++++++++++++++++++
 .../ROOT/pages/camel-4x-upgrade-guide-4_19.adoc    |  5 +++
 4 files changed, 70 insertions(+), 7 deletions(-)

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..004100d46d8d 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
@@ -1502,7 +1502,7 @@ public abstract class GenericFileEndpoint<T> extends ScheduledPollEndpoint imple
 
     /**
      * A pluggable in-progress repository org.apache.camel.spi.IdempotentRepository. The in-progress repository is used
-     * to account the current in progress files being consumed. By default a memory based repository is used.
+     * to account the current in progress files being consumed. By default, a memory based repository is used.
      */
     public void setInProgressRepository(IdempotentRepository inProgressRepository) {
         this.inProgressRepository = inProgressRepository;
diff --git a/core/camel-support/src/main/java/org/apache/camel/support/processor/idempotent/MemoryIdempotentRepository.java b/core/camel-support/src/main/java/org/apache/camel/support/processor/idempotent/MemoryIdempotentRepository.java
index bb50b4d88633..4cffa19e8228 100644
--- a/core/camel-support/src/main/java/org/apache/camel/support/processor/idempotent/MemoryIdempotentRepository.java
+++ b/core/camel-support/src/main/java/org/apache/camel/support/processor/idempotent/MemoryIdempotentRepository.java
@@ -16,7 +16,9 @@
  */
 package org.apache.camel.support.processor.idempotent;
 
+import java.util.LinkedHashMap;
 import java.util.Map;
+import java.util.Map.Entry;
 import java.util.concurrent.locks.Lock;
 import java.util.concurrent.locks.ReentrantLock;
 
@@ -26,8 +28,6 @@ import org.apache.camel.api.management.ManagedResource;
 import org.apache.camel.spi.Configurer;
 import org.apache.camel.spi.IdempotentRepository;
 import org.apache.camel.spi.Metadata;
-import org.apache.camel.support.LRUCache;
-import org.apache.camel.support.LRUCacheFactory;
 import org.apache.camel.support.service.ServiceHelper;
 import org.apache.camel.support.service.ServiceSupport;
 
@@ -49,7 +49,7 @@ public class MemoryIdempotentRepository extends ServiceSupport implements Idempo
     private final Lock cacheAndStoreLock = new ReentrantLock();
 
     @Metadata(description = "Maximum elements that can be stored in-memory", defaultValue = "" + MAX_CACHE_SIZE)
-    private int cacheSize;
+    private int cacheSize = MAX_CACHE_SIZE;
 
     public MemoryIdempotentRepository() {
     }
@@ -59,14 +59,18 @@ public class MemoryIdempotentRepository extends ServiceSupport implements Idempo
     }
 
     /**
-     * Creates a new memory based repository using a {@link LRUCache} with a default of 1000 entries in the cache.
+     * Creates a new memory based repository using a {@link java.util.LinkedHashMap} as its store, with 1000 maximum
+     * capacity. When a new entry is added and the store has reached its maximum capacity, the oldest entry is removed.
+     *
      */
     public static IdempotentRepository memoryIdempotentRepository() {
         return memoryIdempotentRepository(MAX_CACHE_SIZE);
     }
 
     /**
-     * Creates a new memory based repository using a {@link LRUCache}.
+     * Creates a new memory based repository using a {@link java.util.LinkedHashMap} as its store, with the given
+     * maximum capacity. When a new entry is added and the store has reached its maximum capacity, the oldest entry is
+     * removed.
      *
      * @param cacheSize the cache size
      */
@@ -164,7 +168,12 @@ public class MemoryIdempotentRepository extends ServiceSupport implements Idempo
     @Override
     protected void doStart() throws Exception {
         if (cache == null) {
-            cache = LRUCacheFactory.newLRUCache(cacheSize <= 0 ? MAX_CACHE_SIZE : cacheSize);
+            cache = new LinkedHashMap<>() {
+                @Override
+                protected boolean removeEldestEntry(Entry<String, Object> eldest) {
+                    return size() > cacheSize;
+                }
+            };
         }
     }
 
diff --git a/core/camel-support/src/test/java/org/apache/camel/support/MemoryIdempotentRepositoryTest.java b/core/camel-support/src/test/java/org/apache/camel/support/MemoryIdempotentRepositoryTest.java
new file mode 100644
index 000000000000..ef843bd330ca
--- /dev/null
+++ b/core/camel-support/src/test/java/org/apache/camel/support/MemoryIdempotentRepositoryTest.java
@@ -0,0 +1,49 @@
+/*
+ * 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.support;
+
+import java.io.IOException;
+
+import org.apache.camel.spi.IdempotentRepository;
+import org.apache.camel.support.processor.idempotent.MemoryIdempotentRepository;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+class MemoryIdempotentRepositoryTest {
+
+    @Test
+    void repositoryEvictsOldestEntryWhenRepositoryIsFull() throws IOException {
+        final int cacheSize = 5;
+        final int entriesNotFittingInRepository = 4;
+        try (IdempotentRepository repository = MemoryIdempotentRepository.memoryIdempotentRepository(
+                cacheSize)) {
+
+            for (int i = 0; i < cacheSize + entriesNotFittingInRepository; i++) {
+                repository.add(String.valueOf(i));
+            }
+
+            for (int i = entriesNotFittingInRepository; i < cacheSize + entriesNotFittingInRepository; i++) {
+                assertTrue(repository.contains(String.valueOf(i)), "Repository should contain entry " + i);
+            }
+            for (int i = 0; i < entriesNotFittingInRepository; i++) {
+                assertFalse(repository.contains(String.valueOf(i)), "Repository should not contain entry " + i);
+            }
+        }
+    }
+}
diff --git a/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_19.adoc b/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_19.adoc
index a1cc5627cccd..16b6d90b1f38 100644
--- a/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_19.adoc
+++ b/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_19.adoc
@@ -27,6 +27,11 @@ The cloud integration from the following components has been removed: `camel-con
 
 === camel-core
 
+==== MemoryIdempotentRepository
+
+The `MemoryIdempotentRepository` is no longer based on `LRUCache` but uses a `LinkedHashMap` as the repository
+has no need for LRU semantics and the `LinkedHashMap` is more light-weight.
+
 ==== PQC TLS Auto-Configuration
 
 When running on a JDK that supports `X25519MLKEM768` (typically JDK 25+), Camel now automatically