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 a7e696927de FileIdempotentRepository - Make configuring cacheSize more
standard via plain setter and creation in doStart
a7e696927de is described below
commit a7e696927dea30795a49a4c0ac4a36ee700131ff
Author: Claus Ibsen <[email protected]>
AuthorDate: Thu Mar 21 11:01:58 2024 +0100
FileIdempotentRepository - Make configuring cacheSize more standard via
plain setter and creation in doStart
---
.../catalog/beans/FileIdempotentRepository.json | 2 +-
.../file/FileProducerRenameUsingCopyTest.java | 11 ++++---
.../camel/bean/FileIdempotentRepository.json | 2 +-
.../idempotent/FileIdempotentRepository.java | 38 ++++++++--------------
.../idempotent/MemoryIdempotentRepository.java | 23 +++++++++----
.../throttling/ThrottlingExceptionRoutePolicy.java | 4 +--
6 files changed, 41 insertions(+), 39 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 43adaa9d4ca..a763ef9dcc1 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.5.0-SNAPSHOT",
- "properties": { "cacheSize": { "index": 0, "kind": "property",
"displayName": "Cache Size", "required": false, "type": "object", "javaType":
"org.apache.camel.support.processor.idempotent.FileIdempotentRepository",
"deprecated": false, "autowired": false, "secret": false, "defaultValue":
"1000", "description": "The size of the 1st-level in-memory cache" },
"fileStore": { "index": 1, "kind": "property", "displayName": "File Store",
"required": true, "type": "object", "javaType": "org. [...]
+ "properties": { "cacheSize": { "index": 0, "kind": "property",
"displayName": "Cache Size", "required": false, "type": "object", "javaType":
"org.apache.camel.support.processor.idempotent.FileIdempotentRepository",
"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": "object", "javaType [...]
}
}
diff --git
a/core/camel-core/src/test/java/org/apache/camel/component/file/FileProducerRenameUsingCopyTest.java
b/core/camel-core/src/test/java/org/apache/camel/component/file/FileProducerRenameUsingCopyTest.java
index c1d30d3028e..31630eabeda 100644
---
a/core/camel-core/src/test/java/org/apache/camel/component/file/FileProducerRenameUsingCopyTest.java
+++
b/core/camel-core/src/test/java/org/apache/camel/component/file/FileProducerRenameUsingCopyTest.java
@@ -16,12 +16,11 @@
*/
package org.apache.camel.component.file;
-import java.util.concurrent.TimeUnit;
-
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.awaitility.Awaitility;
import org.junit.jupiter.api.Test;
public class FileProducerRenameUsingCopyTest extends ContextTestSupport {
@@ -36,10 +35,12 @@ public class FileProducerRenameUsingCopyTest extends
ContextTestSupport {
template.sendBodyAndHeader(fileUri(), body, Exchange.FILE_NAME,
"hello.txt");
// wait a bit for the file move to be completed
- assertMockEndpointsSatisfied(1, TimeUnit.SECONDS);
+ assertMockEndpointsSatisfied();
- assertFileExists(testFile("done/hello.txt"));
- assertFileNotExists(testFile("hello.txt"));
+ Awaitility.await().untilAsserted(() -> {
+ assertFileExists(testFile("done/hello.txt"));
+ assertFileNotExists(testFile("hello.txt"));
+ });
}
@Override
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 43adaa9d4ca..a763ef9dcc1 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.5.0-SNAPSHOT",
- "properties": { "cacheSize": { "index": 0, "kind": "property",
"displayName": "Cache Size", "required": false, "type": "object", "javaType":
"org.apache.camel.support.processor.idempotent.FileIdempotentRepository",
"deprecated": false, "autowired": false, "secret": false, "defaultValue":
"1000", "description": "The size of the 1st-level in-memory cache" },
"fileStore": { "index": 1, "kind": "property", "displayName": "File Store",
"required": true, "type": "object", "javaType": "org. [...]
+ "properties": { "cacheSize": { "index": 0, "kind": "property",
"displayName": "Cache Size", "required": false, "type": "object", "javaType":
"org.apache.camel.support.processor.idempotent.FileIdempotentRepository",
"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": "object", "javaType [...]
}
}
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 7925d46aa3f..30d0d203df8 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
@@ -65,8 +65,8 @@ public class FileIdempotentRepository extends ServiceSupport
implements Idempote
private final AtomicBoolean init = new AtomicBoolean();
private Map<String, Object> cache;
- @Metadata(description = "The size of the 1st-level in-memory cache",
defaultValue = "1000")
- private int cacheSize = 1000;
+ @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)
private File fileStore;
@Metadata(description = "The maximum file size for the file store in
bytes. The default value is 32mb",
@@ -254,36 +254,26 @@ public class FileIdempotentRepository extends
ServiceSupport implements Idempote
this.dropOldestFileStore = dropOldestFileStore;
}
+ @ManagedAttribute(description = "The current 1st-level cache size
(elements in cache)")
+ public int getCacheSize() {
+ if (cache != null) {
+ return cache.size();
+ }
+ return 0;
+ }
+
/**
- * Sets the 1st-level cache size.
- *
- * Setting cache size is only possible when using the default {@link
LRUCache} cache implementation.
+ * Sets the 1st-level maximum cache size.
*/
public void setCacheSize(int size) {
this.cacheSize = size;
- if (cache != null && !(cache instanceof LRUCache)) {
- throw new IllegalArgumentException(
- "Setting cache size is only possible when using the
default LRUCache cache implementation");
- }
- if (cache != null) {
- cache.clear();
- }
- cache = LRUCacheFactory.newLRUCache(size);
}
- @ManagedAttribute(description = "The current 1st-level maximum cache size")
- public int getCacheSize() {
+ @ManagedAttribute(description = "The 1st-level maximum cache size")
+ public int getMaxCacheSize() {
return cacheSize;
}
- @ManagedAttribute(description = "The current 1st-level cache size
(elements in cache)")
- public int getCurrentCacheSize() {
- if (cache != null) {
- return cache.size();
- }
- return 0;
- }
-
/**
* Reset and clears the 1st-level cache to force it to reload from file
*/
@@ -510,7 +500,7 @@ public class FileIdempotentRepository extends
ServiceSupport implements Idempote
if (this.cache == null) {
// default use a 1st level cache
- this.cache = LRUCacheFactory.newLRUCache(cacheSize);
+ this.cache = LRUCacheFactory.newLRUCache(cacheSize <= 0 ? 1000 :
cacheSize);
}
// init store if not loaded before
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 64ad2f7d7a7..cc1f9522619 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
@@ -26,6 +26,7 @@ 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;
/**
@@ -39,13 +40,15 @@ import org.apache.camel.support.service.ServiceSupport;
@Configurer(metadataOnly = true)
@ManagedResource(description = "Memory based idempotent repository")
public class MemoryIdempotentRepository extends ServiceSupport implements
IdempotentRepository {
+
+ private static final int MAX_CACHE_SIZE = 1000;
+
private Map<String, Object> cache;
- @Metadata(description = "Maximum elements that can be stored in-memory",
defaultValue = "1000")
+ @Metadata(description = "Maximum elements that can be stored in-memory",
defaultValue = "" + MAX_CACHE_SIZE)
private int cacheSize;
public MemoryIdempotentRepository() {
- this.cache = LRUCacheFactory.newLRUCache(1000);
}
public MemoryIdempotentRepository(Map<String, Object> set) {
@@ -56,7 +59,7 @@ 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.
*/
public static IdempotentRepository memoryIdempotentRepository() {
- return new MemoryIdempotentRepository();
+ return memoryIdempotentRepository(MAX_CACHE_SIZE);
}
/**
@@ -65,7 +68,10 @@ public class MemoryIdempotentRepository extends
ServiceSupport implements Idempo
* @param cacheSize the cache size
*/
public static IdempotentRepository memoryIdempotentRepository(int
cacheSize) {
- return
memoryIdempotentRepository(LRUCacheFactory.newLRUCache(cacheSize));
+ MemoryIdempotentRepository answer = new MemoryIdempotentRepository();
+ answer.setCacheSize(cacheSize);
+ ServiceHelper.startService(answer);
+ return answer;
}
/**
@@ -131,14 +137,19 @@ public class MemoryIdempotentRepository extends
ServiceSupport implements Idempo
return cache.size();
}
+ @ManagedAttribute(description = "The maximum cache size")
+ public int getMaxCacheSize() {
+ return cacheSize;
+ }
+
public void setCacheSize(int cacheSize) {
this.cacheSize = cacheSize;
}
@Override
protected void doStart() throws Exception {
- if (cacheSize > 0) {
- cache = LRUCacheFactory.newLRUCache(cacheSize);
+ if (cache == null) {
+ cache = LRUCacheFactory.newLRUCache(cacheSize <= 0 ?
MAX_CACHE_SIZE : cacheSize);
}
}
diff --git
a/core/camel-support/src/main/java/org/apache/camel/throttling/ThrottlingExceptionRoutePolicy.java
b/core/camel-support/src/main/java/org/apache/camel/throttling/ThrottlingExceptionRoutePolicy.java
index 4d5f0d4ad99..b2d94b65211 100644
---
a/core/camel-support/src/main/java/org/apache/camel/throttling/ThrottlingExceptionRoutePolicy.java
+++
b/core/camel-support/src/main/java/org/apache/camel/throttling/ThrottlingExceptionRoutePolicy.java
@@ -133,14 +133,14 @@ public class ThrottlingExceptionRoutePolicy extends
RoutePolicySupport implement
protected void doInit() throws Exception {
super.doInit();
- var list = new ArrayList<Class<?>>();
if (exceptions != null && throttledExceptions == null) {
+ var list = new ArrayList<Class<?>>();
for (String fqn : exceptions.split(",")) {
Class<?> clazz =
camelContext.getClassResolver().resolveMandatoryClass(fqn);
list.add(clazz);
}
+ this.throttledExceptions = list;
}
- this.throttledExceptions = list;
}
@Override