This is an automated email from the ASF dual-hosted git repository. nfilotto pushed a commit to branch CAMEL-21432/reject-cache-size-0 in repository https://gitbox.apache.org/repos/asf/camel.git
commit c39efb98cd0e5ad3bdc02c64a4351a410733b27b Author: Nicolas Filotto <[email protected]> AuthorDate: Tue Nov 12 11:24:14 2024 +0100 CAMEL-21432: camel-core - Prevent 0 as cache size for multicast EIP --- .../java/org/apache/camel/processor/MulticastProcessor.java | 11 +++++------ .../org/apache/camel/support/cache/SimpleLRUCacheTest.java | 7 +++++++ .../java/org/apache/camel/support/cache/SimpleLRUCache.java | 3 +++ 3 files changed, 15 insertions(+), 6 deletions(-) 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 ea91c04a4e3..f9514de06a1 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.List; 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 class MulticastProcessor extends AsyncProcessorSupport private boolean shutdownAggregateExecutorService; private final long timeout; private final int cacheSize; - private final ConcurrentMap<Processor, Processor> errorHandlers; + private final Map<Processor, Processor> errorHandlers; private final boolean shareUnitOfWork; public MulticastProcessor(CamelContext camelContext, Route route, Collection<Processor> processors) { @@ -196,7 +195,7 @@ public class MulticastProcessor extends AsyncProcessorSupport AggregationStrategy aggregationStrategy) { this(camelContext, route, processors, aggregationStrategy, false, null, false, false, false, 0, null, - false, false, CamelContextHelper.getMaximumCachePoolSize(camelContext)); + false, false, 0); } public MulticastProcessor(CamelContext camelContext, Route route, Collection<Processor> processors, @@ -225,9 +224,9 @@ public class MulticastProcessor extends AsyncProcessorSupport this.parallelAggregate = parallelAggregate; this.processorExchangeFactory = camelContext.getCamelContextExtension() .getProcessorExchangeFactory().newProcessorExchangeFactory(this); - this.cacheSize = cacheSize; - if (cacheSize >= 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 26db35b4486..8c1b6438a06 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 @@ class SimpleLRUCacheTest { 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 c1fcb77d41b..ea7e614c805 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<K, V> extends ConcurrentHashMap<K, V> { public SimpleLRUCache(int initialCapacity, int maximumCacheSize, Consumer<V> 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); }
