Skip to content

Commit

Permalink
FileIdempotentRepository - Make configuring cacheSize more standard v…
Browse files Browse the repository at this point in the history
…ia plain setter and creation in doStart
  • Loading branch information
davsclaus committed Mar 21, 2024
1 parent 7493f1a commit a7e6969
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.apache.camel.support.processor.idempotent.FileIdempotentRepository", "deprecated": false, "autowired": false, "secret": false, "description": "File name of the repository (incl directory)" }, "maxFileStoreSize": { "index": 2, "kind": "property", "displayName": "Max File Store Size", "required": false, "type": "object", "javaType": "org.apache.camel.support.processor.idempotent.FileIdempotentRepository", "deprecated": false, "autowired": false, "secret": false, "defaultValue": "32768000", "description": "The maximum file size for the file store in bytes. The default value is 32mb" }, "dropOldestFileStore": { "index": 3, "kind": "property", "displayName": "Drop Oldest File Store", "required": false, "type": "object", "javaType": "org.apache.camel.support.processor.idempotent.FileIdempotentRepository", "deprecated": false, "autowired": false, "secret": false, "defaultValue": "1000", "description": "Sets the number of oldest entries to drop from the file store when the maximum capacity is hit to reduce disk space to allow room for new entries." } }
"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": "org.apache.camel.support.processor.idempotent.FileIdempotentRepository", "deprecated": false, "autowired": false, "secret": false, "description": "File name of the repository (incl directory)" }, "maxFileStoreSize": { "index": 2, "kind": "property", "displayName": "Max File Store Size", "required": false, "type": "object", "javaType": "org.apache.camel.support.processor.idempotent.FileIdempotentRepository", "deprecated": false, "autowired": false, "secret": false, "defaultValue": "32768000", "description": "The maximum file size for the file store in bytes. The default value is 32mb" }, "dropOldestFileStore": { "index": 3, "kind": "property", "displayName": "Drop Oldest File Store", "required": false, "type": "object", "javaType": "org.apache.camel.support.processor.idempotent.FileIdempotentRepository", "deprecated": false, "autowired": false, "secret": false, "defaultValue": "1000", "description": "Sets the number of oldest entries to drop from the file store when the maximum capacity is hit to reduce disk space to allow room for new entries." } }
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -36,10 +35,12 @@ public void testMove() throws Exception {
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.apache.camel.support.processor.idempotent.FileIdempotentRepository", "deprecated": false, "autowired": false, "secret": false, "description": "File name of the repository (incl directory)" }, "maxFileStoreSize": { "index": 2, "kind": "property", "displayName": "Max File Store Size", "required": false, "type": "object", "javaType": "org.apache.camel.support.processor.idempotent.FileIdempotentRepository", "deprecated": false, "autowired": false, "secret": false, "defaultValue": "32768000", "description": "The maximum file size for the file store in bytes. The default value is 32mb" }, "dropOldestFileStore": { "index": 3, "kind": "property", "displayName": "Drop Oldest File Store", "required": false, "type": "object", "javaType": "org.apache.camel.support.processor.idempotent.FileIdempotentRepository", "deprecated": false, "autowired": false, "secret": false, "defaultValue": "1000", "description": "Sets the number of oldest entries to drop from the file store when the maximum capacity is hit to reduce disk space to allow room for new entries." } }
"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": "org.apache.camel.support.processor.idempotent.FileIdempotentRepository", "deprecated": false, "autowired": false, "secret": false, "description": "File name of the repository (incl directory)" }, "maxFileStoreSize": { "index": 2, "kind": "property", "displayName": "Max File Store Size", "required": false, "type": "object", "javaType": "org.apache.camel.support.processor.idempotent.FileIdempotentRepository", "deprecated": false, "autowired": false, "secret": false, "defaultValue": "32768000", "description": "The maximum file size for the file store in bytes. The default value is 32mb" }, "dropOldestFileStore": { "index": 3, "kind": "property", "displayName": "Drop Oldest File Store", "required": false, "type": "object", "javaType": "org.apache.camel.support.processor.idempotent.FileIdempotentRepository", "deprecated": false, "autowired": false, "secret": false, "defaultValue": "1000", "description": "Sets the number of oldest entries to drop from the file store when the maximum capacity is hit to reduce disk space to allow room for new entries." } }
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -254,36 +254,26 @@ public void setDropOldestFileStore(long dropOldestFileStore) {
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
*/
Expand Down Expand Up @@ -510,7 +500,7 @@ protected void doStart() throws Exception {

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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
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;

/**
Expand All @@ -39,13 +40,15 @@
@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) {
Expand All @@ -56,7 +59,7 @@ public MemoryIdempotentRepository(Map<String, Object> set) {
* 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);
}

/**
Expand All @@ -65,7 +68,10 @@ public static IdempotentRepository memoryIdempotentRepository() {
* @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;
}

/**
Expand Down Expand Up @@ -131,14 +137,19 @@ public int getCacheSize() {
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);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,14 +133,14 @@ public void setExceptions(String exceptions) {
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
Expand Down

0 comments on commit a7e6969

Please sign in to comment.