diff --git a/core/camel-core-processor/src/main/java/org/apache/camel/processor/MulticastProcessor.java b/core/camel-core-processor/src/main/java/org/apache/camel/processor/MulticastProcessor.java index ea91c04a4e364..f9514de06a130 100644 --- a/core/camel-core-processor/src/main/java/org/apache/camel/processor/MulticastProcessor.java +++ b/core/camel-core-processor/src/main/java/org/apache/camel/processor/MulticastProcessor.java @@ -24,7 +24,6 @@ import java.util.Map; import java.util.Objects; import java.util.concurrent.ConcurrentHashMap; -import java.util.concurrent.ConcurrentMap; import java.util.concurrent.Executor; import java.util.concurrent.ExecutorService; import java.util.concurrent.RejectedExecutionException; @@ -185,7 +184,7 @@ public void execute(Runnable command) { private boolean shutdownAggregateExecutorService; private final long timeout; private final int cacheSize; - private final ConcurrentMap errorHandlers; + private final Map errorHandlers; private final boolean shareUnitOfWork; public MulticastProcessor(CamelContext camelContext, Route route, Collection processors) { @@ -196,7 +195,7 @@ public MulticastProcessor(CamelContext camelContext, Route route, Collection processors, @@ -225,9 +224,9 @@ public MulticastProcessor(CamelContext camelContext, Route route, Collection= 0) { - this.errorHandlers = (ConcurrentMap) LRUCacheFactory.newLRUCache(cacheSize); + this.cacheSize = cacheSize == 0 ? CamelContextHelper.getMaximumCachePoolSize(camelContext) : cacheSize; + if (this.cacheSize > 0) { + this.errorHandlers = LRUCacheFactory.newLRUCache(this.cacheSize); } else { // no cache this.errorHandlers = null; diff --git a/core/camel-core/src/test/java/org/apache/camel/support/cache/SimpleLRUCacheTest.java b/core/camel-core/src/test/java/org/apache/camel/support/cache/SimpleLRUCacheTest.java index 26db35b44861f..8c1b6438a06ff 100644 --- a/core/camel-core/src/test/java/org/apache/camel/support/cache/SimpleLRUCacheTest.java +++ b/core/camel-core/src/test/java/org/apache/camel/support/cache/SimpleLRUCacheTest.java @@ -329,6 +329,13 @@ void ensureEvictionOrdering() { assertTrue(consumed.contains("Two")); } + @ParameterizedTest + @ValueSource(ints = { 0, -1 }) + void validateCacheSize(int maximumCacheSize) { + assertThrows(IllegalArgumentException.class, () -> new SimpleLRUCache<>(16, maximumCacheSize, x -> { + })); + } + @ParameterizedTest @ValueSource(ints = { 1, 2, 5, 10, 20, 50, 100, 1_000 }) void concurrentPut(int maximumCacheSize) throws Exception { diff --git a/core/camel-support/src/main/java/org/apache/camel/support/cache/SimpleLRUCache.java b/core/camel-support/src/main/java/org/apache/camel/support/cache/SimpleLRUCache.java index c1fcb77d41be0..ea7e614c80556 100644 --- a/core/camel-support/src/main/java/org/apache/camel/support/cache/SimpleLRUCache.java +++ b/core/camel-support/src/main/java/org/apache/camel/support/cache/SimpleLRUCache.java @@ -68,6 +68,9 @@ public class SimpleLRUCache extends ConcurrentHashMap { public SimpleLRUCache(int initialCapacity, int maximumCacheSize, Consumer evicted) { super(initialCapacity, DEFAULT_LOAD_FACTOR); + if (maximumCacheSize <= 0) { + throw new IllegalArgumentException("The maximum cache size must be greater than 0"); + } this.maximumCacheSize = maximumCacheSize; this.evict = Objects.requireNonNull(evicted); }